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

Isn't there a print line function?

by brassmon_k (Sexton)
on Sep 04, 2001 at 19:35 UTC ( #110057=perlquestion: print w/replies, xml ) Need Help??
brassmon_k has asked for the wisdom of the Perl Monks concerning the following question:


Let's just say I have this file and I use $/ to read in paragraphs and put all the lines in each paragraph into @lines. Now let's say in each paragraph I want to match a specific line and it isn't always in the same place in the paragraph which means it would have a different line number sometimes. So I can't say:
print ($lines[9]) if($lines[9] =~ "whatever);
I have to be able to print that specific line if it gets matched out of $_ or $lines. What I'm asking - Is there something like this?
print LINE if($_ =~ "whatever");
So when "whatever" is found by the match that line that "whatever" is on get's printed. Is there anything for PERL that does that or anyway to tell it that because I can't seem to do it.

The Brassmon_k

Replies are listed 'Best First'.
Re: Isn't there a print line function?
by dragonchild (Archbishop) on Sep 04, 2001 at 20:03 UTC
    Having answered a number of your questions from last week, I suspect that there's a conceptual issue going on. You seem to be coming from a language which doesn't really handle variable scoping very well, and for whom variables get modified by actions that Perl doesn't modify them in. Note that I can't think of such a language, but your questions seem to be coming from that slant.

    Your basic question is "How can I: do X to Y if Y has characteristic Z." You phrased it as "How can I print the 9th thing in X if I know that the 9th thing should have characteristic Y, but it might not be the 9th thing?"

    As I reccommended before, think your problems through. Instead of rushing into an editor and typing away, write your solutions out on paper first. In fact, you might try writing out your problem there, too. Oftentimes, it's not that the solution eludes us, but the problem does.

    I know it sounds like a waste of time, but think of it this way - if you manage to get it working without paper, great! But, you have been stopping into PM a lot. That's all well and good, but you're spending a lot of your time on understanding your problem vs. solving your problem. That time doesn't have to be spent like that.

    If you write our your solution in pseudo-code, then you'll start to immediately notice parts you don't understand very well. Heck, write it out in English, as if you were going to explain your proposed solution to someone who doesn't program at all. An example could be as follow:

    Problem: Given a paragraph, print out each line that contains the word "the".

    Solution (in English):

    1. Put all the lines into a list.
    2. Go through the list, starting with the first line.
    3. For each line, check to see if it contains the word "the".
    4. If it does, then write it to the screen.
    5. If it doesn't, skip the line.
    my @list = <LINES>; LINE: foreach my $line (@list) { if ($line =~ /(^|\s)the(\s|$)/i) { print "$line"; } else { next LINE; } }
    (Before anyone smarts off, yes, I know the next LINE; isn't strictly necessary.)

    See how each numbered part in English corresponds very nicely with the code? Try doing that once or twice for very simple examples and you'll see an immediate improvement in your understand of programming as a discipline.

    We are the carpenters and bricklayers of the Information Age.

    Vote paco for President!

      Just before you gave your answer dragonchild I figured it out:) Someone over on my side said exactly what you said...Write it out. I did and he stated why don't you just use a "foreach" loop and I was like DA! I feel like an idiot for not seeing something so simple. UGGGHHH. It kind of ticks me off. My main problem for programming is patience. I don't have enough of it and I rush off into VI like a mad code warrior hacking and slashing my way to no victory at all. I usually try to think it out as I go along but find that if I write in out in Enlgish first, then make a mock-up of the code it actually flows along pretty easy!

      The Brassmon_k
Re: Isn't there a print line function?
by suaveant (Parson) on Sep 04, 2001 at 19:46 UTC
    Well... to print the line itself you can simply do...
    print $_ if $_ =~ /whatever/; #or, since regexps default to use $_ print $_ if /whatever/;
    however, if you want the line number of the paragraph I would suggest one of the following
    for($i = 0; $i < @lines; $i++) { print $lines[$i] if $lines[$i] =~ /whatever/; } # or for(0..$#lines) { print $lines[$_] if $lines[$_] =~ /whatever/; } # or my $i = 0; for(@lines) { print $i if /whatever/; $i++; }

                    - Ant
                    - Some of my best work - Fish Dinner

Re: Isn't there a print line function?
by nardo (Friar) on Sep 04, 2001 at 19:43 UTC
    If you want to find which lines match a pattern you an use grep and if you want to print all them out you can use something like:
    print join('', grep(/whatever/, @lines));
    If you just want to print out the first match you can use
    print ((grep(/whatever/, @lines))[0]);
Re: Isn't there a print line function?
by rucker (Scribe) on Sep 04, 2001 at 19:45 UTC
    I'm not sure I understand this question. Assuming that you want to print a line if it contains "whatever", then if the line is in $_, you can just "print". E.g.
    $_ = "this is a line containing whatever\n"; print if /whatever/;
Re: Isn't there a print line function?
by davorg (Chancellor) on Sep 04, 2001 at 19:48 UTC

    Well, I don't know about PERL[1], but in Perl I think this does what you want:

    foreach (@lines) { print if /whatever/; }

    or am I missing something?

    [1] PERL seems to be a cut-down version of Perl that script-kiddies use to write bad CGI scripts :)


    Perl Training in the UK <>

Re: Isn't there a print line function?
by tachyon (Chancellor) on Sep 04, 2001 at 19:49 UTC

    How about a regex with the /m modifier like this:

    $/ = "\n\n"; # set to grab whole paragraphs, not lines my @paras = <DATA>; my $whatever = qr/paragraph/; for (@paras) { print "$1$2$3\n" if /^(.*)($whatever)(.*)$/m; } __DATA__ This is the first paragraph And paragraph two is here Here is the third paragraph to wrap it up




      There's a difference between setting $/ to "\n\n" and and empty string. In this case, the empty string would probably be better. Also your regex line is far too complicated. I'd write this code as follows:

      $/ = ''; # set to grab whole paragraphs, not lines my @paras = <DATA>; my $whatever = qr/paragraph/; for (@paras) { print if /$whatever/m; } __DATA__ This is the first paragraph And paragraph two is here Here is the third paragraph to wrap it up

      Perl Training in the UK <>

        While I will agree that '' is probably better you are wrong on the regex. You just dump the whole paragraphs. The task (I believe) was only to dump the single *line* within that paragraph that contained the match which is what my example does.




Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://110057]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2018-03-24 22:34 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (299 votes). Check out past polls.