Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: chomp question

by Athanasius (Archbishop)
on Aug 08, 2012 at 08:57 UTC ( [id://986184]=note: print w/replies, xml ) Need Help??


in reply to chomp question

Hello suno, and welcome to the Monastery!

As other monks have said, your question isn’t clear. The following is a guess at what you wanted:

#! perl use strict; use warnings; my @lines = ("First line\n", "Second line\n", "Third line\n", "Fourth line\n", "Fifth line\n",); my @keywords = qw(Second Fourth); my @matches; for (my $i = 0; $i < $#lines; ++$i) { my ($first_word) = $lines[$i] =~ /^\s*(\w+)\b/; for (@keywords) { if ($_ eq $first_word) # Look for a keyword { chomp(my $line1 = $lines[$i ]); # *see below chomp(my $line2 = $lines[$i + 1]); # *see below push @matches, $line1 . $line2; last; } } } my $count; print 'Match ', ++$count, ": '$_'\n" foreach @matches;

Output:

Match 1: 'Second lineThird line' Match 2: 'Fourth lineFifth line'

The key lines of code are the ones marked *see below. By applying chomp to a newly-created variable, the code removes the newline (if any) from the end of a copy of each line of text, without changing the original lines in the array.

HTH,

Athanasius <°(((><contra mundum

Replies are listed 'Best First'.
Re^2: chomp question
by suno (Acolyte) on Aug 08, 2012 at 09:56 UTC
    hi, thanks you so much for the reply.. i am sorry that my question is not clear to all... my actual objective is as follows: say my input is :
    MVC DOWO(8),WCPA CLI WCPA+2,C'.' BNE UPCC22
    the required output is something like :
    MVC DOWO(8),WCPA CLI WCPA+2,C'.' BNE UPCC22
    hope this will make my question clear...

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

        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, $_; } }

        Jenda
        Enoch was right!
        Enjoy the last years of Rome.

        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 suno.pl MVC DOWO(8),WCPA CLI WCPA+2,C'.' BNE UPCC22 bash-3.2$

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://986184]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-19 23:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found