Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

chomp question

by suno (Acolyte)
on Aug 08, 2012 at 08:02 UTC ( [id://986169]=perlquestion: print w/replies, xml ) Need Help??

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

hai, i have my line1 and line2 .Both the lines start with keyword1 and keyword2...i put the keywords to an array. while parsing,i end up at keyword1 which is line1...i need to append with the next following line..can you please help me how to solve this issue by using chomp function?

Replies are listed 'Best First'.
Re: chomp question
by davido (Cardinal) on Aug 08, 2012 at 08:22 UTC

    Where language is a barrier, you really need to show code, use proper formatting, and demonstrate input data, as well as expected output data. A well composed question often doesn't require much command of the language. Providing us with some examples lends the clarity needed for you to obtain a useful answer.


    Dave

Re: chomp question
by aitap (Curate) on Aug 08, 2012 at 08:11 UTC

    Sorry, your message was hard to understand.

    According to perldoc -f chomp, this function just removes $/ from the end of the scalar or from all elements of a list, but you want to append something to something.

    How do you parse your line1 and line2? What do you want to append? Where do you want to append it?

    EDIT: formatting, questions, links

    Sorry if my advice was wrong.
Re: chomp question
by Athanasius (Archbishop) on Aug 08, 2012 at 08:57 UTC

    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

      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..

Re: chomp question
by SerZKO (Beadle) on Aug 08, 2012 at 09:18 UTC

    Hello suno,

    Being novice in Perl myself, I've spent most of the time to understand hashes, lists and arrays. If you have some knowledge of some older programming language (e.g. C like me) you could easily end up in mixing things up (for example lists and arrays)...

    From your question I understand that you don't get next iteration on your array. If that is the case, you really need to read a bit more on Perl. Robert's Perl tutorial is a perfect place to start. http://www.sthomas.net/roberts-perl-tutorial.htm

    Happy Perl-ing !

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-19 05:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found