Help with sorting lists of lists

Discussion in 'Perl Misc' started by Chris Weisiger, Oct 14, 2004.

  1. I apologize if this is the wrong place to post this; please let me know
    the proper target if so.

    I have a list of lists of numbers, and I want to sort the sublists.
    Every variant on the syntax that I've tried either doesn't visibly do
    anything to sort the lists, or else undefines them all. Here's what I
    originally tried:

    for (my $i = 0; $i < $numActors; ++$i)
    {
    for (my $j = 0; $j < $actors[$i]; ++$j)
    {
    $prolls[$i][$j] = int (rand(20) + 1);
    } }
    @prolls[$i] = sort {$a <=> $b} @prolls[$i];
    } }

    I suspect that I'm getting tripped up by how Perl handles lists of
    lists and references and all that, but all the searching I've done
    hasn't turned up anything that's worked. Any help would be appreciated.

    For reference, I'm working on a dice rolling program for the Donjon
    tabletop RPG. It's a great system, but it needs a lot of d20s...

    --
    "Don't take life so serious, son - it ain't nohow permanent." -
    Porkypine
    http://www.cs.hmc.edu/~cweisige
     
    Chris Weisiger, Oct 14, 2004
    #1
    1. Advertising

  2. Chris Weisiger <> wrote in
    news::

    > I have a list of lists of numbers, and I want to sort the
    > sublists.


    > Every variant on the syntax that I've tried either doesn't visibly do
    > anything to sort the lists, or else undefines them all. Here's what I
    > originally tried:
    >
    > for (my $i = 0; $i < $numActors; ++$i)


    Life is generally easier without C-style loops.

    > {
    > for (my $j = 0; $j < $actors[$i]; ++$j)
    > {
    > $prolls[$i][$j] = int (rand(20) + 1);
    > } }
    > @prolls[$i] = sort {$a <=> $b} @prolls[$i];


    You should

    use strict;

    and

    use warnings;

    What you probably want to do is set the $i th element of @prolls to a
    reference to the sorted array: i.e. (untested)

    $prolls[$i] = [ sort { $a <=> $b } @{ $prolls[$i] } ];

    Since you did not post a self-contained script that I could run (why not?),
    I am not sure this would solve your problem. Take a look at

    perldoc perlreftut

    #! perl

    use strict;
    use warnings;

    my @lol = (
    [ 3, 7, 4, 5 ],
    [ 11, 15, 1, 3, 5 ],
    [ 2, 1, -1, 3, 4, 5, 9, 22, 11 ],
    );

    for (@lol) {
    $_= [ sort {$a <=> $b} @{ $_ } ];
    }

    use Data::Dumper;
    print Dumper \@lol;
    __END__

    Sinan
     
    A. Sinan Unur, Oct 14, 2004
    #2
    1. Advertising

  3. On 10/13/04 4:42 PM, A. Sinan Unur wrote:
    > Chris Weisiger <> wrote in
    > news::
    >
    >> I have a list of lists of numbers, and I want to sort the
    >> sublists.

    >
    >> Every variant on the syntax that I've tried either doesn't visibly do
    >> anything to sort the lists, or else undefines them all. Here's what I
    >> originally tried:
    >>
    >> for (my $i = 0; $i < $numActors; ++$i)

    >
    > Life is generally easier without C-style loops.


    Probably; my style is in general not that good since it's a mishmash of
    C styles and Perl styles.

    >
    > You should
    >
    > use strict;
    >
    > and
    >
    > use warnings;


    Done and done, as a matter of course.

    >
    > What you probably want to do is set the $i th element of @prolls to a
    > reference to the sorted array: i.e. (untested)
    >
    > $prolls[$i] = [ sort { $a <=> $b } @{ $prolls[$i] } ];


    And this does the trick. Thank you very much. I'm still not certain
    exactly what is going on here, but at least the code works.

    >
    > Since you did not post a self-contained script that I could run (why
    > not?), I am not sure this would solve your problem. Take a look at
    >
    > perldoc perlreftut


    Strange that perldoc.com didn't come up in my copious Google searching.
    I'll keep it bookmarked. Again, thanks.

    --
    "Don't take life so serious, son - it ain't nohow permanent." -
    Porkypine
    http://www.cs.hmc.edu/~cweisige
     
    Chris Weisiger, Oct 14, 2004
    #3
  4. Chris Weisiger <> wrote in
    news::

    >> perldoc perlreftut

    >
    > Strange that perldoc.com didn't come up in my copious Google
    > searching.


    No need to venture out to the whole world wide web. You can access the Perl
    documentation installed on your computer using this command from the shell
    you are using.

    To see the available documentation, type

    perldoc perltoc

    on the command line.

    Sinan.
     
    A. Sinan Unur, Oct 14, 2004
    #4
  5. Chris Weisiger <> writes:
    > On 10/13/04 4:42 PM, A. Sinan Unur wrote:
    >> $prolls[$i] = [ sort { $a <=> $b } @{ $prolls[$i] } ];

    >
    > And this does the trick. Thank you very much. I'm still not certain
    > exactly what is going on here, but at least the code works.


    Cargo-cult programming, even when the source is a Respected Regular,
    is always bad. So let's take a look at what's going on, shall we?

    >> $prolls[$i] =


    Well, you know what this is, I hope, so we'll skip to the right-hand side.

    >> [ sort { $a <=> $b } @{ $prolls[$i] } ]


    [ something ] returns a reference to an anonymous array that contains
    the list 'something'. By that, we know two things:

    1) $prolls[$i] will contain a reference to an anonymous array, and
    2) sort { $a <=> $b } @{ $prolls[$i] } is a list, or at the very least
    will be interpreted in list context.

    Okay, given that, we're almost done, but since I'm taking baby steps
    already, I'll continue for the benefit of the peanut gallery. If you
    look up the documentation for 'sort' (via: 'perldoc -f sort' at the
    command line), you will see that one of its forms is

    sort BLOCK LIST

    I'll leave reading sort's documentation to you, Dear Reader, but the
    clear implication of this is that

    >> { $a <=> $b }


    is a block, and

    >> @{ $prolls[$i] }


    is a list. This is further confirmed by the fact that @{ something }
    is one way to dereference a reference to an array, and get that array
    back. This is documented in perlref.pod; read it NOW, via 'perldoc
    perlref' at the command line.

    As a consequence of THAT, we know that $prolls[$i] is an arrayref,
    which is convenient, because if you look back up to the top of this
    message, I showed that we are assigning an arrayref back to
    $prolls[$i].

    Does that make sense?

    -=Eric
    --
    Come to think of it, there are already a million monkeys on a million
    typewriters, and Usenet is NOTHING like Shakespeare.
    -- Blair Houghton.
     
    Eric Schwartz, Oct 14, 2004
    #5
  6. Chris Weisiger

    Shawn Corey Guest

    A. Sinan Unur wrote:
    > No need to venture out to the whole world wide web. You can access the Perl
    > documentation installed on your computer using this command from the shell
    > you are using.
    >
    > To see the available documentation, type
    >
    > perldoc perltoc
    >
    > on the command line.
    >
    > Sinan.


    The problem with perldoc is its lack of adequate searching. perldoc -q
    keyword_guess only searches the FAQs. http://www.perldoc.com/ is more
    extensive but like all searches, you have to guess the magic password to
    open the Cave of Riches.

    --- Shawn
     
    Shawn Corey, Oct 14, 2004
    #6
  7. Chris Weisiger

    Anno Siegel Guest

    Shawn Corey <> wrote in comp.lang.perl.misc:
    > A. Sinan Unur wrote:
    > > No need to venture out to the whole world wide web. You can access the Perl
    > > documentation installed on your computer using this command from the shell
    > > you are using.
    > >
    > > To see the available documentation, type
    > >
    > > perldoc perltoc
    > >
    > > on the command line.
    > >
    > > Sinan.

    >
    > The problem with perldoc is its lack of adequate searching. perldoc -q
    > keyword_guess only searches the FAQs. http://www.perldoc.com/ is more
    > extensive but like all searches, you have to guess the magic password to
    > open the Cave of Riches.


    Direct grep is primitive, but efficient. Go to where the pods are:

    eval set `perl -V:privlib`
    cd $privlib/pod # or $privlib/pods, the name has changed recently

    grep -li 'frobnitzer'

    That will list all Perl documents that mention "frobnitzer". Vary
    to taste. Modify appropriately for non-Unix systems.

    Anno
     
    Anno Siegel, Oct 14, 2004
    #7
  8. Shawn Corey <> wrote:


    > The problem with perldoc is its lack of adequate searching.



    Use something else for searching then.

    Any OS worth its salt can easily search in the *.pod files for you.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 14, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JustSomeGuy

    Sorting lists of lists...

    JustSomeGuy, Jun 17, 2004, in forum: C++
    Replies:
    0
    Views:
    326
    JustSomeGuy
    Jun 17, 2004
  2. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    410
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  3. Replies:
    2
    Views:
    1,441
    James Kanze
    Jul 6, 2010
  4. Jason
    Replies:
    0
    Views:
    390
    Jason
    Oct 4, 2006
  5. Tom Kirchner

    sorting by multiple criterias (sub-sorting)

    Tom Kirchner, Oct 11, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    476
    Michael Budash
    Oct 11, 2003
Loading...

Share This Page