[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] MacPerl bugs?



On Thu, Jun 03, 1999 at 11:39:53AM -0500, Scott Prince wrote:
> >Are you sure that the string ends with a newline when you split it?  If
> >your string is more like "a\tb\tc\t\t\t", then you _will_ get 3, because
> >split() drops trailing null fields by default.
> 
> Thats it! I was about to give it up. With MacPerl...
> 
> $x = "a\tb\t\t\t\n";
> chomp $x;
> @x = split (/\t/, $x);
> foreach $f (@x) { $count++; }
> print "$count\n\n";
> exit;
> 
> I lose the empties, on Unix (example below) they are retained. Why should 
> this be different for Unix?

What version of Perl??
perl5.002, the trailing null fields are dropped as expected.
perl5.004_04, the trailing null fields are dropped as expected.
perl5.005_02, the trailing null fields are dropped as expected.
Even perl4.036 drops the trailing null fields.


Here's a simple test case:

#!/usr/local/bin/perl -w
$_ = "first\tsecond\tthird\t\t\t";
@f = split(/\t/);
if (@f == 3) {
    print "Ok.\n";
} else {
    print "Not ok.\n";
}


> Should have posted the original code to begin with, but couldn't remember 
> which script it was from at the time...
> 
> while (<TEMP>)
> {
> $in_count++;
> $item_number++;
> @item_num = ("$item_number");
> $line = $_;
> chomp $line;
> @fields = split (/\t/, $line);
> $fields[3] = &format_date("month yyyy","$fields[3]","yyyymm");
>     # checking only the first record
>     if (!($field_check))
>     {
>         foreach $fc (@fields) { $number_of_fields_read++; }
>         if ($number_of_fields_read != $required_number_o_import_fields)
>         {
>         # error stuff...
>         }
>     $field_check = '1';
>     }

Where is the input coming from?  Is it possible that your input file has
Windows-style newlines - "\r\n"?  chomp() would remove the "\n", but the
"\r" would still be there, and split() would keep all the internal null
fields.


Ronald

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org