Perl Sort Routine

Discussion in 'Perl Misc' started by Arthur, Feb 12, 2004.

  1. Arthur

    Arthur Guest

    Hi All,

    I pulled this sort routine off from one of the postings. It works
    well, but I was hoping someone could actually explain to me exactly
    what it is doing:

    @in = qw(one two two four seven four five one six);
    @out = sort grep(!$saw{$_}++, @in);
    print "@out";

    I'm lost with this line: @out = sort grep(!$saw{$_}++, @in);

    What is !$saw($_)++ ???

    Thanks,

    Arthur
    Arthur, Feb 12, 2004
    #1
    1. Advertising

  2. Arthur

    Tony Curtis Guest

    >> On 12 Feb 2004 08:45:25 -0800,
    >> (Arthur) said:


    > I'm lost with this line: @out = sort grep(!$saw{$_}++, @in);


    "perldoc -q duplicate"

    hth
    t
    Tony Curtis, Feb 12, 2004
    #2
    1. Advertising

  3. Arthur

    Paul Lalli Guest

    On Thu, 12 Feb 2004, Arthur wrote:

    > Hi All,
    >
    > I pulled this sort routine off from one of the postings. It works
    > well, but I was hoping someone could actually explain to me exactly
    > what it is doing:
    >
    > @in = qw(one two two four seven four five one six);
    > @out = sort grep(!$saw{$_}++, @in);
    > print "@out";
    >
    > I'm lost with this line: @out = sort grep(!$saw{$_}++, @in);
    >
    > What is !$saw($_)++ ???
    >
    > Thanks,
    >
    > Arthur
    >


    The line is sorting and returning elements of @in for which !$saw{$_}++ is
    true. The grep goes through each element of @in, assigning each one to $_.
    The first time each individual element is encountered, it is inserted as
    the key to a hash %saw. It is inserted by taking advantage of
    autovivification. That is, $saw{'one'}++ both creates the key 'one' in
    the hash %saw, and immediately increments the value of that key. Since
    'one' was previously not in the hash, the value was originally undef
    (which gets treated as 0 by the ++ operator), and gets inremented to 1.
    However, because the postfix form of ++ is being used, the expression
    returns the value before incrementation - which is undef. The ! then
    swaps that boolean, to make the entire !$saw{$_}++ expression return true.

    The second and subsequent times the same value is seen, the position in
    the hash already exists with a value of (at least) 1, and so the
    expression returns a false value.

    Therefore, the whole line is returning a sorted list of unique values from
    the array @in.

    Paul Lalli
    Paul Lalli, Feb 12, 2004
    #3
    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. Kamilche

    CODE: Generic Sort Routine

    Kamilche, Aug 19, 2004, in forum: Python
    Replies:
    1
    Views:
    289
    Andrew Henshaw
    Aug 23, 2004
  2. rkk
    Replies:
    9
    Views:
    804
    CBFalconer
    Sep 24, 2006
  3. /usr/ceo
    Replies:
    32
    Views:
    1,894
    Mark Space
    Sep 15, 2008
  4. Tom Carter

    Sort Routine Problem

    Tom Carter, Jan 19, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    141
    Tom Carter
    Jan 20, 2004
  5. Navin
    Replies:
    1
    Views:
    674
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page