in reply to array of arrays

It sounds to me like you need a different data structure, a HoA (Hash of Array). This is a hash key that is associated with a list of many key values. This is a more "natural" (meaning more Perl way) for what you are doing.
#!/usr/bin/perl -w use strict; my %hash; while (<DATA>) { my($id,$value)=split; push @{$hash{$id}}, $value; } foreach my $id (sort{$a<=>$b} keys (%hash)) { print "id=$id values=@{$hash{$id}}\n"; } #prints: #id=1 values=3 2 10 #id=2 values=5 6 #id=3 values=4 __DATA__ 1 3 1 2 1 10 2 5 2 6 3 4
The "tricky" syntax part is that "extra {}" in @{$hash{$id}} is necessary when using a sub sub-scripted variable (ie just @$hash{$id} won't work! The extra {} tells @ what to operate upon.

I don't know if this is some sort of homework problem?

Replies are listed 'Best First'.
Re^2: array of arrays
by Limbic~Region (Chancellor) on Jun 29, 2009 at 13:42 UTC
    Marshall,
    This is a more "natural" (meaning more Perl way) for what you are doing.

    Would you mind explaining that. The two primary reasons you would use a hash here are if the indexes were not numerical or sparsely populated and you wanted to conserve space. Those 'ifs' are assumptions not supported (or refuted) by facts presented by the OP. While I agree that chances are it is a better fit for this data, making such statements to a (presumably) newbie without explanation can lead to cargo culting.

    Cheers - L~R

      Hi L~R!

      I don't know what "cargo culting" means. I asked about a homework assignment because this problem didn't seem "real-world" to me. Your points about non-numeric, etc are well taken.

      One thing that newbies often miss is the power of Perl list processing. Most of the common languages iterate over some array with an index variable array[$i]. In Perl the way is to iterate over all elements and the index doesn't matter. There is a lot of literature about "off-by-one" errors. Processing a Perl list avoids this problem - there is no "index" variable. It becomes: for everything in this list, do "X".

      I would go as far as to say that as one's experience in Perl increases, the use of [$i] decreases. Now of course there are very valid reasons to use this type of statement! But in general a new Perl'er will overuse this. The use of the Perl hash is also often underused.

        Marshall,
        Please do read cargo cult then as it will be enlightening. In a nutshell, it means blindly following a ritual in the belief it will lead to the desired results. By telling someone who is presumably very new to perl that they should be using a HoA instead of an AoA because it is the more perlish way to do things without explaining why, could and has, resulted in someone always using a HoA even in situations when an AoA is better.

        I don't disagree with your position on the power of perl or how it is often misused or underutilized. My issue was that you didn't bother to explain yourself or even make your advice conditional upon stated assumptions. This can lead down the path of cargo culting.

        My opinion isn't shared by everyone and there are a number of good counter points. For more, see Breaking The Rules and Breaking The Rules II.

        Cheers - L~R