stu96art has asked for the wisdom of the Perl Monks concerning the following question:

I have data that I would like to put into a 2D array, then be able to sort on one element, and be able to reference the new array elements with $rec[0][1], $rec[11][2],  $rec[1][1] and so forth. Here is the data first, code, then results when I Dumped it. I like the results, I just cannot reference them individually.
1 1229.003932 1348.475942 2 1127.677089 1628.387836 3 1830.910815 686.226114 4 2181.269057 780.242484 5 2302.996066 850.572327 6 1201.169859 1107.266903 7 1266.842105 665.334403 8 1471.475049 644.755582 9 2278.018406 1909.274463 10 2616.853057 1779.732754 11 2730.703647 1697.259759 12 1608.323892 1555.214756 13 1719.087310 1988.052650 14 1924.751609 1987.482170
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @data; my ($c, $i, $j); open ( DATA, "<c:/my documents/npoint.txt" ) or die ("could not open f +ile. &!"); $c = 0; # Loop while there are more lines to read while (not eof DATA) { # Read four lines and store them in the @record array my @record = ( scalar(<DATA>), scalar(<DATA>), scalar(<DATA>) ); $c = $c + 3; # Remove the new-line from the end of the lines chomp @record; # Store a reference to this @record instance # in the @data array. push @data, \ @record; } my @sorted = sort { $a -> [1] <=> $b -> [1] } @data; print Dumper(@sorted); #for ($i = 0; $i < ($c); $i++) { # for ($j = 0; $j <= 2; $j++) { # print $sorted[$i][$j] ; # } #}
Results : I am happy with the results, but I want to be able to use $sorted[0]2, or $sorted21. The stuff in comments is what I tried, but it would not work
$VAR1 = [ '2', '1127.677089', '1628.387836' ]; $VAR2 = [ '6', '1201.169859', '1107.266903' ]; $VAR3 = [ '1', '1229.003932', '1348.475942' ]; $VAR4 = [ '7', '1266.842105', '665.334403' ]; $VAR5 = [ '8', '1471.475049', '644.755582' ]; $VAR6 = [ '12', '1608.323892', '1555.214756' ]; $VAR7 = [ '13', '1719.087310', '1988.052650' ]; $VAR8 = [ '3', '1830.910815', '686.226114' ]; $VAR9 = [ '14', '1924.751609', '1987.482170' ]; $VAR10 = [ '4', '2181.269057', '780.242484' ]; $VAR11 = [ '9', '2278.018406', '1909.274463' ]; $VAR12 = [ '5', '2302.996066', '850.572327' ]; $VAR13 = [ '10', '2616.853057', '1779.732754' ]; $VAR14 = [ '11', '2730.703647', '1697.259759' ];

Replies are listed 'Best First'.
Re: Referencing an array
by diotalevi (Canon) on Feb 05, 2003 at 22:01 UTC
    for my $record (@sorted) { print @$record; } # OR for your other request print $sorted[0][2], $sorted[2][1];

    Now remove all the $c, $i, $j garbage. You don't need and it's just making your code harder to read. Oh and you might want to remove that chomp @record line if you don't want everything all on one line.


    Seeking Green geeks in Minnesota

Re: Referencing an array
by JamesNC (Chaplain) on Feb 06, 2003 at 00:04 UTC
    Or if you really want to try it your way... :-)

    Sometimes it helps to just print out the @sorted... you would see it was a bunch of refs to arrays... so then you could just do another print $sorted[0] to see what that gave you... you would see it too was another ARRAY(0x22518c) you would get to it with either $sorted[0]->[0] or ${$sorted[0]}[0] Which is kinda hard to read IMHO...

    Anyway, the other problem you had.. you were on the right track in a way.. was that your var 'c' just needed to count the number of arrays you created instead of the number of lines :)

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @data; my @sorted; my ($c, $i, $j); #open ( DATA, "<c:/my documents/npoint.txt" ) or die ("could not open +file. &!"); $c = 0; # Loop while there are more lines to read while (not eof DATA) { # Read four lines and store them in the @record array my @record = ( scalar(<DATA>), scalar(<DATA>), scalar(<DATA>) ); $c++; # Remove the new-line from the end of the lines chomp @record; # Store a reference to this @record instance # in the @data array. push @data, \ @record; } @sorted = sort { $a -> [1] <=> $b -> [1] } @data; for ($i = 0; $i < $c; $i++) { for ($j = 0; $j <= 2; $j++) { printf "%4s ", $sorted[$i]->[$j] if $j==0; printf " %20s", $sorted[$i]->[$j] if $j>0; print "\n" if $j ==2; } } __DATA__ 1 1229.003932 1348.475942 2 1127.677089 1628.387836 3 1830.910815 686.226114 4 2181.269057 780.242484 5 2302.996066 850.572327 6 1201.169859 1107.266903 7 1266.842105 665.334403 8 1471.475049 644.755582 9 2278.018406 1909.274463 10 2616.853057 1779.732754 11 2730.703647 1697.259759 12 1608.323892 1555.214756 13 1719.087310 1988.052650 14 1924.751609 1987.482170
    Hope this helps... :-) fixed typo...
Re: Referencing an array
by Cabrion (Friar) on Feb 06, 2003 at 01:50 UTC
    while (not eof DATA) { # Read four lines and store them in the @record array my @record = ( scalar(<DATA>), scalar(<DATA>), scalar(<DATA>) ); $c = $c + 3; # Remove the new-line from the end of the lines chomp @record; # Store a reference to this @record instance # in the @data array. $data[$record[0]] = [$record[1],$record[2]]; } # Arrays start at zero and the sort below will barf if you # don't have each element in the array be an array ref $data[0] = [] unless $data[0]; close DATA; my @sorted = sort { $a->[0] <=> $b->[0] } @data; print Dumper(@sorted)."\n"; for ($i = 0; $i < ($c); $i++) { next unless defined($sorted[$i]); for ($j = 0; $j <= 1; $j++) { print $sorted[$i][$j]."\n" if defined $sorted[$i][$j]; } }
    You will need to figure out what to do about $data[0]. I wasn't sure if the 1,2,3... from your data were important as values or they just defined order. If you the values are just to maintain order, then you could  shift @data instead of creating the dummy  $data[0] = ....

    Plenty left for you to think through and tinker with.