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

RE: [MacPerl] perl to c/c+/c



At 20.39 97/6/11, Tim Endres wrote:
>Your manager is right, if the code is ever to be ported.
>
>Here is what I would do in perl:
>
>%list = ();
>
>open( IN, "< list.txt" ) || die...;
>while ( <IN> )
>        {
>        chop;
>        $list{$_} = 1;
>        }
>close( IN );
>
>open (in2, ">file.lst");
>oreach $id ( sort keys %list )
>        {
>        print ( in2 $id, "\n" );
>        }
>close (in2);
>
>Any duplicates will result in setting the same hash value, which
>performs the "uniq" part of your unix command, and the "sort keys"
>gives you the sorted result of the uniq-ified values.

Only because the file is so large and hashes take up more resources, I
would recommend something similar to the following:

#!perl
open( IN, "< list.txt" ) || die...;
my @list = <IN>;
close( IN );

my($id,$lid);
open (in2, ">file.lst");
foreach $id ( sort @list )
        {
        print ( in2 $id ) unless ($id eq $lid);
        $lid = $id;
        }
close (in2);
__END__

Using an array takes up less resources than a hash.  And since it is being
sorted, if there are duplicate lines it will always be equivalent to the
line directly preceding it, so you can simply look to see if they match
before you print.

But this may take longer to run, as you are doing that extra evaluation
every time through.  So it's a take-yer-pick.  If you have enough memory to
handle the hash, that would probably be faster (I think).

--
Chris Nandor                 pudge@pobox.com                 http://pudge.net/
%PGPKey=('B76E72AD',[1024,'08 24 09 0B CE 73 CA 10  1F F7 7F 13 81 80 B6 B6'])



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