in reply to remove line break from 1 line

I'm pretty sure chomp only works on a single line...

I'm definitely sure that reading the entire file into a single variable isn't waht you want (particularly if you want to chomp). I would read it line by line and while(<$in>) { chomp } if I were you.

But if you must slurp the entire file into a var (which is fine when the files are small), then I would probably choose something like  $entire_file =~ s/[\r\n]//sg.

-Paul

Replies are listed 'Best First'.
Re^2: remove line break from 1 line
by chargrill (Parson) on Jan 20, 2007 at 06:11 UTC

    Actually... excerpted from perldoc -f chomp

    If you chomp a list, each element is chomped, and the total number of characters removed is returned.

    So , something like this will remove the locale equivalent of a newline from every element:

    my @input = <STDIN>; chomp( @input );

    Now everything entered on STDIN separated by a newline has been chomped.

    Just be wary of precedence... Also from perldoc -f chomp:

    Note that parentheses are necessary when you're chomping any- thing that is not a simple variable. This is because "chomp $cwd = `pwd`;" is interpreted as "(chomp $cwd) = `pwd`;", rather than as "chomp( $cwd = `pwd` )" which you might expect. Similarly, "chomp $a, $b" is interpreted as "chomp($a), $b" rather than as "chomp($a, $b)".


    --chargrill
    s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)