BioLounge
BioLounge

Reputation: 51

How to write a multidiminsional array as tab delimited .txt file in perl

I have a multidimensional array called @main and I want to write this array into a tab delimited .txt file in perl Can anyone help me in this issue?

Upvotes: 1

Views: 4796

Answers (5)

knb
knb

Reputation: 9295

If "multidimensional" in your question means n > 2, the tab delimited format might be infeasible.

Is this a case where you want to solve a more general problem: to serialize a data structure?

Look for instance, at the YAML Module (install YAML::XS). There is a DumpFile(filepath, list) and a LoadFile(filepath) method. The output will not be a tab-delimited file, but still be human readable.

You could also use a JSON serializer instead, e.g. JSON::XS.

Upvotes: 1

TLP
TLP

Reputation: 67908

open my $fh, '>', "out.txt" or die $!;
print $fh (join("\t", @$_), "\n") for @array;

Upvotes: 5

Adam Batkin
Adam Batkin

Reputation: 52994

I'm guessing that your multi-dimensional array is actually an array of references to arrays, since that's the only way that Perl will let you embed an array-in-an-array.

So for example:

@array1 = ('20020701', 'Sending Mail in Perl', 'Philip Yuson');
@array2 = ('20020601', 'Manipulating Dates in Perl', 'Philip Yuson');
@array3 = ('20020501', 'GUI Application for CVS', 'Philip Yuson');

@main = (\@array1, \@array2, \@array3);

To print them to a file:

open(my $out, '>', 'somefile.txt') || die("Unable to open somefile.txt: $!");
foreach my $row (@main) {
    print $out join(",", @{$row}) . "\n";
}
close($out);

Upvotes: 2

David W.
David W.

Reputation: 107040

Two dimensions I hope:

 foreach my $row (@array) {
     print join ("\t", @{$row}) . "\n";
 }

Perl doesn't have multidimensional arrays. Instead, one of its three native datatypes ia a one dimensional array called a List. If you need a more complex structure in Perl, you can use references to other data structures in your List. For example, each item in your List is a reference to another List. The primary list can represent the rows, and the secondary list are the column values in that row.

In the above foreach loop is looping through the primary list (the one that represents each row), and $row is equal to the reference to the list that represents the column values.

In order to get a Perl list and not a reference to the list, I dereference the reference to the list. I do that by prefixing it with an @ sign. I like using @{$row} because I think it's a little cleaner than just @$row.

Now that I can refer to my list of column values as @{$row}, I can use a join to create a string that separates each of the values in @{$row} with a tab character and print it out.

Upvotes: 1

Seth Robertson
Seth Robertson

Reputation: 31461

That is not a multidimensional array, it is an array that was formed by concatenating three other arrays.

perl -e '@f=(1,2,3); @g=(4,5,6); @h=(@f,@g); print join("\t",@h)."\n";'

Please provide desired output if you want further help.

Upvotes: 1

Related Questions