Re: File Parsing Question

2006-01-22 Thread Shawn Corey

William Black wrote:

Hello,

I'm trying to figure out how to read multiple lines from a file at once for
parsing.  For example,, If the file contained the following:

input file

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8

I want to read in lines 1-4 for processing then during the next iteration
read lines 5-8.  Could someone give me a could starting place?


I know of no other way than the hard way.

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

while( ! eof DATA ){
  my @lines = ();
  for my $i ( 1 .. 4 ){
my $line = ;
push @lines, $line;
  }

  # process @lines
  print Dumper [EMAIL PROTECTED];

}

__END__
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9


--

Just my 0.0002 million dollars worth,
   --- Shawn

"Probability is now one. Any problems that are left are your own."
   SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is available at http://perldoc.perl.org/

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




re: File Parsing Question

2006-01-22 Thread William Black
Hello,

I'm trying to figure out how to read multiple lines from a file at once for
parsing.  For example,, If the file contained the following:

input file

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8

I want to read in lines 1-4 for processing then during the next iteration
read lines 5-8.  Could someone give me a could starting place?




--
William Black
[EMAIL PROTECTED]


Re: [beginner] file parsing question

2001-04-24 Thread M.W. Koskamp


- 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
>




Re: [beginner] file parsing question

2001-04-24 Thread Paul


--- Timothy Kimball <[EMAIL PROTECTED]> wrote:
> Ah, yes, one of the most frustrating bugs in the world:
> : if ($REFln[1] = "SN") {
> This *assigns* the value "SN" to $REFln[1]. What you want to do is
> *test* it. String comparisons in Perl are done with "eq" (and numeric
> comparisons with "=="). So you want this:
>   if ($REFln eq "SN") {

> You can avoid this by always writing comparisons with the constant
> (if there is one) on the left-hand side:
> 
>   if ("SN" eq $REFln)
> 
> but I rarely see people actually do that.

People rarely do it, btu I *HIGHLY* recommend it.
This way, if you write 
  if ("SN" = $REFln) {
the compiler will shut down and warn you that you can't *do* that, and
you'll probably say "Oh! that should have been ==, or maybe eq"

It's just cleaner code. =o)


__
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/



Re: [beginner] file parsing question

2001-04-24 Thread Paul Johnson

On Tue, Apr 24, 2001 at 04:33:00PM -0400, Timothy Kimball wrote:

> You can avoid this by always writing comparisons with the constant
> (if there is one) on the left-hand side:
> 
>   if ("SN" eq $REFln)
> 
> but I rarely see people actually do that.

I think that's because it feels so unnatural and looks so ugly ;-)

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net



Re: [beginner] file parsing question

2001-04-24 Thread Kevin Meltzer

Hi Joel,

Did you type this in by hand? :) 

>   parseRef ($testln);
> sub parseREF {

You would want to change one of those! Anyways..

Your problem is in this line:

>   if ($REFln[1] = "SN") {

= is for assignments. You want this to be:

if ($REFln[1] eq "SN") {

To learn more about perl operators like eq, =, and == (which is for numeric
comparisons, while eq is for strings) read the perlop manual page.

Cheers,
Kevin

On Tue, Apr 24, 2001 at 08:20:59PM -, Stout, Joel R ([EMAIL PROTECTED]) 
spew-ed forth:
> 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);
> 
> 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
> 

Writing CGI Applications with Perl - http://perlcgi-book.com
-- 
Nuclear explosions under the Nevada desert? What the f*ck are we testing for?
We already know the sh*t blows up.
-- Frank Zappa



Re: [beginner] file parsing question

2001-04-24 Thread Timothy Kimball


Ah, yes, one of the most frustrating bugs in the world:

:   if ($REFln[1] = "SN") {

This *assigns* the value "SN" to $REFln[1]. What you want to do is
*test* it. String comparisons in Perl are done with "eq" (and numeric
comparisons with "=="). So you want this:

if ($REFln eq "SN") {

You can avoid this by always writing comparisons with the constant
(if there is one) on the left-hand side:

if ("SN" eq $REFln)

but I rarely see people actually do that.
--

Tim Kimball · ACDSD / MAST¦ 
Space Telescope Science Institute ¦ We are here on Earth to do good to others.
3700 San Martin Drive ¦ What the others are here for, I don't know.
Baltimore MD 21218 USA¦   -- W.H. Auden



[beginner] file parsing question

2001-04-24 Thread Stout, Joel R

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);

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