[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Porting a Perl Program



At 13.09 1/8/98, Developers wrote:
>I have written a very small program in PERL to read in HTML files from a
>remote source and then count the 5 most frequent words.  This works fine in
>UNIX, but I am a bit confused about how to get the same functionality on
>the Mac.  The part of the program which is giving me hassle is the '|sort'
>on the (open SESAME) line which obviously is not possible on a mac (no
>command line ;-).  The program is included below.

Lucky you:  I just released v.0.11 of File::Sort just for occasions such as
this (on CPAN).

Although, you obviously don't need to do that; since you have the array in
memory anyway, there is no reason to send it to sort(1) to do the job.
Sort it with Perl.

So instead of:

    open(SESAME, "|sort +1rn >keywords.out");
    while ( my($word, $count) = each %seen )
    {
        print SESAME "$word\t\t\t\t$count\n";
    }
    close(SESAME);


    open(TOPTEN, "keywords.out") || die "Can't open file: $!\n";
    @temparray = <TOPTEN>;
    close(TOPTEN);

Maybe something like:

    @temparray = map "$_\t\t\t\t$seen{$_}\n",
                 sort {$seen{$b} <=> $seen{$a}}
                 keys %seen;

I believe that will give you the same result you had.  And it is much nicer
code than piping to sort(1) anyway, even if you are on a Unix box.

Map-boy signing off,

--
Chris Nandor               pudge@pobox.com           http://pudge.net/
%PGPKey=('B76E72AD',[1024,'0824 090B CE73 CA10  1FF7 7F13 8180 B6B6'])
#==                    MacPerl: Power and Ease                     ==#
#==    Publishing Date: Early 1998. http://www.ptf.com/macperl/    ==#



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch