Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Can PERL know a line without matching?

by brassmon_k (Sexton)
on Jul 27, 2001 at 20:04 UTC ( [id://100348]=perlquestion: print w/replies, xml ) Need Help??

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

Lets just say I have this text.
MEANINGLESS TEXT Bob is cool Bill is dumb Ted is cruel Sally wines allot Hit me on my pager 9207882332 I can't see the headlights. I'm blind now. ALTERNATIVE TEXT Turtles are faster than Bill's thought process Bill is dumb Ted is cruel Ted needs to get hit by a bus Sally should buy a muzzle Hit me on my pager 9207882332 I hate brights
Now let's say I specify $var on (STDIN). The $var would be the phone number in the line "Hit me on my pager". Now each section has a different amount of lines and the text for some of the lines is different in each section except for the "Hit me on my pager" line. Now if I do a match on that number and I wanted to print the section head if the number is found and a few selected lines from each heading how would I go about telling PERL that for "MEANINGLESS TEXT" "Bob is cool" is line3 and "Bill is dumb" is line4 and for "ALTERNATIVE TEXT" - "Ted is cruel" is line5 and "Sally should buy a muzzle" is line7 with all this text being in the same file so I can do something roughly equivalent to the following. Here's why I can't do a match on certain lines and I can only specify the line to print. Let's say under "MEANINGLESS TEXT" line4 is what I want to print but it won't always say "Bill is dumb" tommorow it might say "Sally is dumb" I USED ASTERISKS FOR LT AND GT BRACKETS SO THEY DON'T GET HTMLIFIED
print "What number?"; chomp($var = *STDIN*); $var1 = "MEANINGLESS TEXT"; $var2 = "ALTERNATIVE TEXT"; $line3 = (However you tell PERL that this is line1 of the "MEANINGLESS + TEXT" record block); $line4 = (However you tell PERL that this is line4 of the "MEANINGLESS + TEXT" record block); $line5 = (However you tell PERL that this is line5 of the "ALTERNATIVE + TEXT" record block); $line7 = (However you tell PERL that this is line7 of the "ALTERNATIVE + TEXT" record block); open(FILE, "resultsdata"); while (*FILE*) { if (/$var/ && /$var1/) { print "$var1\n"; print "$line3\n"; print "$line4\n"; if (/$var/ && /$var2/) { print "$var2\n"; print "$line5\n"; print "$line7\n"; } } }
(My dilemma is an if statement with the (and) operator won't match both scalars it only matches the first scalar $var or vice versa if I switched their positions, and the fact that I don't know how to tell PERL what and where a line is.
HELP!

The Brassmon_k

Replies are listed 'Best First'.
Re: Can PERL know a line without matching?
by bikeNomad (Priest) on Jul 27, 2001 at 20:28 UTC
    If you set $/ to "" you will read paragraphs at a time. That is, you will read a block of lines that are separated by 1 or more blank lines. Then you can split on newline boundaries:
    $/ = ""; # read paragraphs while (my $para = <>) { my $lastHeading; my @lines = split(/\n/, $para); if (@lines == 1) # heading? { $lastHeading = $lines[0]; next; } my $hitMe; if ($lastHeading eq 'MEANINGLESS TEXT') { $hitMe = $lines[4]; # fifth line } elsif ($lastHeading eq 'ALTERNATIVE TEXT') { $hitMe = $lines[5]; # sixth line } # now do something... }
Re: Can PERL know a line without matching?
by scain (Curate) on Jul 27, 2001 at 20:27 UTC
    It is somewhat difficult to understand from your problem statement, so I'll make a few general observations and hope you can take them and run, or clarify.

    First, you could indicate what part of the file you are in with a flag, ie,

    $meaningless=0; $alternate=0; while (<FILE>) { if (/MEANINGLESS/) { $meaningless=1; $alternate=0; } elsif (/ALTERNATE/) { $meaningless=0; $alternate=1; } }

    Next, I am not at all clear on why you need line numbers. A match is a match is a match. If it matches, you can print it, regardless of the line number. You can count line numbers if you combine with the above flag system:

    $meaningless_count=0; $alternate_count=0; while (<FILE>) { #include flags from above if ($meaningless) { $meaningless_count++; } elsif($alternate) { $alternate_count++; } }
    Does any of this help?

    Scott

Re: Can PERL know a line without matching?
by da (Friar) on Jul 27, 2001 at 20:48 UTC
    Please back up a few steps. Can you state what you want your code to do, in ordinary language? You say what your inputs are, what code you tried, but your description of what you want to accomplish is all muddled up with what you tried, and the end product doesn't make much sense, at least to me.

    Are you:

    • Trying to compare two files, and if they have an identical line, print the lines from the second file surrounding that line?
    • Searching a number of texts for a particular string from the command line, and if the string appears in one of the texts, print the first line of the text along with the lines surrounding the match?
    • or perhaps something more complicated?

    ___ -DA > perl -MPOSIX -le '$ENV{TZ}="EST";print ctime(1000000000)' Sat Sep 8 20:46:40 2001
      I'm searching a single text file. If I match on a phone number I want it to print certain lines within the block record that it find it in. The block records have different titles and data in them but always have the phone number. So if I find the phone number and devise a way of recognizing what record block it is then I can tell the script for this record block print these lines and for this record block print these lines only if you find the phone number and recognize the block record heading.

      The Brassmon_k
      The examples above helped me a little but am still not getting correct results. I can't seem to get the script to print off any other even if it matches them except for the line with the phone number in it.
        If I can restate the problem to make sure I understand it:

        You have a text file with records which start with a newline, a line of text, and another newline. You want to print selected lines from a record, if both the following are true: the record title matches a predefined list, and the phone number is in the record. The title of the record determines which lines of the record to print.

        Your question for us is how to find out what record you're looking at.

        It sounds like bikenomad's code should do almost exactly that, if you simply include print statements inside the if statement:

        if ($lastHeading eq 'MEANINGLESS TEXT') { print $lines[2]; # third line print $lines[5]; # sixth line print $lines[7]; # eighth line } elsif ($lastHeading eq 'ALTERNATIVE TEXT') { print $lines[3]; # fourth line print $lines[5]; # sixth line print $lines[7]; # eighth line
        Does this help?

        ___ -DA > perl -MPOSIX -le '$ENV{TZ}="EST";print ctime(1000000000)' Sat Sep 8 20:46:40 2001
        Ok, it is becoming a little more clear.

        You need to keep every line in the block, probably by assigning each line as an element of an array. After you determine that a text block is the one you want, you can print each line you want from the array, or all of them. How you decide to print a given line could be done with a regex or grep or other possiblities. If you can't get it to work now, post some code, and we'll give you pointers.

        Scott

Re: Can PERL know a line without matching?
by nardo (Friar) on Jul 27, 2001 at 20:16 UTC
    I USED ASTERISKS FOR LT AND GT BRACKETS SO THEY DON'T GET HTMLIFIED

    There is no need for this when your code is inside a <code> tag.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-24 17:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found