Re: how to read values from a given paragraph
by CountZero (Bishop) on May 07, 2015 at 07:17 UTC
|
If I understand it well, you have one line of input BRNO MAGNO SLOTNO PRODNO PRODREV 31 1 14 ROJ 208 882/1 R1C PRODNAM MA
+NDATE SERNO MASTRP GED-DVD 20090604 CB49914646 1021 SLSTATE DATE TIME
+ 0 120628 131219
And you need the values of <PRODNAM>,<PRODNO>,<PRODREV> and <SERNO>. Please tell us what part of the input line relates to each of <PRODNAM>,<PRODNO>,<PRODREV> and <SERNO>.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James My blog: Imperial Deltronics
| [reply] [d/l] [select] |
|
|
hi thanks for replying
the values are mentioned exactly below these strings:
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
for e.g PRODNO=ROJ 208 882/1
PRODREV=R1C
PRODNAM=GED-DVD
SERNO=CB49914646
These values keeps changing everytime.
thanks and regards
| [reply] |
|
|
No, the values are not "mentioned exactly below these strings." Look at your own posts in this thread. See how your sample data is in one long string that wraps wherever it may, instead of the multiple lines of the original file, so you can't tell where the columns line up? Did you notice that it didn't look right when you previewed all these posts?
You've been told in this thread how to prevent that with code tags, so that people can understand your data and help you. The instructions above and below the posting textarea also tell you how. Had you done so, you would have had an answer to your first post a few hours ago. Please take a few moments to learn to post correctly, so that we can help you.
Aaron B.
Available for small or large Perl jobs and *nix system administration; see my home node.
| [reply] |
|
|
Why omit code tags? They're also used for data
| [reply] |
Re: how to read values from a given paragraph
by Anonymous Monk on May 07, 2015 at 07:21 UTC
|
| [reply] |
Re: how to read values from a given paragraph
by NetWallah (Canon) on May 07, 2015 at 18:39 UTC
|
Looking at the HTML source for your post. your data looks like this (I added code tags):
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
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.
#!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";
}
Run like this
perl line-analysis.pl data1.txt
Output is:
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
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
Hey Karl, Thanks a trillion !!! do i need to just run it or need some modifications too ?
As i am very poor in scripting :(
| [reply] |
Re: how to read values from a given paragraph
by Anonymous Monk on May 07, 2015 at 06:06 UTC
|
| [reply] |
Re: how to read values from a given paragraph
by vinoth.ree (Monsignor) on May 07, 2015 at 06:17 UTC
|
Hi Vijay
please give us sample output format, and moreover post your input within code tag so that it gives structure to the input.
All is well. I learn by answering your questions...
| [reply] [d/l] |
|
|
Thanks for replying,i need the output in belo format please.
PRODNAM=
PRODREV=
PRODNO=
SERNO=
regards,
| [reply] |
|
|
| [reply] [d/l] |
|
|
|
|
|
|
|
Re: how to read values from a given paragraph
by Anonymous Monk on May 07, 2015 at 08:04 UTC
|
| [reply] |
Re: how to read values from a given paragraph
by karlgoethebier (Abbot) on May 07, 2015 at 19:07 UTC
|
NetWallah tried to reverse engineer your problem.
It would be nice to get some feedback by you.
Regards, Karl
«The Crux of the Biscuit is the Apostrophe»
| [reply] |