Re: where to define this array?



jbl <jblno@xxxxxxxxxxxxxxx> wrote in comp.lang.perl.misc:
> I am using Win32 so the shebang is just for looks.
>
> Where is the correct place to define @line_data in the example below?

There is none. Don't define it. You use it to store the file in memory,
but that isn't necessary.

> #!/user/bin/perl
> use warnings;
> use strict;
>
> #remove duplicate (elements) lines from file
>
> open(IN,'C:\mydir\myfile.txt')
> || die "Cannot open C:\\mydir\\myfile\.txt: $!";
> open(OUT,">C:\\newsbin_dot_com\\mylog\.txt")
> || die "Cannot open to write C:\\newsbin_dot_com\\mylog\.txt: $!";
> while (<IN>) {
>
> push (@line_data,$_); # <=HERE
> }
> close(IN);

You could get them into @line_data easier, but you don't neet that at all.

> my (%saw,undef); # from perlfaq4.htm
>
> my @duplicates_removed = grep(!$saw{$_}++, @line_data); #<=HERE
>
> print OUT @duplicates_removed;
> close(OUT);
> __END__

Reading from DATA and printing to STDOUT for simplicity, the whole
thing becomes

my %saw;
$saw{ $_} ++ or print while <DATA>;

printing each unique line as it arrives.

Anno
.



Relevant Pages

  • Re: Printing: Is there any diff between Duane Reade, CVS and Walgreens?
    ... I've never used "drug store" printing. ... A Walgreens just opened up here a few weeks ago. ... It's not whether it's Duane Reade, CVS or Walgreens -- or Quik-Print -- or whatever. ...
    (rec.photo.digital)
  • Re: epson r200
    ... Just do a nozzle check, ... that all nozzles are printing, and then turn off the printer. ... >> Buy a set of cleaning carts and do a couple of cleans then run a purge ... run a nozzel check and store the printer with the ...
    (comp.periphs.printers)
  • Re: Printing Problems
    ... You will have to store ... and then access it later from the delegate that handles ... the actual printing (or before you start printing, ... > using the PRINTDLG structure with the standard print dialog. ...
    (microsoft.public.dotnet.languages.csharp)
  • Newbie here... getting a count of repeated instances in a list.
    ... I started trying to learn python today. ... will open a text file containing email addresses and store them in a list. ... For now I just have it printing to the screen. ... something with hash object to make this go a whole lot faster. ...
    (comp.lang.python)