Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re^3: chomp question

by suno (Acolyte)
on Aug 08, 2012 at 09:58 UTC ( #986198=note: print w/replies, xml ) Need Help??

in reply to Re^2: chomp question
in thread chomp question

Here.. MVC,CLI,BNE are keywords... wherever CLI comes, i need to append the next line along with the current line..

Replies are listed 'Best First'.
Re^4: chomp question
by Jenda (Abbot) on Aug 08, 2012 at 10:57 UTC
    while (<>) { chomp if /^CLI\b/; print; } # or without all those default variables while (my $line = <$FILEHANDLE>) { chomp($line) if $line =~ /^CLI\b/; print $line; }

    Probably not exactly what you had in mind, right? In either case you either want to check whether the line starts with "CLI" and chomp() it if it does or remember the last line (or whether it started with CLI) and append the text you read instead of adding a new element into the array.

    my @lines; my $last_line_was_CLI = 0; while (<>) { chomp; if ($last_line_was_CLI) { $lines[-1] .= $_; $last_line_was_CLI = 0; } elsif (/^CLI\b/) { $last_line_was_CLI = 1; push @lines, $_; } else { push @lines, $_; } }

    Enoch was right!
    Enjoy the last years of Rome.

Re^4: chomp question
by ramlight (Friar) on Aug 08, 2012 at 13:59 UTC
    Perhaps this will do what you want to do?
    use strict; use warnings; while (my $line = <DATA>) { if ($line =~ /CLI/) { chomp $line; while (my $line2 = <DATA>) { next unless $line2 =~ /\S/; # ignore blank lines $line .= " $line2"; last; } } print $line; } __END__ MVC DOWO(8),WCPA CLI WCPA+2,C'.' BNE UPCC22
    which prints
    bash-3.2$ perl MVC DOWO(8),WCPA CLI WCPA+2,C'.' BNE UPCC22 bash-3.2$

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://986198]
[msh210]: Am I wrong?
[shmem]: no. You're right.
[msh210]: About time... last time I was right was... I don't even remember when.
[shmem]: perl -e 'warn "test$/", ^@^ '
[Eily]: well, at least it's right about warn doing the same thing as die :)
[shmem]: same with undef as last element
[msh210]: oh, good point. It's the best kind of correct
[shmem]: msh210: but I'd not call it a lie outright. That's when you do know the truth.
[msh210]: I don't have sendmail (I'm on MS Windows), so does one of you think you can report the bug?
[shmem]: msh210: 'tis about time to tell the sad truth to perl5porters - via perlbug

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (16)
As of 2017-04-24 12:43 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (439 votes). Check out past polls.