From: Jeni Zundel <[EMAIL PROTECTED]>
> I do most of my studying/ prototyping on a mac. I admit it. I love my mac.
> So, I wrote a little script on my mac, as follows:
>
>
> #!/usr/bin/perl
> # dedupe.pl
>
> use strict;
> use warnings;
>
> my (@words, %schtuff, $word); # set up necessary variables.
1) strip unnecessary comments
2) don't declare variables upfront. Declare them on the first use and
only for the smallest possible scope.
> open FH, "c:/data/filename.txt" or die $!; # open a filehandle for the
> file to be read
In this case it's not important, but generally it's better to use
three parameter open() and lexical filehandles. Also the $^E may have
more details than $!.
my $filename = "c:/data/filename.txt"
open my $FH, '<', $filename
or die qq{Failed to open "$filename" : $^E\n};
> @words = <FH>; # put each row of
> the input file into the array, @words
> foreach $word(@words) { # open a loop to iterate
> through each line in the array
It's better to read and process the lines one at a time instead of
slurping the whole file into memory:
while (my $word = <$FH> {
> $schtuff{$word} +=1; # for each line in the
> array, make a key in the hash & increment a counter as the value.
if you mean increment, use increment
$schtuff{$word}++;
> }
close $FH;
> open OUTFILE, ">>c:/data/filename_dedup1.txt" or die $!;
> print OUTFILE keys %schtuff;
>
>
> On the mac, I tested it and found it worked successfully. So, I brought it
> to work and
> put it on my windoze box. Running active perl, it runs, but does
> not output any rows. Does anyone have any thoughts or suggestions?
> Also, any comments on how I could have coded this better or
> differently are always welcomed.
>
> Jen
None of the changes I suggested could have been the problem though.
Are you sure the c:/data/filename_dedup1.txt ends up empty? Could you
try to add some debug prints into the loop?
Jenda
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/