http://www.perlmonks.org?node_id=106280

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

Monks, please bestow upon me your wisdom. I have a pattern match problem. The problem is:

($msisdn) is a phone number

I match a variable ($msisdn) from STDIN. It works in a small text file from which I copied data from a big text file. I have my script reading in paragraphs splitting on the newline and it matches paragraph headings as $lines[1] and then searches for the variable ($msisdn) after it finds the appropriate paragraph heading. There are multiple paragraph headings of the same name throughout the document but with different ($msisdn's). The problem I'm having is the line that ($msisdn) is on isn't the same line throughout the document. The lines aren't being switched it's just that the "mSOriginating" paragraph has less or more lines sometimes, and I've been telling this script to look on different lines for the ($msisdn) but it doesn't seem to want to look on other lines and if it does it ignores the ($msisdn) match or it prints off all of the same paragraph headings in the entire document. I'll post the text I'm going through below. Now in this case the ($msisdn) is on line 43 or $lines[43] and that works fine but for the same paragraph heading "mSOriginating" (which occurs multiple times in my large text file) the ($msisdn) isn't always on line 43. So when my script finds that the ($msisdn) isn't on line 43 it complains that I have an uninitialized value for the pattern match in my pattern match statement. I don't get it:( The pattern match works but then it doesn't:< I'll show the text file first then my script. The line you want to look at in the text is the second from the bottom it's the "Called Party" line.
PCSPLMNCallDataRecord mSOriginating callPosition + 3'D chargeableDuration 0 1 1'BCD dateForStartOfCharge 1 8 13'BCD exchangeIdentity "MAPP01E 0117802"'S interruptionTime 0 0 0'BCD recordSequenceNumber 15701541'D tariffClass 10'D tariffSwitchInd 0'D timeForStartOfCharge 9 32 31'BCD timeForStopOfCharge 9 33 32'BCD callIdentificationNumber 5025228'D chargedParty 0'D switchIdentity 0001'H subscriptionType 00'H timeFromRegisterSeizureToStartOfCharging 0 0 9'BCD disconnectingParty 1'D internalCauseAndLoc 0 3'BCD mSCIdentification 1119207079800F'TBCD locationNumberOriginating 114141079808F0'H teleServiceCode 11'H iMSICalling 13600410003154F0'H timeForTCSeizureCalling 092015'H cellIDForFirstCellCalling 130046044D55B5'H radioChannelProperty 01'H firstRadioChannelUsed 00'H firstAssignedSpeechCoderVersion 01'H speechCoderPreferenceList 0100'H cellIDForLastCellCalling 130046044D55C8'H ->1|0|125 00'H ->1|0|123 00'H translatedNumber 116180446697F9'H originatingLineInformation 62'D chargeNumber 136084465140'TBCD networkCallReference 244F4A0001'H eosInfo 00'H typeOfCallingSubscriber 1'D originForCharging 1'D trafficActivityCode 11 2 13'BCD outgoingRoute "0GRI2"'S incomingRoute "BAPL01I"'S callingPartyNumber 14xxxxxxxxxx'TBCD ((((Th +is is the $msisdn line)))) calledPartyNumber 1xxxxxxxxF'TBCD
Now the script:
#!/usr/bin/perl -w use strict; my @lines; my $para; my $lastHeading; my $msisdn; print "What msisdn?"; chomp($msisdn = <STDIN>); $/ = ""; # read paragraphs while ($para = <>) { @lines = split(/\n/, $para); if (@lines == 1) # A Heading { $lastHeading = $lines[0]; next; } if ($lines[1] =~ "mSOriginating") { if ($lines[43] =~ "$msisdn") { print "MSORIGINATING\n"; print "$lines[0]\n"; print "$lines[8]\n"; print "$lines[7]\n"; print "$lines[43]\n"; print "\n"; } } }
Now if I do this:
if ($lines[43] || $lines[30] || etc... =~ "$msisdn")
The script doesn't function properly. It will print nothing out except the uninitialized error msgs. Now if I try separate if statements one below another in the same if it still does the same thing, example:
if ($lines[1] =~ "mSOriginating") { if ($lines[43] =~ "mSOriginating") { print "blah"; } if ($lines[30] =~ "mSOriginating") { print "blah"; } }
Now according (To my thought pattern atleast) Each if should be evaluated in order if the damn script can't match the ($msisdn) on 43 then it sees the next if for line 30 and if it finds nothing or if it does it shouldn't spit out uninitialized value errors. Even if I tell the script for "mSOriginating" all the lines it will find ($msisdn) 43, 30, etc... it will still error out with the named errors. However if I do this:
if ($lines[30] =~ "$msisdn") {
It works and gives no errors. That makes no sense at all. If I tell it to find the pattern for line 30 of the "mSOriginating" paragraph it will, and will give no errors. But if I tell it line[43] it will give errors. Even though both lines 43 and 30 (different "mSOriginatg") paragraphs have the ($msisdn) in the right place. I tested it so I know both work. The line 30 if will work in the big and small text file (This is what I need) but line 43 won't. However in the small text file the line 43 match works. In the small file however I only put "mSOriginating" paragraphs in that have the ($msisdn) on line 43 but I know it works because I copied and pasted them out of the big text file. Sorry for the length but I'm deeply troubled by this. My ship is sinking throw me a personal flotation device.

Any ideas?

The brassmon_k

Edit Masem 2001-08-20 - Several code/escape character edits for readability

Replies are listed 'Best First'.
Re: Same pattern match different lines
by brassmon_k (Sexton) on Aug 20, 2001 at 22:36 UTC
    Never mind I'm dumb I figured it out.

    $para is the input variable for what text file I tell the script to read as input. I was telling it to read $lines whatever number from @lines limited by a match on a paragraph heading. Of course it's going to give me errors if it's matching the paragraph heading from each paragraph and the ($msisdn) isn't where it expected it to be. It needs to read from the whole file. With $para I just tell it to look in each paragraph for the ($msisdn) $/ already did all the work. I was telling it to be to exact. I was telling the script look on this line when I should've been saying look in each paragraph if you match this and this then print. Instead I was telling the script look for a match on these lines (I didn't even tell it to look through paragraphs I was just telling the script to look for matches on lines. God I feel dumb...I am dumb for making that mistake. I just had to sit back and realize that it was a simple answer. Aren't those the hardest ones though...The thing that gets overlooked. Sorry to bother everyone.

    The Brassmon_k
    PS. I think it's kind of BS. to get minus points for readability edits but hey I guess you do what you gotta do.
Re: Same pattern match different lines
by Cine (Friar) on Aug 20, 2001 at 22:27 UTC
    Try
    if ($lines[42] =~ /mSOriginating/)

    Update:

    bleeder,
    if ($lines[42] =~ "mSOriginating")
    isnt the problem ;(

    T I M T O W T D I