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.
On Fri, Apr 1, 2011 at 4:49 PM, John W. Krahn <jwkr...@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: 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-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > > -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/