[don't quote .signatures]
Quoth "" <>:
> On Oct 9, 6:08*pm, Ben Morrow <b...@morrow.me.uk> wrote:
> > Quoth "friend...@gmail.com" <hirenshah...@gmail.com>:
> >
> > > I have a large file in following format:
> >
> > > ID | Time | IP | Code
> >
> > > I want only data lines which has unique IP+Code.
> >
> > > If IP+Code is repeated then I don't want line.
> >
> > perldoc -q unique
>
> Below is code which I have written to extract unique IP+Code from
> large file. (File format is ID | Time | IP | code).
>
> I am not sure which will be best way to do this.
>
> #!/usr/local/bin/perl
Where is
use warnings;
use strict;
? You have already been told to include this.
> print "Welcome\n";
>
> $pri_file = "out_pri.txt";
>
> $cnt = 0;
> $flag = 0;
>
> open(INFO_PRI,$pri_file)or die $!;
> open(INFO,$pri_file)or die $!;
You have already been told to use lexical filehandles and 3-arg open.
You should make the error message actually useful:
open (my $INFO_PRI, "<", $pri_file)
or die "can't open '$pri_file': $!";
Why are you opening the same file twice? Just iterate over @pri_lines_
instead.
> @pri_lines_ = <INFO>;
Why on earth are you using a variable name ending in _?
> while($pri_line = <INFO_PRI>)
> {
> @primary = split('\|',$pri_line);
> $pri_cli_ip = $primary[4];
> $pri_id = $primary[7];
> print "$pri_id\n";
>
>
> foreach $p_line (@pri_lines_)
> {
> @pri = split('\|',$p_line);
You keep doing the same split over and over. Split the line first, and
keep the results in a datastructure till you need them.
> $cli_ip = $pri[4];
> $id = $pri[7];
>
> if(($pri_cli_ip == $cli_ip) && ($pri__id == $id))
Did you read perldoc -q unique? It says to use a hash for finding
uniqueness.
> {
> $cnt++;
You are not resetting $cnt between iterations of the outer loop, so
every other line will be considered duplicate.
> if($cnt == 2){
> $cnt = 0;
> $flag = 1;
> last;
If you give the outer loop a label, you can use next LABEL and avoid
$flag.
> }
> }
> }
> if($flag == 0){
> open(FILE,'>>pri_unique.txt');
> print FILE "$pri_line\n";
> close(FILE);
Why do you keep opening and closing this file?
Ben
--
Outside of a dog, a book is a man's best friend.
Inside of a dog, it's too dark to read.
Groucho Marx