Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: At each change in...

by Abigail-II (Bishop)
on Jun 13, 2002 at 09:55 UTC ( [id://174134]=note: print w/replies, xml ) Need Help??


in reply to At each change in...

Well, if you are on a Unix system, make use of its toolkit. One of the tools is called sort and usually has options to "merge, do not sort" and "uniques only". On my system, you these options are enable with -mu, but check your manual page. Then, instead of opening your file with:
open my $fh => "file" or die $!;
open it with:
open my $fh => "sort -mu file |" or die $!;

No counter needed, no check, no keeping track of the previous. Ultimate code reuse.

Abigail

Replies are listed 'Best First'.
Re: Re: At each change in...
by stefp (Vicar) on Jun 13, 2002 at 11:41 UTC
    For me, the whole point of perl is to be able to forget this Unix stuff by getting inside one hood. Perl is the (almost) the same on every platform. And the effort to learn perl is offset by being able to forget this myrad of Unix commands with myriad of options that are supported or not on a given platform. Also there is often overhead avoided by perl. In the case at hand, we have to fork and pipe (no big deal) and sort (this last can cost if the processed file is very big)

    But again, TMTOWTDI.

    -- stefp -- check out TeXmacs wiki

      Is having to learn a myriad of Unix command worse than having to learn a myriad of APIs of a myriad of Perl modules? Or do you plan to do everything yourself? I rather reuse what others have done....

      Note also that IEEE Std 1003.1 - 2001 (aka POSIX) "Shell and Utilities" require the -m and -u options.

      As for the overhead of sort, in the given example, no sort is actually done. The -m option, for merge, merges files which are already presumed to be sorted - but when given one file, this passes things unsorted.

      Abigail

        It so easy to do some things in Perl that very often, I don't even think of using external commands or exotic modules so my code is independant of anything but a vanilla Perl installation. I learn Perl in 1989 for that very reason: to forget about sed, cut, sort (...) while getting the benefit to already know since perl was conceptually reaped from them.

        you don't need a sort because you assume your file(s) sorted; what a specious argument!!

        -- stefp -- check out TeXmacs wiki

Re: Re: At each change in...
by ariels (Curate) on Jun 13, 2002 at 12:26 UTC
    Why not just use the program uniq? It would appear to perform the same function as you desire, and is perhaps more recognizable when reading.
      uniq works as well; my point was more about using the toolkit available then sort vs uniq.

      Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://174134]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-19 12:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found