Re: where to define this array?
- From: anno4000@xxxxxxxxxxxxxxxxxxxxxxx (Anno Siegel)
- Date: 15 May 2005 23:31:29 GMT
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
.
- References:
- where to define this array?
- From: jbl
- where to define this array?
- Prev by Date: Re: Need help with perl script using threads.
- Next by Date: Re: Range function
- Previous by thread: Re: where to define this array?
- Next by thread: FAQ 4.13 How do I find the current century or millennium?
- Index(es):
Relevant Pages
|