in reply to Re: Re: slurping into a substring'ed' array
in thread slurping into a substring'ed' array

dear all respondees,

thanks for your fast responses, i was in the proces of testing it myself, and had got as far as:

my %seen; my @sorted = sort($a <=> $b) grep{ $seen{$_}++ } map {substr($_,0,1)} +@original_array;

when i realised y'all were on the board already!!

I just wanted to point out that my code above printed out this list:

A,A,B,B,C

So why did it only print out the extra As and Bs? Out of curiosity.

Thanks again for your help.

Sam

Replies are listed 'Best First'.
Re: Re: Re: Re: slurping into a substring'ed' array
by dragonchild (Archbishop) on Apr 26, 2004 at 15:31 UTC
    grep checks against %seen and, the first time, sees that it's zero (false), so doesn't pass it through. The second time, it's non-zero (true), so it passes that value through. The third and subsequent times, it's still true. Your grep would have dropped all unique values. (If you had a D123, for instance, it would have been missed.)

    If you absolutely have to have it in one line, provide a function called unique() and do something like:

    sub unique { my %x; @x{@_} = @_; values %x } my @sorted = sort { $a cmp $b } unique map { substr($_, 0, 1 ) } @original;

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose