Steve Pittman wrote:
>
> I am using activestate on a windows box ...the files I am parsing are
> Unix files...I tried this so far...
>
> open ( IN, "<$_[0]" )||die "Can't open DAT source file: $tempFile $!\n";
>   while (<IN>){s/\r/\n/g;@lines = <IN>;}
>   close (IN);
>
>   foreach $line (@lines)
>   {

Your code is laid out a bit obscurely Steve. Unpacking it a little and taking
out some unnecessary stuff it looks like this:


open IN, $_[0] or die "Can't open DAT source file: $tempFile $!\n";

while (<IN>) {
  s/\r/\n/g;
  @lines = <IN>;
}

close (IN);


Is this code within a subroutine? Because if not then your @_ array shouldn't be
populated and you will have no filename. Also, what's $tempFile? However,
presumably your file open is succeeding, so..

The while loop only executes once. It will read the first record from the file
into $_, change all "\r" into "\n" in that record, throw it away, and read the
rest of the file into @lines. IN is now at eof so the loop will terminate.

I'm worried that you expect carriage returns terminating your records, yet you
say your file is a Unix file. Unix traditionally has lone linefeeds terminating
text records, whereas Windows has carriage return/linefeed pairs. But Perl
should hide either format from you and give you records ending in just "\n".
Records terminated by carriage return is the Mac standard, and you can get
around this by setting the $/ variable like this:

sub routine {

  my $file = shift;
  open my $in, $file or die "Can't open DAT source file: $file $!\n";
  my @lines;
  {
    local $/ = "\r";
    chomp(@lines = <$in>);
  }

  foreach my $line (@lines) {
    :
    :
  }
}

But please be sure that the data is really formatted as you think it is.

Also, please use strict and warnings, and declare all of your variables - it
saves an enormous amount of time debugging simple coding errors.

HTH,

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to