multidimensional array in perl

i'm trying to open a file with three or more columns and an undetermined, but finite number of rows. I want to define an array for each row with each element of the row as a sub array. The columns are separated by tabs or spaces.

Here's the file:

12x3.12z34b.342sd3.sds 454.23.23.232 aSDGgsOds
4z3x3.134b.332sSSsd3.Ccsds 354.23.2113.232 aSDffGgds
12x333.123ww4b.342sed3.sdsO 22454.23.23.20032 BSDGgds
...

i'm trying it like this:

#!/usr/bin/perl -w
use warnings;
use strict;

open (FILE, xyz.txt) or die " $!";
   our @lines<FILE>;
   chomp @lines;
close (FILE);

for (@lines) {
  $/="\t|' '";      # field sperator is either tab or ' '
  our @lines=([split / /, $_]);
}

print "First element \'$lines[0][0]\'\n";
print "Second element \'$lines[0][1]\'\n";
print "Third element \'$lines[0][2]\'\n";
print "First element \'$lines[1][0]\'\n";
...

or somehow make a hash reference to each element of the row

...
print "First element \'$lines{0}->{first]\'\n";
print "Second element \'$lines{0}->{second}\'\n";
print "Third element \'$lines{0}->third}\'\n";
print "First element \'$lines{1}->{first}\'\n";
...

any ideas?

thanks

That will take a while to process......

sorry i meant 'undetermined, but finite number of rows' :wink:

Here is an example:

open(FILE, '<x.txt') or die $!;
my @lines;
while (<FILE>) {
	chomp;
	@lines = (@lines, [split(/\s+/)]);
}
close FILE;

foreach (@lines) {
	printf("%-35s|%-30s|%-15s\n", $_->[0], $_->[1], $_->[2]);
}

exactly... thanks! :b:

How can I generate this data as an array of hashes?

12x3.12z34b.342sd3.sds 454.23.23.232 aSDGgsOds
4z3x3.134b.332sSSsd3.Ccsds 354.23.2113.232 aSDffGgds
12x333.123ww4b.342sed3.sdsO 22454.23.23.20032 BSDGgds

like this:

@lines = ( {
AA => "454.23.23.232"
BB => "12x3.12z34b.342sd3.sds"
CC => "aSDGgsOds"
},
{
AA => "354.23.2113.232"
BB => "4z3x3.134b.332sSSsd3.Ccsds"
CC => "aSDffGgds"
},
{
AA => "22454.23.23.20032"
BB => "12x333.123ww4b.342sed3.sdsO"
CC => "BSDGgds"
}
);

but my syntax isn't working...

open(FILE, '<x.txt') or die $!;
my @lines;
while (<FILE>) {
	chomp;
	 my ($BB,$AA,$CC)=split(/\s+/,$_);
         $lines[$_]={BB=>$BB,AA=>$AA,CC=>$CC};
}
close FILE;

print $lines[0]{BB},"\n";
...
open(FILE, '<x.txt') or die $!;
my @lines;
while (<FILE>) {
	chomp;
	 my ($BB,$AA,$CC)=split(/\s+/,$_);
         @lines = (@lines, {BB=>$BB,AA=>$AA,CC=>$CC});
}
close FILE;

print $lines[0]->{BB},"\n";
...

nice, what id I have missing lines or columns in the data. What is the syntax to shift to next element/key if one is missing while still separating by every new line?

IE:
12x3.12z34b.342sd3.sds 454.23.23.232 aSDGgsOds

4z3x3.134b.332sSSsd3.Ccsds 354.23.2113.232
12x333.123ww4b.342sed3.sdsO 22454.23.23.20032 BSDGgds

...

For the missing line, you will need to check whether the line is empty in the loop and skip over. This is an easy exercise. Try to find out yourself first.

Missing column sometimes cannot be checked. It depends on how irregular your data set can be.

i'm just doing something simple now:

if (!$_) {
  next;
} else {
...

wanted to see how else it can be done. thanks again for the help :b: