just dave has asked for the wisdom of the Perl Monks concerning the following question:

Hello my fellow monks! I have a program that reads a report file(built of columns separeted by tabs)
It reads each line, and splits it by tabs, then loops through each "split string" and compares it to a "global string" .
Every thing works well except when dealing with the last "split string" since it contains the 'cr' or 'lf' characters, and this causes the 'cmp' or 'eq' to "think" that the string are different even though they are the same!.
Now I've tried chomp, and '=~ s/ \s//g' , but nothing seems to work(I might be using this in a wrong way).
Can anyone help shed some light?
Thanks, Dave (just dave...)

Replies are listed 'Best First'.
Re: How do I get rid of the cr/lf
by davorg (Chancellor) on Aug 22, 2006 at 13:23 UTC

    Can we please see (simplified) code and sample data. It's hard to know what the problem is from your description. "chomp" should remove whatever is the line end character on your system, but you might be being burned by cross-platform portability issues.

    Have you tried something like s/\012|\015//?

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Hi again, I'll give a code sample:
      #opening the file for reading open IN,"$fileName" ; #reading the whole file into an array. my @F=<IN>; close IN; #reading the last line of the file my $lastLine=$F[$#F]; my $firstLine=$F[0]; #spliting last line by tabs my @line=split /\t/,$lastLine; #spliting first line by tabs my @firstSplitLine=split /\t/,$firstLine; #finding the column number that equals the fieldName my $fieldNum = -1; my $numberOfFields = scalar(@firstSplitLine); for($counter=0 ; $counter < $numberOfFields ; $counter++) { $ourstring = $firstSplitLine[$counter]; $ourstring =~ s/\n\n/\n/g; #=~ s/ \s//g;# HERE IS WHERE I'M TRYING + DIFFERENT STUFF... if($fieldName eq $ourstring) # The "problematic" area { $fieldNum=$counter; last; } } # if the field wasn't found if($fieldNum==-1) { print STDERR "\n" if ($permit_writing); print STDERR "The field you asked for was not found, now returning l +ast value: $lastValue\n" if ($permit_writing); print STDERR "\n" if ($permit_writing); return $lastValue; } #printing the required field print STDERR " Value: $line[$fieldNum]\n\n" if ($permit_writing); return $line[$fieldNum];

      Edit: Changed <pre> tags to <code> tags (davorg)

        (Please use <code> tags to display code, not <pre>tags)

        Do you have "use warnings" in your code? I ask because the important piece of your code seems to be if($fieldName eq $ourstring) and according to the extract you've shown us, $fieldName is undefined at that point.

        But assuming that it's set somewhere outside of this extract, I still think that you can simplify a lot of this code. I'd consider setting up a hash where the keys are the column names and the associated values are the column numbers. Something like:

        my %columns; @columns{@split_first_line} = 0 .. $#split_first_line;

        You can then get the column number associated with a given field name with a simple:

        if (defined (my $field_num = $columns{$field_name})) { # field exists }

        Also, the best place to deal with chomping the input data is probably as soon as you've read it in, when a simple chomp(@f) would suffice.

        --
        <http://dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

      take a look at $/. Setting that should make chomp work
Re: How do I get rid of the cr/lf
by wojtyk (Friar) on Aug 22, 2006 at 15:30 UTC
    Also, to venture outside Perl for a moment...if you simply call dos2unix on your input files prior to processing them, the script won't have to deal with it at all.
Re: How do I get rid of the cr/lf
by mantra2006 (Hermit) on Aug 22, 2006 at 15:37 UTC
    Hey
    dos2Unix in Unix machines will work and remove all unwanted characters
    you can try the following sed commands
    sed 's/.$//' # assumes that all lines end with CR/LF sed 's/^M$//' # in bash/tcsh, press Ctrl-V then Ctrl-M

    Sridhar
Re: How do I get rid of the cr/lf
by planetscape (Chancellor) on Aug 23, 2006 at 13:20 UTC