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

Hi, I need to create a two dimensional array, insert a string and an index and then sort it by the index. After I finished using the array I want to empty it for reuse. Any suggestion on how I can accomplise this. --kirk
  • Comment on Creating a two dimensional array and sorting by index

Replies are listed 'Best First'.
Re: Creating a two dimensional array and sorting by index
by fruiture (Curate) on Dec 09, 2002 at 19:27 UTC

    See perldata, perllol and perlfunc (for map and sort).

    my @AoA = ( [ 'foo' , 1 ], [ 'bar' , 5 ], [ 'buz' , 3 ], ); print "@$_\n" for sort { $a->[1] <=> $b->[1] } @AoA; @AoA = (); #free structure
    --
    http://fruiture.de
      Can I do it like this, since I don't know what the string is going to be just there order. my @AoA = ( $string1 , 1 , $string2 , 5 , $string3 , 3 , ); print "@$_\n" for sort { $a->1 <=> $b->1 } @AoA; @AoA = (); #free structure
Re: Creating a two dimensional array and sorting by index
by jdporter (Paladin) on Dec 09, 2002 at 19:32 UTC
    Assuming that "index", in this case, is just a number, not related to the index of items in the array, then you could do something like this:
    my @things; push @things, [ $string, $index ]; # as many times as necessary for ( sort { $a->[1] <=> $b->[1] } @things ) { # do whatever with it, e.g. print: print "Index: $_->[1] String: $_->[0]\n"; }
    And then to clear it out:     @things = ();

    jdporter
    ...porque es dificil estar guapo y blanco.

Re: Creating a two dimensional array and sorting by index
by BrowserUk (Patriarch) on Dec 09, 2002 at 19:24 UTC

    I don't understand your question. The main advantage of an array, other than simple aggregation, is that you access it by index in the order it was created. In that sense, it is already sorted by index.

    Could you give us a bit more information to go on about what you want to do?

    There are so many ways to interpret what you have asked, any solution offered would be nothing more than a guess as to your real requirements.


    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

      Hi, Iam reading a text file and greping for certain strings and outputing them to a new file. In the text files there are the following string: apple plum cherry orange I want to match for these words and print them out to a file like this: plum|apple|orange|cherry To do this I need to assign a number to each string and then resort these numbers to resort the string. Creating a two dimensional array I think will do this. --kirk
Re: Creating a two dimensional array and sorting by index
by pg (Canon) on Dec 09, 2002 at 19:27 UTC
    Try this, the real trick only takes one line:
    use strict; my $AoA = [[1, "apple"], [4, "pear"], [2, "plum"], [3, "orange"]]; print join("|", map {$_->[1]} sort {$a->[0] <=> $b->[0]} @{$AoA});