Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: pat match mult/lines mult/pat

by bikeNomad (Priest)
on Jul 27, 2001 at 01:56 UTC ( [id://100168]=note: print w/replies, xml ) Need Help??


in reply to pat match mult/lines mult/pat

Beyond the qw() problem that has already been noted, your logic is a bit odd:
foreach $pat (@patterns) { if ( /$pat[1]/ && /$pat[2]/) { print "theselines"; if ( /$pat[1]/ && /$pat[3]/) { print "theselines"; if ( /$pat[1]/ && /$pat[4]/) {

First, you're ignoring $pat[0] altogether. Then, you're using a non-existent element $pat[4]. And you're manually unrolling a loop and putting it inside a loop. If all you want to do is print a line when it matches what came from stdin and matches one of several patterns, just do something like this:

chomp(my $var = <STDIN>); $var = qr($var); my $pattern = qr($var1|$var2|$var3); while (<FILE>) { print if /$var/ && /$pattern/; }

Replies are listed 'Best First'.
Re: Re: pat match mult/lines mult/pat
by brassmon_k (Sexton) on Jul 27, 2001 at 19:09 UTC
    Yes I realized the mistakes I put in my initial entry like not defining the scalars above the array and neglecting array0 and array4 was non-existant.

    I've already achieved the results that your idea puts out. I guess my problem is a bit odd to explain.
    Those results are:
    Calling Party Number: TON: 4 NPI: 1 MSISDN: 6082572086 Calling Party Number: TON: 4 NPI: 1 MSISDN: 6082572086 Charge Number: TON: 3 NPI: 1 MSISDN: 6082572086 Calling Party Number: TON: 4 NPI: 1 MSISDN: 6082572086 Calling Party Number: TON: 4 NPI: 1 MSISDN: 6082572086 Called Party Number: TON: 2 NPI: 1 MSISDN: 16082572086 Called Party Number: TON: 2 NPI: 1 MSISDN: 16082572086
    Here's what I did with your idea.
    chomp(my $var = <STDIN>); $var = qr($var); $var1 = "MSTerminating" $var2 = "MSORIGINATING" $var3 = "TRANSIT" my $pattern = qr($var1|$var2|$var3); while (<FILE>) { print if /$var/ && /$pattern/; }
    Now you see what this bit of code did seemingly match only the $var it's like (and) operator pattern matches are ignored and only one is accepted.

    However I did indeed want those lines printed off but I also wanted the record block titles to print. If I do "print if /$vara/" it will do what I want print the record block titles but it will print all of them in the entire document. I only want it to print the section title when it finds the phone number. What I desire is output like the following: So to say it as clearly as I can is if I find the phone number in a record block such as "MSTerminating" or "MSORIGINATING" print the lines I specify like,
    line1
    line2 etc.
    Is there anyway to tell PERL that "MSTerminating" is line1 for a record block and this "Cell ID for First Cell: MCC: 310 MNC: 64 LAC: x'44D CI: x'4F07" would be line13? Can you tell perl line numbers because I've been looking and that would be ideal for my situation then it would be easy as pie. I could arrange something such as - if you find $var in "MSTerminating" print $line1, $line2, etc. Then the exact results would be there. The only problem is each record block (Like 8 different ones) have a different number of lines and different data in each type of record block. The desired example output is below and I've illustrated the differences with putting (line#) after the text for 3 of the entries as you can see.
    MSTerminating Cell ID for First Cell: MCC: 310 MNC: 64 LAC: x'44D CI: x'4F07 Calling Party Number: TON: 4 NPI: 1 MSISDN: 6082572086 (line15) Called Party Number: TON: 1 NPI: 1 MSISDN: 16084466501 (line16) Date for Start of Charge: 01/07/18 (line18) Time for Start of Charge: 00:14:33 Chargeable Duration: 00:00:00 MSORIGINATING Calling Party Number: TON: 4 NPI: 1 MSISDN: 6082572086 (line10) Called Party Number: TON: 1 NPI: 1 MSISDN: 16084466501 (line11) Date for Start of Charge: 01/07/18 (line30)
    Please for the love of lines how do you do accomplish telling PERL this. I've scoured the net and a few books I have to try to figure out how to tell PERL which line to print but all I've found is pattern matching because it's easier. Well unfortuneately I think I need to know how to tell PERL go print line1 then line13 if you find this number under the "MSTerminating" block because that's the only way it can be done in my case I think.

    The Brassmon_k
    David M. Hagens
    Airadigm Communications

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://100168]
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-16 18:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found