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

I'm trying to create an array of arrays using array references. I think I've narrowed the problem down. When I attempt to print out the array of arrays, I get the same data for each array reference. I printed out all of the reference numbers to arrays and they were all the same.

Maybe my code will help explain:

foreach my $file (@homes) { open(HOUSE, "<$base_directory/homes/$file") || die &show_error("Unab +le to open file in sort routine"); @home = <HOUSE>; close(HOUSE); # create array of arrays chomp(@home); push @sorted, \@home; }

When I print the references stored in the array, i get ARRAY(0x8108f20) ARRAY(0x8108f20) ARRAY(0x8108f20). How do I get this code to create different array references, so that when I go to use the data, I get different information each time through. Basically, N different array references based on what is read in from "$file". Hope this makes sense.

Edit ar0n -- wrapped code in code tags

Replies are listed 'Best First'.
Re: help with array of arrays
by gav^ (Curate) on Apr 01, 2002 at 01:58 UTC
    The problem is that you are using the same array on each loop iteration. You either have to create an anonymous array with:
    push @sorted, [@home];
    or make sure you use my to ensure you are using a new array in your current scope:
    my @home = <HOUSE>; chomp @home; push @sorted, \@home;
    If you use a module like Data::Dump or Data::Dumper to print out your array, you'll find it easy to work out what's going on.

    gav^

Re: help with array of arrays
by PrimeLord (Pilgrim) on Apr 01, 2002 at 02:06 UTC
    You have to dereference the array values in order to access them. if you wanted to access all of the values of the arrays in the sorted array you could do something like this.

    foreach (@sorted) { print "@{$_}\n"; }


    That will give you each element in each array of their own line. HTH.

    -Prime
Re: help with array of arrays
by emilford (Friar) on Apr 01, 2002 at 02:36 UTC
    Thanks for the responses! Actually by just saying my @home each time through the loop, I was able to create an array of seperate references. But now that that is working, why am I having trouble sorting the array?

    I have an array of array references, which I want to be sorted by the second element of the referenced array.
    foreach my $sort (@sorted){ print "<h1>$sort->[2]</h1>"; } # sort the array of arrays by price @sorted = sort {$main::a->[2] <=> $main::b->[2]} @sorted; foreach my $sort (@sorted){ print "<h1>$sort->[2]</h1>"; }
    With this code, the numbers are printed out as follows.

    Before Sorting
    4,900 1,900 1,333,987

    After sorting
    1,900 1,333,987 4,900

    Those don't seem to be in ascending order to me! Any help is appreciated.

      Yup, that's sorted. What's tripping you up is the commas. Because you're comparing them using <=>, they get compared in numerical context. Sure enough, "1,900"+0 and "1,333,987"+0 are both 1, so they're at the beginning of the "sorted" list. You may want to uncommify the data when you read it in, and put the commas back in only when you print it.

      perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

      You need to use strict and warnings. You are trying to compare two things that aren't actually numbers (as they have a comma in them). If you had warnings on you'd get something like:
      Argument "1,900" isn't numeric in sort at sort.pl line 7, <DATA> line +3. Argument "4,900" isn't numeric in sort at sort.pl line 7, <DATA> line +3. Argument "1,333,987" isn't numeric in sort at sort.pl line 7, <DATA> l +ine 3.
      You also don't need to use $main::a and $main::b as $a and $b are special and are globals.

      You probably want to do something like:

      @sorted = sort {$a->[2] <=> $b->[2]} map { s/,//g; $_ } @sorted;
      You might want to look up the Schwartzian Transform or perldoc sort.

      gav^

Re: help with array of arrays
by emilford (Friar) on Apr 01, 2002 at 03:05 UTC
    You guys have been more than helpful. Everything is falling in to place and starting to work how I want it. I've got much to learn...thanks again for your help.
Re: help with array of arrays
by indapa (Monk) on Apr 01, 2002 at 18:15 UTC
    Also you might want to take a look at perldsc it has a good explaination of array of arrays, hashes of arrays, arrays of hashes, etc