in reply to how to read values from a given paragraph
To parse this, try the code below. Logic is complicated by the fact that the data format is not consistent, and it needs to handle several special cases.BRNO MAGNO SLOTNO PRODNO PRODREV 31 1 14 ROJ 208 882/1 R1C PRODNAM MANDATE SERNO MASTRP GED-DVD 20090604 CB49914646 1021 SLSTATE DATE TIME 0 120628 131219
Run like this#!perl use strict; use warnings; my (%val, @line); while (<>){ chomp $_; my $offset = /^(\s+)/?length($1) : 0; if (@line){ #print "$_ \[DATA]\n"; my @datavalues = split; if (scalar(@datavalues)==scalar(@line)){ for (0..$#datavalues){ $line[$_]{VALUE} = $datavalues[$_] ; #print "\[ $line[$_]->{NAME}=$line[$_]->{VALUE}] "; $val{ $line[$_]->{NAME} } = $line[$_]->{VALUE}; } }else{ for my $fld (@line){ $offset = $fld->{OFFSET}; $fld->{VALUE} = substr($_,$offset,length($fld->{NAME} +) + $fld->{TRAILINGSPACES} ); $fld->{VALUE}=~s/^\s+//; # Zap leading spaces $fld->{VALUE}=~s/\s+$//; # and trailing spaces #print "\[ $fld->{NAME}=$fld->{VALUE}] "; $val{ $fld->{NAME} } = $fld->{VALUE}; } } #print "\n"; @line=(); # Zap it }else{ while( m/(\S+)(\s*)/g){ push @line, {NAME=>$1, TRAILINGSPACES=>length($2),OFFSET=>$off +set}; $offset += length($1)+length($2); } #print "$_\n"; #print " Name =$_->{NAME} \t Tr.Spaces=$_->{TRAILINGSPACES} \t +off=$_->{OFFSET}\n" for @line; } } for (sort keys %val){ print " $_\t= $val{$_};\n"; }
BRNO = 31; DATE = 120628; MAGNO = 1; MANDATE = 20090604; MASTRP = 1021; PRODNAM = GED-DVD; PRODNO = ROJ 208 882/1; PRODREV = R1C; SERNO = CB49914646; SLOTNO = 14; SLSTATE = 0; TIME = 131219;
"You're only given one little spark of madness. You mustn't lose it." - Robin Williams
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: how to read values from a given paragraph
by karlgoethebier (Abbot) on May 07, 2015 at 18:57 UTC | |
by get2vijay (Novice) on May 08, 2015 at 04:24 UTC |