FAQ
I'm also very new to Perl and wrote a long and newbyish script
that
does exactly what the Unix command "sort FILENAME | uniq"
does just to
see how it can be done.
How long? Because you can do that on one line in perl. :-)
Hi John (we're not worthy - Wayne's World) ;-)

I've just come back to your post above (30/05/05) and I've tried to
get the internal workings of the code to agree with my brain. Please
help!
perl -e'print sort grep !$seen{$_}++, <>' FILENAME
Initially I thought you were creating a hashref(?) key with every line
of input, if unique. But I cannot access the keys using the hashref
(below) so there is something else at work here.

for $key_v (keys %{$seen}) {
print "$key_v\n";
}

This returns nothing when used with the above code in a script (code
posted at request). Sorry in advance if I've made newbie mistake but
please explain how !$seen{$_}++ knows what was used before.

PS Will the Perl Cookbook have examples to common tools such as this?
What does everyone think of the book - very helpful?

TIA



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This e-mail and its contents are subject to the Telkom SA Limited
e-mail legal notice available at
http://www.telkom.co.za/TelkomEMailLegalNotice.PDF
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Search Discussions

  • Chris Devers at Jun 13, 2005 at 12:05 pm

    On Mon, 13 Jun 2005, Tielman Koekemoer (TNE) wrote:

    PS Will the Perl Cookbook have examples to common tools such as this?
    Yes. Oodles of them.
    What does everyone think of the book - very helpful?
    Extremely.



    --
    Chris Devers
  • Thomas Bätzler at Jun 13, 2005 at 12:27 pm

    Tielman Koekemoer (TNE) <KoekemTN@telkom.co.za> asked:
    I've just come back to your post above (30/05/05) and I've
    tried to get the internal workings of the code to agree with
    my brain. Please help!
    perl -e'print sort grep !$seen{$_}++, <>' FILENAME
    Initially I thought you were creating a hashref(?) key with
    every line of input, if unique. But I cannot access the keys
    using the hashref
    (below) so there is something else at work here.

    for $key_v (keys %{$seen}) {
    print "$key_v\n";
    }

    This returns nothing when used with the above code in a
    script (code posted at request). Sorry in advance if I've
    made newbie mistake but please explain how !$seen{$_}++ knows
    what was used before.
    He's using just a hash, namely %seen. What he's doing is
    reading all the files on the command line (or standard input
    if there are no filename arguments) in list context, i.e.
    he turns them into a large list of lines. This list is then
    "filtered" by the grep command. This will return a subset of
    the argument list - namely those items for which the grep
    expression is true. That expression is "!$seen{$_}++".
    $_ will be on of the lines. The first time a unique line
    is evaluated in this expression, $seen{$_} does not exist
    and the value is undefined. The ! operator forces this into
    a boolean false and returns boolean true, i.e. the line will
    be part of the result set. the post-increment ++ operator
    will then create a hash key with the line as key and assign
    it (undef)+1 = 1 as a value. So when the line is encountered
    again, it will evaluate to a positive number, which will be
    negated to false by the ! operator, which in turn means that
    the line will not be put into the result set twice.
    PS Will the Perl Cookbook have examples to common tools such as this?
    What does everyone think of the book - very helpful?
    It has lots of useful code snippets, and they are commented
    well enough to follow with a basic grasp of Perl. I would
    not want to miss it in my library. It also used to be part
    of a Book/CD set called the Perl Bookshelf. AFAIK it's out
    of print but you might be able to pick it up at a used book
    store (or onlinle at ABEbooks) cheaply.

    Alternatively, if you want to learn about Perl style, check
    out Joseph N. Hall's Effective Perl Programming.

    HTH,
    Thomas
  • Chris Devers at Jun 13, 2005 at 1:34 pm

    On Mon, 13 Jun 2005, Thomas Bätzler wrote:

    It also used to be part of a Book/CD set called the Perl Bookshelf.
    AFAIK [_Perl Cookbook_ is] out of print but you might be able to pick
    it up at a used book store (or onlinle at ABEbooks) cheaply.
    The first edition went out of print, but the second hasn't:

    <http://www.oreilly.com/catalog/perlckbk2/>

    And AFAIK it has been part of each edition of the Perl CD Bookshelf,
    including the current 4th edition:

    <http://www.oreilly.com/catalog/perlcdbs4/>
    Alternatively, if you want to learn about Perl style, check
    out Joseph N. Hall's Effective Perl Programming.
    Or the forthcoming, more up-to-date _Perl Best Practices_:

    <http://www.oreilly.com/catalog/perlbp/>

    Though in this case you'll have to wait until July or August for it to
    be released, but I have no doubt that it'll be worth the wait.


    --
    Chris Devers
  • John W. Krahn at Jun 13, 2005 at 6:14 pm

    Chris Devers wrote:

    Or the forthcoming, more up-to-date _Perl Best Practices_:

    <http://www.oreilly.com/catalog/perlbp/>

    Though in this case you'll have to wait until July or August for it to
    be released, but I have no doubt that it'll be worth the wait.
    Yes, anything by Dr. Conway will be interesting to say the least! :-)


    John
    --
    use Perl;
    program
    fulfillment
  • John W. Krahn at Jun 13, 2005 at 7:04 pm

    Tielman Koekemoer (TNE) wrote:
    I'm also very new to Perl and wrote a long and newbyish script
    that
    does exactly what the Unix command "sort FILENAME | uniq"
    does just to
    see how it can be done.
    How long? Because you can do that on one line in perl. :-)
    Hi John (we're not worthy - Wayne's World) ;-)
    I am just standing on the shoulders of giants. :-)
    I've just come back to your post above (30/05/05) and I've tried to
    get the internal workings of the code to agree with my brain. Please
    help!
    perl -e'print sort grep !$seen{$_}++, <>' FILENAME
    Initially I thought you were creating a hashref(?) key with every line
    of input, if unique. But I cannot access the keys using the hashref
    (below) so there is something else at work here.
    The expression !$seen{$_}++ uses the hash %seen, if it was using a hashref it
    would be written as !$seen->{$_}++
    for $key_v (keys %{$seen}) {
    print "$key_v\n";
    }

    This returns nothing when used with the above code in a script (code
    posted at request). Sorry in advance if I've made newbie mistake but
    please explain how !$seen{$_}++ knows what was used before.
    Thomas wrote a pretty good explanation. If you have any more questions just ask.
    PS Will the Perl Cookbook have examples to common tools such as this?
    Yes, it is described in section 4.7 "Extracting Unique Elements from a List"
    (this is in the second edition.)
    What does everyone think of the book - very helpful?
    Yes it is a good book. A lot of the stuff in the book can also be found in
    the FAQ files that are included with Perl.

    perldoc -q "How can I remove duplicate elements from a list or array"

    perldoc perlfaq



    John
    --
    use Perl;
    program
    fulfillment

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedJun 13, '05 at 11:53a
activeJun 13, '05 at 7:04p
posts6
users4
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase