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

i have a script which just reads a file and depending upon a keyword i just write selected line to an output file

however i am not getting the expected output

use warnings; my @lines; my $mess; open(hanr,"splitin.pl")or die"error $!\n"; open hanw, ">", "output.csv" or die $!; @lines = <hanr>; foreach my $line (@lines) { chomp($line); $line =~ /^\$outcome /x or next; my $Length = -2; my $mess = substr $line , 10 ,$Length; print hanw "$mess , \n"; } close hanr; close hanw;

upon checking even after chomp i am getting lines such as these

if ( $prevseg ne "Iaw" ) { $outcome="alT - spi - Script $0 aborted. new Error Code: 022. ";

because of indentation the regex fails , but i am puzzled as to why chomp doesnt work ?? ; why it does'nt remove the white spaces

Replies are listed 'Best First'.
Re: Chomp not working
by Athanasius (Archbishop) on Sep 24, 2014 at 08:47 UTC

    Hello sandy105,

    chomp removes the trailing newline from a string, if there is one; otherwise, it does nothing (unlike chop, which removes the final character from a string). To remove initial whitespace, you need something like this:

    $line =~ s/^\s+//;

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Chomp not working (Path::Tiny lines spew)
by Anonymous Monk on Sep 24, 2014 at 09:15 UTC
    Path::Tiny has chomp option :) also "or dies" on error ... here is pretty much the same code
    #!/usr/bin/perl -- use strict; use warnings; use Path::Tiny qw/ path /; my @lines = path( "splitin.pl" )->lines( { chomp => 1 } ); s/\s+$// for @lines; ## extra "chomp", trim trailing whitespace s/^\s+// for @lines; ## trim leading whitespace @lines = grep /^\$outcome /x, @lines; @lines = map { substr( $_, 10, -2 )."\n" } @lines; path( "output.csv" )->spew( @lines ); undef @lines;
Re: Chomp not working
by misterperl (Friar) on Sep 24, 2014 at 14:59 UTC
    # consider some changes to make your life easier
    # process the input and be done with it... die"error $!\n" unless open hanr,"splitin.pl"; my @lines = <hanr>; close hanr; my $r = ''; # its easier to debug when you have a scalar you can print + later, rather than printing line by line... for my $line ( @lines ) { # odd since $outcome is undef .. ? next unless $line =~ /^\$outcome /x; $line =~ s/.{9,9}(.+)../$1/; # get the middle chars $r .= $line; #append # print hanw "$mess , \n" # why chomp then add \n back in? Just l +eave it there....; } # process the output here die $! unless open hanw, ">output.csv"; print hanw $r; close hanw;
      $outcome is a literal "$outcome that i want to check for ; see i have escaped $ /^\$outcome/
Re: Chomp not working
by codiac (Beadle) on Sep 29, 2014 at 01:03 UTC
    If you want to remove leading and trailing white space, that is trim. https://metacpan.org/pod/String::Util#trim-string

    If you want to trim and flatten internal white space, that is crunch. https://metacpan.org/pod/String::Util#crunch-string