For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2005 > How to get this hash?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to get this hash?
Jeff Pang

2005-11-17, 7:55 am

Hi,lists,

I have a file that look as:

--
User: honorhr40
Got data from YY23 (md1=3D23,md2=3D24):
[0] xxxx=3D46[I:0 T:Int]
[1] pd=3D[I:0 T:Str]
[2] cosnm=3Dnormal[I:0 T:Str]
--
User: lulu-918
Got data from YY18 (md1=3D18,md2=3D19):
[0] xxxx=3D69[I:0 T:Int]
[1] pd=3Dback.zzz.com[I:0 T:Str]
[2] cosnm=3Dnormal[I:0 T:Str]

.......

and thousands of these lines.

I want to get the No.1 line and No.5 line,just as:

User: honorhr40
[2] cosnm=3Dnormal[I:0 T:Str]

and put the two lines into a hash,as:

$hash{'honorhr40'} =3D 'normal';

I have no ideas on doing this work.Can you anyone give me some help?thanks.
John W. Krahn

2005-11-17, 7:55 am

Jeff Pang wrote:
> Hi,lists,


Hello,

> I have a file that look as:
>
> --
> User: honorhr40
> Got data from YY23 (md1=23,md2=24):
> [0] xxxx=46[I:0 T:Int]
> [1] pd=[I:0 T:Str]
> [2] cosnm=normal[I:0 T:Str]
> --
> User: lulu-918
> Got data from YY18 (md1=18,md2=19):
> [0] xxxx=69[I:0 T:Int]
> [1] pd=back.zzz.com[I:0 T:Str]
> [2] cosnm=normal[I:0 T:Str]
>
> ......
>
> and thousands of these lines.
>
> I want to get the No.1 line and No.5 line,just as:
>
> User: honorhr40
> [2] cosnm=normal[I:0 T:Str]
>
> and put the two lines into a hash,as:
>
> $hash{'honorhr40'} = 'normal';
>
> I have no ideas on doing this work.Can you anyone give me some help?thanks.


This appears to do what you want:

#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;

$/ = "--\n";

my %hash;

while ( <DATA> ) {

next unless /^User:\s*(.+?)$(?=.*=([^[]+)\[)/sm;

$hash{ $1 } = $2
}

print Dumper \%hash;

__DATA__

--
User: honorhr40
Got data from YY23 (md1=23,md2=24):
[0] xxxx=46[I:0 T:Int]
[1] pd=[I:0 T:Str]
[2] cosnm=normal[I:0 T:Str]
--
User: lulu-918
Got data from YY18 (md1=18,md2=19):
[0] xxxx=69[I:0 T:Int]
[1] pd=back.zzz.com[I:0 T:Str]
[2] cosnm=normal[I:0 T:Str]



John
--
use Perl;
program
fulfillment
Jeff Pang

2005-11-17, 7:55 am

Thanks for John.I have tested it,it is good for me.

2005/11/17, John W. Krahn <krahnj@telus.net>:
> Jeff Pang wrote:
>
> Hello,
>
nks.[color=darkred]
>
> This appears to do what you want:
>
> #!/usr/bin/perl
> use warnings;
> use strict;
> use Data::Dumper;
>
> $/ =3D "--\n";
>
> my %hash;
>
> while ( <DATA> ) {
>
> next unless /^User:\s*(.+?)$(?=3D.*=3D([^[]+)\[)/sm;
>
> $hash{ $1 } =3D $2
> }
>
> print Dumper \%hash;
>
> __DATA__
>
> --
> User: honorhr40
> Got data from YY23 (md1=3D23,md2=3D24):
> [0] xxxx=3D46[I:0 T:Int]
> [1] pd=3D[I:0 T:Str]
> [2] cosnm=3Dnormal[I:0 T:Str]
> --
> User: lulu-918
> Got data from YY18 (md1=3D18,md2=3D19):
> [0] xxxx=3D69[I:0 T:Int]
> [1] pd=3Dback.zzz.com[I:0 T:Str]
> [2] cosnm=3Dnormal[I:0 T:Str]
>
>
>
> John
> --
> use Perl;
> program
> fulfillment
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>

karthik S

2005-11-17, 7:55 am

John and Jeff,

Could you please explain the RE used.

/^User:\s*(.+?)$(?=3D.*=3D([^[]+)\[)/sm;

I understood that it looks for 'User:\s'. Then I am not able to figure
out the meaning of the remaining part of RE.


With Best Regards,=20

Karthikeyan S=20



-----Original Message-----
From: Jeff Pang [mailto:jeffpyh@gmail.com]=20
Sent: Thursday, November 17, 2005 2:06 PM
To: John W. Krahn
Cc: Perl Beginners
Subject: Re: How to get this hash?

Thanks for John.I have tested it,it is good for me.

2005/11/17, John W. Krahn <krahnj@telus.net>:
> Jeff Pang wrote:
>
> Hello,
>
help?thanks.[color=darkred]
>
> This appears to do what you want:
>
> #!/usr/bin/perl
> use warnings;
> use strict;
> use Data::Dumper;
>
> $/ =3D "--\n";
>
> my %hash;
>
> while ( <DATA> ) {
>
> next unless /^User:\s*(.+?)$(?=3D.*=3D([^[]+)\[)/sm;
>
> $hash{ $1 } =3D $2
> }
>
> print Dumper \%hash;
>
> __DATA__
>
> --
> User: honorhr40
> Got data from YY23 (md1=3D23,md2=3D24):
> [0] xxxx=3D46[I:0 T:Int]
> [1] pd=3D[I:0 T:Str]
> [2] cosnm=3Dnormal[I:0 T:Str]
> --
> User: lulu-918
> Got data from YY18 (md1=3D18,md2=3D19):
> [0] xxxx=3D69[I:0 T:Int]
> [1] pd=3Dback.zzz.com[I:0 T:Str]
> [2] cosnm=3Dnormal[I:0 T:Str]
>
>
>
> John
> --
> use Perl;
> program
> fulfillment
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>


--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Jeff Pang

2005-11-17, 7:55 am

Sorry,I can't understand for this RE too.John explain that pls.

2005/11/17, S, karthik (IE03x) <karthik.S@honeywell.com>:
> John and Jeff,
>
> Could you please explain the RE used.
>
> /^User:\s*(.+?)$(?=3D.*=3D([^[]+)\[)/sm;
>
> I understood that it looks for 'User:\s'. Then I am not able to figure
> out the meaning of the remaining part of RE.
>
>
> With Best Regards,
>
> Karthikeyan S
>
>
>
> -----Original Message-----
> From: Jeff Pang [mailto:jeffpyh@gmail.com]
> Sent: Thursday, November 17, 2005 2:06 PM
> To: John W. Krahn
> Cc: Perl Beginners
> Subject: Re: How to get this hash?
>
> Thanks for John.I have tested it,it is good for me.
>
> 2005/11/17, John W. Krahn <krahnj@telus.net>:
> help?thanks.
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>

Jeff Pang

2005-11-17, 7:55 am

Sorry,I really didn't know this regex written by John:

/^User:\s*(.+?)$(?=3D.*=3D([^[]+)\[)/sm;

Can anyone explain for that?thanks.

2005/11/17, Jeff Pang <jeffpyh@gmail.com>:
> Sorry,I can't understand for this RE too.John explain that pls.
>
> 2005/11/17, S, karthik (IE03x) <karthik.S@honeywell.com>:
>

John Doe

2005-11-17, 7:55 am

S, karthik (IE03x) am Donnerstag, 17. November 2005 09.54:
> John and Jeff,
>
> Could you please explain the RE used.
>
> /^User:\s*(.+?)$(?=.*=([^[]+)\[)/sm;
>
> I understood that it looks for 'User:\s'. Then I am not able to figure
> out the meaning of the remaining part of RE.


Hi karthik

Let me try to explain (and maybe expect a better explanation from JWK):

Because of

$/ = "--\n";

which defines what is considered as record separator, the regex is every time
applied to a whole data record consisting of 5 lines.

/^User:\s* matches the beginning of the first record line and following space.

(.+?)$

Then catches the user name into $1 (the $ matches the [real] line break).
Now to

(?=.*=([^[]+)\[)

The (?=......) construct is a "zero-width positive look-ahead assertion". It
matches nothing, just asserts that the construct content is following the
position of the actual match position.
(It's not 100% clear to me why JWK uses (?=) and not a simple (?:) - possibly
he takes in account the case that there is no [2] data line)

Now, left is, in (?=),

..*=([^[]+)\[

which I split now:

..*

matches as much chars (including line breaks) as possible until to the...

=

last '=' char. This is found in the [2] data line. Left is now

([^[]+)\[

The () catches what's in it into $2. In the () is

[^[]+

which is [^SOMETHING]+ . This matches one or more (due to +) char NOT (due to
^) containing SOMETHING. SOMETHING is '[' here. The match stops at

\[

, that is, a literal '[' - in the first record example: [I:0 T:Str]

The modifiers /m and /s are important for the interpretation of '$' and '.' in
the regex. This and everything else is explained in detail in

perldoc perlre


===

The regex won't do what you want if there are other lines after the [2] line.


joe

[...][color=darkred]

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com