
parsing a delimited file
Hi List
I am trying to parse the following file and load it into a hash.
File Structure
DN: 2570764 (PORTED-IN)
TYPE: SINGLE PARTY LINE
SNPA: 438 SIG: DT LNATTIDX: 4
XLAPLAN KEY : 438CAUC1 RATEAREA KEY : ILEPEROT
LINE EQUIPMENT NUMBER: LG 31 5 02 57
LINE CLASS CODE: 1FR
IBN TYPE: STATION
CUSTGRP: RESGRP SUBGRP: 0 NCOS: 1
LINE TREATMENT GROUP: 1
CARDCODE: RDTLSG GND: N PADGRP: PKNIL BNV: NL MNO: N
PM NODE NUMBER : 387
PM TERMINAL NUMBER : 258
DNGRPS OPTIONS:
NETNAME: PUBLIC
ADDRESS: 5144254676
OPTIONS:
COD DGT NAME PUBLIC HR BLOCK IMPOTS PIC 6885 Y
RES OPTIONS: NONE
OFFICE OPTIONS:
U3WC AIN TIID
Separater is colon(:)
my code
use strict;
use warnings;
my $fname = $ARGV[0];
open my $RFH,'<',$fname;
while (<$RFH>) {
next unless /:/ and my %field = /(.*?):(.*?)/mg;
foreach my $key (keys %field ) {
print "KEY=[$key]------VALUE=[$field{$key}]\n";
}
}
But I am not getting the desired result.
Thanks & Regards in advance
Anirban Adhikary.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: parsing a delimited file
--000e0cd32b8a7bba33049fd96a22
Content-Type: text/plain; charset=ISO-8859-1
See Inline comments
use strict;
> use warnings;
> my $fname = $ARGV[0];
> open my $RFH,'<',$fname;
>
> while (<$RFH>) {
> next unless /:/ and my %field = /(.*?):(.*?)/mg; # Wrong
>
# I would prefer doing this
chomp;
if(/^(.+?)\:(.+?)/){ $field{$1}=$2;}
}
> foreach my $key (keys %field ) {
> print "KEY=[$key]------VALUE=[$field{$key}]\n";
> }
> }
>
> But I am not getting the desired result.
>
> Thanks & Regards in advance
> Anirban Adhikary.
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
> For additional commands, e-mail: beginners-help [at] perl.org
> http://learn.perl.org/
>
>
>
--
Thanks
Ram
<http://www.netcore.co.in/>
n <http://pragatee.com>
--000e0cd32b8a7bba33049fd96a22--
Re: parsing a delimited file
--00163630eb3fdd07d5049fd97b94
Content-Type: text/plain; charset=ISO-8859-1
Have u run your code?\
Here u cant chomp because you have key/value pairs spans more than one
line..........
Thanks & Regards
Anirban.
On Fri, Apr 1, 2011 at 4:37 PM, Ramprasad Prasad <ramprasad.ap [at] gmail.com>wrote:
> See Inline comments
>
> use strict;
>> use warnings;
>> my $fname = $ARGV[0];
>> open my $RFH,'<',$fname;
>>
>> while (<$RFH>) {
>> next unless /:/ and my %field = /(.*?):(.*?)/mg; # Wrong
>>
> # I would prefer doing this
> chomp;
> if(/^(.+?)\:(.+?)/){ $field{$1}=$2;}
> }
>
>
>
>
>> foreach my $key (keys %field ) {
>> print "KEY=[$key]------VALUE=[$field{$key}]\n";
>> }
>> }
>>
>> But I am not getting the desired result.
>>
>> Thanks & Regards in advance
>> Anirban Adhikary.
>>
>> --
>> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
>> For additional commands, e-mail: beginners-help [at] perl.org
>> http://learn.perl.org/
>>
>>
>>
>
>
> --
> Thanks
> Ram
> <http://www.netcore.co.in/>
>
>
>
>
> n <http://pragatee.com>
>
>
--00163630eb3fdd07d5049fd97b94--
Re: parsing a delimited file
Anirban Adhikary wrote:
> Hi List
Hello,
> I am trying to parse the following file and load it into a hash.
>
> File Structure
>
>
> DN: 2570764 (PORTED-IN)
> TYPE: SINGLE PARTY LINE
> SNPA: 438 SIG: DT LNATTIDX: 4
> XLAPLAN KEY : 438CAUC1 RATEAREA KEY : ILEPEROT
> LINE EQUIPMENT NUMBER: LG 31 5 02 57
> LINE CLASS CODE: 1FR
> IBN TYPE: STATION
> CUSTGRP: RESGRP SUBGRP: 0 NCOS: 1
> LINE TREATMENT GROUP: 1
> CARDCODE: RDTLSG GND: N PADGRP: PKNIL BNV: NL MNO: N
> PM NODE NUMBER : 387
> PM TERMINAL NUMBER : 258
> DNGRPS OPTIONS:
> NETNAME: PUBLIC
> ADDRESS: 5144254676
> OPTIONS:
> COD DGT NAME PUBLIC HR BLOCK IMPOTS PIC 6885 Y
> RES OPTIONS: NONE
> OFFICE OPTIONS:
> U3WC AIN TIID
>
> Separater is colon(:)
>
> my code
>
> use strict;
> use warnings;
> my $fname = $ARGV[0];
> open my $RFH,'<',$fname;
You should always verify that the file opened correctly before trying to
use the filehandle:
open my $RFH, '<', $fname or die "Cannot open '$fname' because: $!";
> while (<$RFH>) {
> next unless /:/ and my %field = /(.*?):(.*?)/mg;
You are testing for the presence of a colon twice which is redundant.
In the match /(.*?):(.*?)/mg the /m option is superfluous because you
are not using the anchors ^ or $. But your real problem seems to be
':(.*?)' which will match *zero* non-newline characters after the colon.
In lines with more than one colon you need some way to properly parse
the different fields which may be difficult because it looks like some
keys have embedded whitespace.
> foreach my $key (keys %field ) {
> print "KEY=[$key]------VALUE=[$field{$key}]\n";
> }
> }
>
> But I am not getting the desired result.
What exactly are the desired results?
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: parsing a delimited file
I want to create a new file where I get each line like KEY=3DVALUE
In the source file in a single line the are multiple KEY-VALUE pair
and there is also some line where KEY VALUE pair spreads across two
lines.
On Fri, Apr 1, 2011 at 4:49 PM, John W. Krahn <jwkrahn [at] shaw.ca> wrote:
> Anirban Adhikary wrote:
>>
>> Hi List
>
> Hello,
>
>> I am trying to parse the following file and load it into a hash.
>>
>> File Structure
>>
>>
>> DN: =A0 =A0 2570764 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 (PORTED-IN)
>> TYPE: SINGLE PARTY LINE
>> SNPA: 438 =A0 SIG: DT =A0 =A0LNATTIDX: 4
>> XLAPLAN KEY : =A0 438CAUC1 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0RATEAREA KEY :=
=A0 =A0 ILEPEROT
>> LINE EQUIPMENT NUMBER: =A0 =A0 LG =A0 =A031 5 02 57
>> LINE CLASS CODE: =A0 =A0 =A01FR
>> IBN TYPE: STATION
>> CUSTGRP: =A0 =A0 =A0 =A0 RESGRP =A0 =A0 SUBGRP: 0 =A0NCOS: 1
>> LINE TREATMENT GROUP: =A0 =A0 1
>> CARDCODE: =A0RDTLSG =A0 =A0GND: N =A0PADGRP: PKNIL =A0BNV: NL MNO: N
>> PM NODE NUMBER =A0 =A0 : =A0 =A0387
>> PM TERMINAL NUMBER : =A0 =A0258
>> DNGRPS OPTIONS:
>> NETNAME: PUBLIC
>> ADDRESS: =A0 =A0 =A0 =A0 =A0 =A0 =A05144254676
>> OPTIONS:
>> COD DGT NAME PUBLIC HR BLOCK IMPOTS PIC 6885 Y
>> RES OPTIONS: NONE
>> OFFICE OPTIONS:
>> U3WC AIN TIID
>>
>> Separater is colon(:)
>>
>> my code
>>
>> use strict;
>> use warnings;
>> my $fname =3D $ARGV[0];
>> open my $RFH,'<',$fname;
>
> You should always verify that the file opened correctly before trying to =
use
> the filehandle:
>
> open my $RFH, '<', $fname or die "Cannot open '$fname' because: $!";
>
>
>> while (<$RFH>) {
>> =A0 =A0 =A0 =A0next unless /:/ and =A0my %field =3D /(.*?):(.*?)/mg;
>
> You are testing for the presence of a colon twice which is redundant.
>
> In the match /(.*?):(.*?)/mg the /m option is superfluous because you are
> not using the anchors ^ or $. =A0But your real problem seems to be ':(.*?=
)'
> which will match *zero* non-newline characters after the colon. =A0In lin=
es
> with more than one colon you need some way to properly parse the differen=
t
> fields which may be difficult because it looks like some keys have embedd=
ed
> whitespace.
>
>
>> =A0 =A0 =A0 =A0foreach my $key (keys %field ) {
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0print "KEY=3D[$key]------VALUE=3D[$field{=
$key}]\n";
>> =A0 =A0 =A0 =A0}
>> }
>>
>> But I am not getting the desired result.
>
> What exactly are the desired results?
>
>
>
> John
> --
> Any intelligent fool can make things bigger and
> more complex... It takes a touch of genius -
> and a lot of courage to move in the opposite
> direction. =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 -- Albert Einstein
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
> For additional commands, e-mail: beginners-help [at] perl.org
> http://learn.perl.org/
>
>
>
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: parsing a delimited file
Anirban Adhikary wrote:
> I want to create a new file where I get each line like KEY=VALUE
> In the source file in a single line the are multiple KEY-VALUE pair
> and there is also some line where KEY VALUE pair spreads across two
> lines.
That's nice.
Please do not top-post your replies. And please trim any unwanted text
in your replies. Thank you (in advance.)
And again: what exactly are the desired results?
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: parsing a delimited file
On 01/04/2011 12:00, Anirban Adhikary wrote:
> Hi List
>
> I am trying to parse the following file and load it into a hash.
>
> File Structure
>
>
[snip data]
>
> Separater is colon(:)
>
> my code
>
> use strict;
> use warnings;
> my $fname = $ARGV[0];
> open my $RFH,'<',$fname;
>
> while (<$RFH>) {
> next unless /:/ and my %field = /(.*?):(.*?)/mg;
> foreach my $key (keys %field ) {
> print "KEY=[$key]------VALUE=[$field{$key}]\n";
> }
> }
>
> But I am not getting the desired result.
The code below seems to do what you want.
Cheers,
Rob
use strict;
use warnings;
my $data;
{
local $/;
$data = <DATA>;
}
my $key = qr/ (?: \w+ [ ] )* \w+ /x;
my [at] xx = $data =~ /\G ( $key ) \s? : \s* (.+?) \s* (?= $key \s? : | \z)/sgx;
while ( [at] xx) {
my ($key, $val) = splice [at] xx, 0, 2;
printf "%s=%s\n", $key, $val;
}
__DATA__
DN: 2570764 (PORTED-IN)
TYPE: SINGLE PARTY LINE
SNPA: 438 SIG: DT LNATTIDX: 4
XLAPLAN KEY : 438CAUC1 RATEAREA KEY : ILEPEROT
LINE EQUIPMENT NUMBER: LG 31 5 02 57
LINE CLASS CODE: 1FR
IBN TYPE: STATION
CUSTGRP: RESGRP SUBGRP: 0 NCOS: 1
LINE TREATMENT GROUP: 1
CARDCODE: RDTLSG GND: N PADGRP: PKNIL BNV: NL MNO: N
PM NODE NUMBER : 387
PM TERMINAL NUMBER : 258
DNGRPS OPTIONS:
NETNAME: PUBLIC
ADDRESS: 5144254676
OPTIONS:
COD DGT NAME PUBLIC HR BLOCK IMPOTS PIC 6885 Y
RES OPTIONS: NONE
OFFICE OPTIONS:
U3WC AIN TIID
**OUTPUT**
DN=2570764 (PORTED-IN)
TYPE=SINGLE PARTY LINE
SNPA=438
SIG=DT
LNATTIDX=4
XLAPLAN KEY=438CAUC1
RATEAREA KEY=ILEPEROT
LINE EQUIPMENT NUMBER=LG 31 5 02 57
LINE CLASS CODE=1FR
IBN TYPE=STATION
CUSTGRP=RESGRP
SUBGRP=0
NCOS=1
LINE TREATMENT GROUP=1
CARDCODE=RDTLSG
GND=N
PADGRP=PKNIL
BNV=N
L MNO=N
PM NODE NUMBER : 387
PM TERMINAL NUMBER=258
DNGRPS OPTIONS=N
ETNAME=PUBLIC
ADDRESS=5144254676
OPTIONS=COD DGT NAME PUBLIC HR BLOCK IMPOTS PIC 6885 Y
RES OPTIONS=NONE
OFFICE OPTIONS=U3WC AIN TIID
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: parsing a delimited file
On 01/04/2011 13:57, Rob Dixon wrote:
>
> use strict;
> use warnings;
>
> my $data;
> {
> local $/;
> $data =<DATA>;
> }
>
> my $key = qr/ (?: \w+ [ ] )* \w+ /x;
>
> my [at] xx = $data =~ /\G ( $key ) \s? : \s* (.+?) \s* (?= $key \s? : | \z)/sgx;
My apologies, I had thought that there was always exactly one space
between the end of the key and the colon. In fact the field 'PM NODE
NUMBER' differs from all the others. The fix is to change this line to
my [at] xx = $data =~ /\G ( $key ) \s* : \s* (.+?) \s* (?= $key \s* : | \z)/sgx;
>
> while ( [at] xx) {
> my ($key, $val) = splice [at] xx, 0, 2;
> printf "%s=%s\n", $key, $val;
> }
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/