[30668] in Perl-Users-Digest
Perl-Users Digest, Issue: 1913 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Oct 10 14:09:54 2008
Date: Fri, 10 Oct 2008 11:09:22 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Fri, 10 Oct 2008 Volume: 11 Number: 1913
Today's topics:
"\!" appears on new line <xiaoxia2005a@yahoo.com>
Re: "\!" appears on new line <fawaka@gmail.com>
Re: "\!" appears on new line <xiaoxia2005a@yahoo.com>
Re: "\!" appears on new line <peter@makholm.net>
Re: "\!" appears on new line <xiaoxia2005a@yahoo.com>
Re: "\!" appears on new line <peter@makholm.net>
Re: "\!" appears on new line <xiaoxia2005a@yahoo.com>
Re: "\!" appears on new line <sbryce@scottbryce.com>
Re: "\!" appears on new line <xiaoxia2005a@yahoo.com>
Re: "\!" appears on new line <xiaoxia2005a@yahoo.com>
Re: "\!" appears on new line <xiaoxia2005a@yahoo.com>
Re: "\!" appears on new line <tadmc@seesig.invalid>
Re: data file <hirenshah.05@gmail.com>
Re: data file <jurgenex@hotmail.com>
Re: data file <tadmc@seesig.invalid>
Re: data file <ben@morrow.me.uk>
Re: data file <glex_no-spam@qwest-spam-no.invalid>
Re: data file <jurgenex@hotmail.com>
Re: data file <hirenshah.05@gmail.com>
Re: data file <glex_no-spam@qwest-spam-no.invalid>
Re: data file <jurgenex@hotmail.com>
Re: data file <hirenshah.05@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 10 Oct 2008 06:07:31 -0700 (PDT)
From: April <xiaoxia2005a@yahoo.com>
Subject: "\!" appears on new line
Message-Id: <40f51f2c-55e9-40e4-a0b7-53c3312686e0@s9g2000prm.googlegroups.com>
print $wholething, $wholething2, "\!", "\n";
Thank you
!
How can I get them on the same line?
------------------------------
Date: 10 Oct 2008 13:12:40 GMT
From: Leon Timmermans <fawaka@gmail.com>
Subject: Re: "\!" appears on new line
Message-Id: <48ef54c8$0$199$e4fe514c@news.xs4all.nl>
On Fri, 10 Oct 2008 06:07:31 -0700, April wrote:
> print $wholething, $wholething2, "\!", "\n";
>
> Thank you
> !
>
> How can I get them on the same line?
It prints on one line for me. Though I do wonder very much why you are
escaping the exclamation point.
Leon Timmermans
------------------------------
Date: Fri, 10 Oct 2008 06:19:53 -0700 (PDT)
From: April <xiaoxia2005a@yahoo.com>
Subject: Re: "\!" appears on new line
Message-Id: <e5319028-c70a-4107-ae7d-1554b559a80b@l42g2000hsc.googlegroups.com>
I tried sevral ways ... then it could be a bug? I'm using v5.8.8
built for MSWin32-x86-multi-thread (ActivePerl).
Just doing some exercises, thanks for your prompt response Leon!
On Oct 10, 9:12=A0am, Leon Timmermans <faw...@gmail.com> wrote:
> On Fri, 10 Oct 2008 06:07:31 -0700, April wrote:
> > print $wholething, $wholething2, "\!", "\n";
>
> > Thank you
> > !
>
> > How can I get them on the same line?
>
> It prints on one line for me. Though I do wonder very much why you are
> escaping the exclamation point.
>
> Leon Timmermans
------------------------------
Date: Fri, 10 Oct 2008 15:22:56 +0200
From: Peter Makholm <peter@makholm.net>
Subject: Re: "\!" appears on new line
Message-Id: <871vyovb2n.fsf@hacking.dk>
April <xiaoxia2005a@yahoo.com> writes:
> print $wholething, $wholething2, "\!", "\n";
>
> Thank you
> !
>
> How can I get them on the same line?
My guess is that $wholething2 have a newline at the end. Remove that
and it should work as you want it to.
//Makholm
------------------------------
Date: Fri, 10 Oct 2008 06:36:17 -0700 (PDT)
From: April <xiaoxia2005a@yahoo.com>
Subject: Re: "\!" appears on new line
Message-Id: <eb49a5bf-a15e-424b-a980-25ea94255a04@l76g2000hse.googlegroups.com>
here is a variation of the whole script, but does the same:
print "Enter a phrase and convert the first letter to upper case and
add a ! at end: ";
my $input =3D <STDIN>;
my $firststinput =3D uc( substr($input, 0, 1) );
print $firststinput, substr($input, 1), "\!", "\n";
does substr change to a new line?
On Oct 10, 9:22=A0am, Peter Makholm <pe...@makholm.net> wrote:
> April <xiaoxia20...@yahoo.com> writes:
> > print $wholething, $wholething2, "\!", "\n";
>
> > Thank you
> > !
>
> > How can I get them on the same line?
>
> My guess is that $wholething2 have a newline at the end. Remove that
> and it should work as you want it to.
>
> //Makholm
------------------------------
Date: Fri, 10 Oct 2008 15:42:16 +0200
From: Peter Makholm <peter@makholm.net>
Subject: Re: "\!" appears on new line
Message-Id: <87wsggtvlz.fsf@hacking.dk>
April <xiaoxia2005a@yahoo.com> writes:
> print "Enter a phrase and convert the first letter to upper case and
> add a ! at end: ";
> my $input = <STDIN>;
Here you string will have an newline at the end. You'll have to chomp it.
> my $firststinput = uc( substr($input, 0, 1) );
> print $firststinput, substr($input, 1), "\!", "\n";
Uhmmm, and use ucfirst if that is what you need to do.
//Makholm
------------------------------
Date: Fri, 10 Oct 2008 06:53:22 -0700 (PDT)
From: April <xiaoxia2005a@yahoo.com>
Subject: Re: "\!" appears on new line
Message-Id: <091cf760-4a07-4b4e-8a11-26aec934b937@k13g2000hse.googlegroups.com>
thanks, however after the recommended change the script hangs
forllowing keyboard entry:
#! c:\perl
print "Enter a phrase and convert the first letter to upper case and
add a ! at end: ";
my $input =3D <STDIN>;
chomp( $input =3D <STDIN> );
my $firststinput =3D ucfirst($input);
print $firststinput, substr($input, 1), "\!", "\n";
C:\Perl\Exercise1>perl new3-6d.pl
Enter a phrase and convert the first letter to upper case and add a !
at end: thank you
On Oct 10, 9:42=A0am, Peter Makholm <pe...@makholm.net> wrote:
> April <xiaoxia20...@yahoo.com> writes:
> > print "Enter a phrase and convert the first letter to upper case and
> > add a ! at end: ";
> > my $input =3D <STDIN>;
>
> Here you string will have an newline at the end. You'll have to chomp it.
>
> > my $firststinput =3D uc( substr($input, 0, 1) );
> > print $firststinput, substr($input, 1), "\!", "\n";
>
> Uhmmm, and use ucfirst if that is what you need to do.
>
> //Makholm
------------------------------
Date: Fri, 10 Oct 2008 08:10:34 -0600
From: Scott Bryce <sbryce@scottbryce.com>
Subject: Re: "\!" appears on new line
Message-Id: <qbmdndjJD4TC_3LVnZ2dnUVZ_u-dnZ2d@comcast.com>
April wrote:
> thanks, however after the recommended change the script hangs
> following keyboard entry:
That is because you are asking for another keyboard input.
> #! c:\perl
> print "Enter a phrase and convert the first letter to upper case and
> add a ! at end: ";
> my $input = <STDIN>;
> chomp( $input = <STDIN> );
chomp $input;
------------------------------
Date: Fri, 10 Oct 2008 07:13:22 -0700 (PDT)
From: April <xiaoxia2005a@yahoo.com>
Subject: Re: "\!" appears on new line
Message-Id: <12f71f20-cdd1-426b-a459-cdbc71f333fb@w39g2000prb.googlegroups.com>
this one finally worked! thanks you two!
#! c:\perl
my $input;
print "Enter a phrase and convert the first letter to upper case and
add a ! at end: ";
chomp( $input =3D <STDIN> );
my $firststinput =3D ucfirst( $input );
print $firststinput, "\!", "\n";
On Oct 10, 9:53=A0am, April <xiaoxia20...@yahoo.com> wrote:
> thanks, however after the recommended change the script hangs
> forllowing keyboard entry:
>
> #! c:\perl
> print "Enter a phrase and convert the first letter to upper case and
> add a ! at end: ";
> my $input =3D <STDIN>;
> chomp( $input =3D <STDIN> );
> my $firststinput =3D ucfirst($input);
> print $firststinput, substr($input, 1), "\!", "\n";
>
> C:\Perl\Exercise1>perl new3-6d.pl
> Enter a phrase and convert the first letter to upper case and add a !
> at end: thank you
>
> On Oct 10, 9:42=A0am, Peter Makholm <pe...@makholm.net> wrote:
>
>
>
> > April <xiaoxia20...@yahoo.com> writes:
> > > print "Enter a phrase and convert the first letter to upper case and
> > > add a ! at end: ";
> > > my $input =3D <STDIN>;
>
> > Here you string will have an newline at the end. You'll have to chomp i=
t.
>
> > > my $firststinput =3D uc( substr($input, 0, 1) );
> > > print $firststinput, substr($input, 1), "\!", "\n";
>
> > Uhmmm, and use ucfirst if that is what you need to do.
>
> > //Makholm- Hide quoted text -
>
> - Show quoted text -
------------------------------
Date: Fri, 10 Oct 2008 07:24:34 -0700 (PDT)
From: April <xiaoxia2005a@yahoo.com>
Subject: Re: "\!" appears on new line
Message-Id: <e746bc04-0d28-485b-9cd5-4485fdf97154@c60g2000hsf.googlegroups.com>
On Oct 10, 10:10=A0am, Scott Bryce <sbr...@scottbryce.com> wrote:
> April wrote:
> > thanks, however after the recommended change the script hangs
> > following keyboard entry:
>
> That is because you are asking for another keyboard input.
>
> > #! c:\perl
> > print "Enter a phrase and convert the first letter to upper case and
> > add a ! at end: ";
> > my $input =3D <STDIN>;
> > chomp( $input =3D <STDIN> );
>
> chomp $input;
you are correct Scott, input & chomp, thanks!
------------------------------
Date: Fri, 10 Oct 2008 07:51:20 -0700 (PDT)
From: April <xiaoxia2005a@yahoo.com>
Subject: Re: "\!" appears on new line
Message-Id: <9b40d0bf-c2d7-4648-b991-8c77e710b3de@t39g2000prh.googlegroups.com>
this is more concise:
print "Enter a phrase and convert the first letter to upper case and
add a ! at end: ";
chomp( my $input =3D <STDIN> );
print ucfirst( $input ), "\!", "\n";
On Oct 10, 10:13=A0am, April <xiaoxia20...@yahoo.com> wrote:
> this one finally worked! =A0thanks you two!
>
> #! c:\perl
> my $input;
> print "Enter a phrase and convert the first letter to upper case and
> add a ! at end: ";
> chomp( $input =3D <STDIN> );
> my $firststinput =3D ucfirst( $input );
> print $firststinput, "\!", "\n";
>
> On Oct 10, 9:53=A0am, April <xiaoxia20...@yahoo.com> wrote:
>
>
>
> > thanks, however after the recommended change the script hangs
> > forllowing keyboard entry:
>
> > #! c:\perl
> > print "Enter a phrase and convert the first letter to upper case and
> > add a ! at end: ";
> > my $input =3D <STDIN>;
> > chomp( $input =3D <STDIN> );
> > my $firststinput =3D ucfirst($input);
> > print $firststinput, substr($input, 1), "\!", "\n";
>
> > C:\Perl\Exercise1>perl new3-6d.pl
> > Enter a phrase and convert the first letter to upper case and add a !
> > at end: thank you
>
> > On Oct 10, 9:42=A0am, Peter Makholm <pe...@makholm.net> wrote:
>
> > > April <xiaoxia20...@yahoo.com> writes:
> > > > print "Enter a phrase and convert the first letter to upper case an=
d
> > > > add a ! at end: ";
> > > > my $input =3D <STDIN>;
>
> > > Here you string will have an newline at the end. You'll have to chomp=
it.
>
> > > > my $firststinput =3D uc( substr($input, 0, 1) );
> > > > print $firststinput, substr($input, 1), "\!", "\n";
>
> > > Uhmmm, and use ucfirst if that is what you need to do.
>
> > > //Makholm- Hide quoted text -
>
> > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -
------------------------------
Date: Fri, 10 Oct 2008 09:47:15 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: "\!" appears on new line
Message-Id: <slrngeuqnj.n6e.tadmc@tadmc30.sbcglobal.net>
April <xiaoxia2005a@yahoo.com> wrote:
> print $firststinput, "\!", "\n";
That is the same as
print "$firststinput!\n";
only easier to read and maintain.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Fri, 10 Oct 2008 08:28:17 -0700 (PDT)
From: "friend.05@gmail.com" <hirenshah.05@gmail.com>
Subject: Re: data file
Message-Id: <276f5e30-6ccf-4316-a9aa-79c071f46e73@v53g2000hsa.googlegroups.com>
On Oct 9, 6:08=A0pm, 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
>
> Ben
>
> --
> Musica Dei donum optimi, trahit homines, trahit deos. =A0 =A0|
> Musica truces mollit animos, tristesque mentes erigit. =A0 | =A0 b...@mor=
row.me.uk
> Musica vel ipsas arbores et horridas movet feras. =A0 =A0 =A0 =A0|
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
print "Welcome\n";
$pri_file =3D "out_pri.txt";
$cnt =3D 0;
$flag =3D 0;
open(INFO_PRI,$pri_file)or die $!;
open(INFO,$pri_file)or die $!;
@pri_lines_ =3D <INFO>;
while($pri_line =3D <INFO_PRI>)
{
@primary =3D split('\|',$pri_line);
$pri_cli_ip =3D $primary[4];
$pri_id =3D $primary[7];
print "$pri_id\n";
foreach $p_line (@pri_lines_)
{
@pri =3D split('\|',$p_line);
$cli_ip =3D $pri[4];
$id =3D $pri[7];
if(($pri_cli_ip =3D=3D $cli_ip) && ($pri__id =3D=3D $id))
{
$cnt++;
if($cnt =3D=3D 2){
$cnt =3D 0;
$flag =3D 1;
last;
}
}
}
if($flag =3D=3D 0){
open(FILE,'>>pri_unique.txt');
print FILE "$pri_line\n";
close(FILE);
}else{
$flag =3D 0;
}
}
close(INFO_PRI);
close(INFO);
------------------------------
Date: Fri, 10 Oct 2008 09:18:53 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: data file
Message-Id: <b1vue45ugo9jr31eg5licbrqlmj8u2mi2i@4ax.com>
"friend.05@gmail.com" <hirenshah.05@gmail.com> wrote:
>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.
>
>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
>$pri_file = "out_pri.txt";
>
>$cnt = 0;
>$flag = 0;
>
>open(INFO_PRI,$pri_file)or die $!;
>open(INFO,$pri_file)or die $!;
>
>@pri_lines_ = <INFO>;
>
>while($pri_line = <INFO_PRI>)
[rest of code snipped]
Many things I don't understand in this code, among them why you are
using 2 file handles to the same file, why you are slurping in the whole
file on one file handle and then process the file line by line on the
other file handle, why you have a nested loop, etc, etc.
Your requirements seem to be straight forward and easy to translate into
a simple algorithm (warning, sketch only, not tested):
my %idtable;
open ($F, '<', $myfile) of die "Cannot read $myfile because $!\n";
while (<$F>) { #loop through file and gather all IP | Code combinations
(undef, undef, $ip, $code) = split '\|';
$idtable{"$ip|$code"}++; #record this ip-code combination
}
seek $F, 0; #reset file to start
while (<$F>) { #loop through file again and ....
(undef, undef, $ip, $code) = split '\|';
print if $idtable{"$ip|$code"} == 1;
#... print that line if the ip-code combination
#exists exactly once in the file
close $F;
jue
------------------------------
Date: Fri, 10 Oct 2008 11:19:29 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: data file
Message-Id: <slrngev04h.o2t.tadmc@tadmc30.sbcglobal.net>
friend.05@gmail.com <hirenshah.05@gmail.com> wrote:
> $flag = 0;
You should choose meaningful variable names.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Fri, 10 Oct 2008 17:27:42 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: data file
Message-Id: <ucc4s5-dh3.ln1@osiris.mauzo.dyndns.org>
[don't quote .signatures]
Quoth "friend.05@gmail.com" <hirenshah.05@gmail.com>:
> 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.
ben@morrow.me.uk Groucho Marx
------------------------------
Date: Fri, 10 Oct 2008 11:46:58 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: data file
Message-Id: <48ef8702$0$89875$815e3792@news.qwest.net>
friend.05@gmail.com wrote:
> 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
>>
>> Ben
> 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.
Well, it's not the way you posted.
Did you actually read the perldoc Ben mentioned above? You don't use a
hash at all, so I'm guessing not.
>
> #!/usr/local/bin/perl
use strict;
open( my $INFO, '<', $pri_file ) or die "Can't open $pri_file: $!";
open( my $OUT, '>', 'unique.out' ) or die "Can't open unique.out: $!";
my %info;
while ( my $line = <$INFO> )
{
chomp( $line );
# split the data.. you can split directly into the variables..
# my ( $v1, $v2 ) = ( split( /\|/, $line ) )[1,2];
# print $line to $OUT if the hash key of $cli_ip and $id doesn't already
exist.
}
------------------------------
Date: Fri, 10 Oct 2008 09:57:42 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: data file
Message-Id: <e32ve49bni87d8qpv2s87rkptabcit5m11@4ax.com>
"J. Gleixner" <glex_no-spam@qwest-spam-no.invalid> wrote:
>friend.05@gmail.com wrote:
>> 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
>>>
>>> Ben
>
>> 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.
>
>Well, it's not the way you posted.
>
>Did you actually read the perldoc Ben mentioned above? You don't use a
>hash at all, so I'm guessing not.
ACK!
>while ( my $line = <$INFO> )
>{
> chomp( $line );
># split the data.. you can split directly into the variables..
># my ( $v1, $v2 ) = ( split( /\|/, $line ) )[1,2];
># print $line to $OUT if the hash key of $cli_ip and $id doesn't already
>exist.
That will print each IP+code exactly once. I think (but I may be
mistaken, the OPs isn't clear on that) he wants only those lines, that
_are_ unique wrt. the IP+code, i.e. where there is no second line with
the same IP+code.
jue
------------------------------
Date: Fri, 10 Oct 2008 10:30:09 -0700 (PDT)
From: "friend.05@gmail.com" <hirenshah.05@gmail.com>
Subject: Re: data file
Message-Id: <5a87a0b5-3ffa-463e-b056-2ec683b64d4f@p58g2000hsb.googlegroups.com>
On Oct 10, 12:57=A0pm, J=FCrgen Exner <jurge...@hotmail.com> wrote:
> "J. Gleixner" <glex_no-s...@qwest-spam-no.invalid> wrote:
> >friend...@gmail.com wrote:
> >> 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
>
> >>> Ben
>
> >> 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.
>
> >Well, it's not the way you posted.
>
> >Did you actually read the perldoc Ben mentioned above? =A0You don't use =
a
> >hash at all, so I'm guessing not.
>
> ACK!
>
> >while ( my $line =3D <$INFO> )
> >{
> > =A0 =A0chomp( $line );
> ># split the data.. you can split directly into the variables..
> ># my ( $v1, $v2 ) =3D ( split( /\|/, $line ) )[1,2];
> ># print $line to $OUT if the hash key of $cli_ip and $id doesn't already
> >exist.
>
> That will print each IP+code exactly once. I think (but I may be
> mistaken, the OPs isn't clear on that) he wants only those lines, that
> _are_ unique wrt. the IP+code, i.e. where there is no second line with
> the same IP+code.
>
> jue- Hide quoted text -
>
> - Show quoted text -
Thanks to all for help. That was helpful.
But.
I created the hash (IP+Code) combination.
But How to chk if this hash(each combination) is exactly one time in
file ?
------------------------------
Date: Fri, 10 Oct 2008 12:49:22 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: data file
Message-Id: <48ef95a2$0$89878$815e3792@news.qwest.net>
Jürgen Exner wrote:
> "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid> wrote:
>> friend.05@gmail.com wrote:
>>> 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
>>>>
>>>> Ben
>>> 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.
>> Well, it's not the way you posted.
>>
>> Did you actually read the perldoc Ben mentioned above? You don't use a
>> hash at all, so I'm guessing not.
>
> ACK!
>
>> while ( my $line = <$INFO> )
>> {
>> chomp( $line );
>> # split the data.. you can split directly into the variables..
>> # my ( $v1, $v2 ) = ( split( /\|/, $line ) )[1,2];
>> # print $line to $OUT if the hash key of $cli_ip and $id doesn't already
>> exist.
>
> That will print each IP+code exactly once. I think (but I may be
> mistaken, the OPs isn't clear on that) he wants only those lines, that
> _are_ unique wrt. the IP+code, i.e. where there is no second line with
> the same IP+code.
You're right, I mis-understood.
A fairly easy to follow solution would be to keep track of the data,
using two hashes.
my (%times, %line );
while(...)
{
# chomp,split,...
# times is the number of times the $cli_ip and $id were found
$times{ $cli_ip . $id }++;
# could 'next' if it is > 1
# and store the line itself, for the $cli_ip and $id
$line{ $cli_ip . $id } = $line;
}
Then, after the while, for each of the keys in %times, print the
value from %line where the value of $times{ $key } is 1, to the output file.
That should be enough to get the OP in the right direction, without
writing the whole darn thing for them.
------------------------------
Date: Fri, 10 Oct 2008 10:48:09 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: data file
Message-Id: <a25ve4lf4rs9dehe1ntupisvl92f60pdl7@4ax.com>
"friend.05@gmail.com" <hirenshah.05@gmail.com> wrote:
>I created the hash (IP+Code) combination.
>
>But How to chk if this hash(each combination) is exactly one time in
>file ?
You could count the number of occurences and then compare the count
against 1?
$IDTable{"$IP+$Code"}++;
[......]
if ($IDTable{"$IP+$Code"} == 1) {
print "Look ma, $IP+$Code occurs exactly once in the file\n";
------------------------------
Date: Fri, 10 Oct 2008 10:55:22 -0700 (PDT)
From: "friend.05@gmail.com" <hirenshah.05@gmail.com>
Subject: Re: data file
Message-Id: <e2033ff4-62cf-4efa-80db-544d51f7f51f@8g2000hse.googlegroups.com>
On Oct 10, 12:18=A0pm, J=FCrgen Exner <jurge...@hotmail.com> wrote:
> "friend...@gmail.com" <hirenshah...@gmail.com> wrote:
> >On Oct 9, 6:08=A0pm, 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.
>
> >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
> >$pri_file =3D "out_pri.txt";
>
> >$cnt =3D 0;
> >$flag =3D 0;
>
> >open(INFO_PRI,$pri_file)or die $!;
> >open(INFO,$pri_file)or die $!;
>
> >@pri_lines_ =3D <INFO>;
>
> >while($pri_line =3D <INFO_PRI>)
>
> [rest of code snipped]
>
> Many things I don't understand in this code, among them why you are
> using 2 file handles to the same file, why you are slurping in the whole
> file on one file handle and then process the file line by line on the
> other file handle, why you have a nested loop, etc, etc.
>
> Your requirements seem to be straight forward and easy to translate into
> a simple algorithm (warning, sketch only, not tested):
>
> my %idtable;
> open ($F, '<', $myfile) of die "Cannot read $myfile because $!\n";
> while (<$F>) { #loop through file and gather all IP | Code combinations
> =A0 =A0 =A0 =A0 (undef, undef, $ip, $code) =3D split '\|';
> =A0 =A0 =A0 =A0 $idtable{"$ip|$code"}++; #record this ip-code combination=
}
>
> seek $F, 0; #reset file to start
> while (<$F>) { #loop through file again and ....
> =A0 =A0 =A0 =A0 (undef, undef, $ip, $code) =3D split '\|';
> =A0 =A0 =A0 =A0 print if $idtable{"$ip|$code"} =3D=3D 1;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 #... print that line if the ip-code combi=
nation
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 #exists exactly once in the file
> close $F;
>
> jue- Hide quoted text -
>
> - Show quoted text -
Hi jue,
IF I use
$idtable{"$ip|$code"}++; #record this ip-code combination
will this not replace previous valuse if same key(ip-code) comes
again ?
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 1913
***************************************