----- Original Message -----
From: Stout, Joel R <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, April 24, 2001 10:20 PM
Subject: [beginner] file parsing question


> Sorry so lengthy but here goes:
>
> I am a Perl newbie and trying to parse a file.  Depending on the tags in
the
> data I want to parse each line a different way.  I built the following
> program to test my process.
>
> use strict;
> my (@lines, $testln, @REFln);

What is @lines for? Is doesnt seem to be used.
And @REFln could be declared inside sub parseREF; where $testln is better
declared locally in sub testType.
It might also be more efficient to read the file one time and then process
it....
try this:

#!perl

my @lines = (<>);
foreach $line (@lines) {
    chomp $line;
    my ($element1, $element2, $element3) = split (/\*/, $line, 3);
    if ($element1 eq 'REF' && !($element2 eq 'SN')) {
        print "Not a Shipment Number, $element1 type $line.\n";
   } else {
        print "I found a $element1 line: $line\n";
   }
}




>
> while (<>) {
> chomp;
> testType ($_);
> }
>
> sub testType {
> $testln = $_[0];
> if (/^REF/) {
> print "I found a REF line: $testln\n";
> parseRef ($testln);
> } elsif (/^NTE/) {
> print "I found a NTE line: $testln\n";
> }
> }
>
> sub parseREF {
> print "Parsing line: $_[0]\n";
>
> @REFln = split (/\*/, $_[0]);
>
> print "Element 1) $REFln[0] ";
> print " 2) $REFln[1] ";
> print " 3) $REFln[2]\n";
>
> if ($REFln[1] = "SN") {
> print "$REFln[1]: $REFln[2]\n";
> } else {
> print "Not a Shipment Number, $REFln[1] type line.\n";
> }
> }
>
> Based on the input:
> CUR**USD
> REF*SN*0108106
> REF*PO*cn190108106
> REF*BL*cn190108106
> REF*PS*JessupPA
>
> I expected to see "SN: 0108106" (which I did) but I didn't expect to see
> "SN: cn190108106" and "SN: cn190108106", "SN: JessupPA".
> I expected to see "Not a Shipment Number, PO/BL/PS  type line." for those.
> It almost seems like $REFln[1] is not being refreshed each time parseREF
> happens.
>
> I have written this same program in VB but I'm trying to do my parsing in
> Perl now .  To tell you the truth I really don't have a clue why this
> doesn't work, so any help is much appreciated.
>
> Joel
>

Reply via email to