Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

The print "$line"; prints lines from the file, but the final print command just prints "1,,,,,,,,," Obviously my split isn't doing the job - can someone tell me why? Thanks
#!/usr/bin/perl -w use strict; my $special ="/path/special.txt"; my ($P_N,$N__B,$N__G,$dry,$my,$by,$Dad,$gyrate,$curd); my $count; print "Content-type: text/html\n\n"; open (PAGE, "$special") or die "Can't open $special: $!"; flock (PAGE, 1) or die "Can't lock $special for reading"; while (defined (my $line = <PAGE>)) {print "$line<br>"; $count++; $line = chomp ($line); ($P_N,$N__B,$N__G,$dry,$my,$by,$Dad,$gyrate,$curd) = split "\t",$l +ine; } print "$P_N,$N__B,$N__G,$dry,$my,$by,$Dad,$gyrate,$curd";

Replies are listed 'Best First'.
Re: array split problems
by jonnyfolk (Vicar) on Jan 20, 2004 at 16:35 UTC
    replace the line:
    $line = chomp ($line);
    with simply:
    chomp ($line);
    and your troubles will all be behind you...
      That worked perfectly thanks very much. Can you tell me why the line I wrote wouldn't work?

        perldoc -f chomp returns:

        chomp VARIABLE chomp( LIST ) chomp This safer version of "chop" removes any trailing string that corresponds to the current value of $/ (also known as $INPUT_RECORD_SEPARATOR in the "English" module). It returns the total number of characters removed from all its arguments.

        Your code didn't work because of the following statement: It returns the total number of characters removed from all its arguments..

        /oliver/

        Because chomp() returns the characters removed, not the characters remaining.

        The PerlMonk tr/// Advocate
Re: array split problems
by hardburn (Abbot) on Jan 20, 2004 at 14:55 UTC

    You're seeing the data on the very last line, which I'm guessing isn't formatted correctly with the rest of the file. The last line seems to contain something that becomes '1' in string context, and that's about all you can say about it based on what you've given.

    Update: Also, import the necessary flock constants from the Fcntl module (use Fcntl qw(:DEFAULT :flock);). Saying flock(FH, 1) . . . isn't portable. Fcntl will allow you to say flock(FH, LOCK_SH) . . . or flock(FH, LOCK_EX) . . ..

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated