Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Re: Can I match a range from an array?

by brassmon_k (Sexton)
on Aug 03, 2001 at 18:28 UTC ( #101987=note: print w/ replies, xml ) Need Help??

in reply to Re: Can I match a range from an array?
in thread Can I match a range from an array?

You are right about warnings and strict I'm just frustrated trying to get this to work and I sort of developed a code writer's block from this. I'm not a 100% sure on what your saying? Are you suggesting this as I showed.

$1 = "Cell ID for First Cell: MCC: 310 MNC: 64 LAC: x'44D CI: x'MAD +023"; #This is what I want to print. %Firstcell = ( "Cell ID for First Cell: MCC: 310 MNC: 64 LAC: x'44D CI: x'4F07", " +Cell ID for First Cell: MCC: 310 MNC: 64 LAC: x'44D CI: x'MAD023", Cell ID for First Cell: MCC: 310 MNC: 64 LAC: x'44D CI: x'270B", "C +ell ID for First Cell: MCC: 310 MNC: 64 LAC: x'44D CI: x'APPL2B");
Now you say I don't need to grab the line with a regex match and you then say I need one to capture it? I'm kinda unclear there. See I don't know what the numbers on the end will be so I can't use grep to capture it.

So you're saying like this -
if ($lastheading = "MSTerminating") { if (grep "$1", @lines) { print "$_"; } }
I only have one problem. For some damn reason "$_" won't print anything. I have:
while ($para = <>) {
but that should still print "$_"

Anyway is what you were talking about something like the above cuz I don't get what you're saying.

The Brassmon_k

Comment on Re: Re: Can I match a range from an array?
Select or Download Code
Re: Re: Re: Can I match a range from an array?
by rchiav (Deacon) on Aug 03, 2001 at 19:05 UTC
    Well that's not quite what I was referring to.

    Here's what I understand:
    -You have a line that has a number at the end of it that signifies something.
    -You want to print something depending on the number.
    -I'm assuming that what you want to print is the name of the cell site.

    Soooo.. What I would do is the following..

    #!/usr/bin/perl -w use strict; #putting the line in question into $_ for this example. $_ = "Cell ID for Last Cell: MCC: 310 MNC: 64 LAC: x'44D CI: x'503E +"; #create our hash of 'site codes' => 'text to print' my %cells = ('207B' => 'Miami', '432F' => 'Buffalo', '443R' => 'Whatever you want for cell site 443R', '503E' => 'Portland, OR' ); if (/CI: x'(\w+)$/) { print "Cell site is $cells{$1}.\n"; }
    Now.. if the regex confuses you, I'd suggest spending a little time with perlre. What this basically does it grabs the last part of the string that you said is variable. Then it takes that and looks it up in the hash. Now this is a very simple example, without error checking and the other various things that you should have in there. You need to take into account if there's no entry in the hash... and how you deal with that.

    Hope this helps..

      (If you want to know more about a function mentioned here, click on it and you'll get the documentation on that specific function, including syntax and suggested uses.)

      Another idea here is to do something along the lines of the following:

      foreach my $line (@the_cell_site_lines) { my @data = split /\s+/, $line; # If you want the cellnumber, do the following... my $cell_number = $data[12]; $cell_number = substr($cell_number, 2); # Do something here with the cellnumber you now have. }
      The first thing I noticed is that the lines are regular and stuff is separated by spaces. This immediately made me think of split and using an array of values to figure out what was going on.

      Once that happens, I can grab the 13th item (remember that Perl array indices start at 0, so it's the 12th index). That item is "x'503E". I have to get rid of the first 2 characters, so I use substr. I could've used a regex (something like $cell_number =~ s/^x'//;), but why use a cannon when all you need is a pistol? substr is faster and clearer.

      Creating a hash of cellnumber => cellname is an extremely good idea, and I suggest you do that. There are ways of putting that into a separate file and bringing it in either using do or require. If you want help with those, ask. (Oh - when you first do your testing, don't type the entire hash in - just use maybe 5-10 entries, to make sure your code works. Then, add the rest.)

      /me wants to be the brightest bulb in the chandelier!

      OK That's really neat, I wasn't thinking at it like that. I kind of thought that's what you wanted me to do but wasn't sure, I see now though. However, one final problem, OK, I don't know what any of the lines will be so how do I tell the script that if it matches one of the numbers to print out the correct value from the hash.

      You're telling it to print out Miami for $cells{1} but I don't know what the cell site line is going to be it might be 503E so I'd have to tell it somehow to print out the name when you find the number in the record block and in the hash so it knows what key/value to print. So my dilemma is really the printing and matching.

      I can do both for a single match no problem. However I have to do 387 of them because there are 387 different Cell site names. So how can I tell the if statement this.

      Well your regex will match any cell site line I know that, and if it's under the "if (lastHeading = "MSTerminating)" then it will only evaluate in an "MSTerminating" record block so that one is good and now the final problem which I haven't been able to solve. How do I tell the script to print out the line without knowing what number will be matched. In my current script I don't know why "$_" won't print, but that's the only way I can guess to print out something when you don't know what the answer will be but you know what you want to do with the answer.

      The Brassmon_k
        OK.. you really should look at perlre. The regex /CI: x'(\w+)$/ looks for "CI: x'<one or more word characters><end of line>".
        The parens around the \w+ capture that to the variable "$1". And that is going to be the code you're looking for. So in my example.. $1 == "503E". And for any line, it's going to equal whatever is between "x'" and the end of the line.

        Hope that clairifies things. And also.. read all of perlre. All of this will be much clearer, and you're probably completely rewrite your script as a result of learning what you can do with regular expressions.


Re: Re: Re: Can I match a range from an array?
by brassmon_k (Sexton) on Aug 03, 2001 at 19:25 UTC
    OK that is so cool that you helped me. I finally got it but yet I don't. My brain block went away and I figured it out. Haven't gotten to much sleep lately. Here it is an almost working version that does everything I want. Now what I can't figure out is the fact that for the if statement you know if ( $cellsite{cell} ) and if ( $cellsite{cell1} ) it's A: obviously finding the Cell ID lines because it prints them out but the problem is B: The "if" statements are supposed to match up with the right "Cell ID" line and only print out the value for that key. My problem is that when I put in the phone number to limit the search results and the value for the key is the "Cell ID MAD023" one it still will print the "Cell ID APPL03". See their are 2 "MSTerminating" record blocks in the file it searches through each have 2 different numbers and 2 different "Cell ID lines" the problem is though that it prints both values for one phone number. What am I doing wrong. The output of the script I'll place below the script. I'm not that familiar with voting here cuz I just hit level 2 today and that (++) or (-) radio buttons above our entries (Didn't see that on level one) but I can give you guys like
    ++ points right? If that's the case you guys are getting em. I'm just double checking with you guys. Anyway here's the code.
    #!/usr/bin/perl -w use strict; my $msisdn; my $cell; my %cellsite; my $lastHeading; my $para; my @lines; my $cell1; chomp($msisdn = <STDIN>); $cell = "Cell ID for First Cell: MCC: 310 MNC: 64 LAC: x'44D CI: x' +4F07"; $cell1 = "Cell ID for First Cell: MCC: 310 MNC: 64 LAC: x'44D CI: x +'APP03"; %cellsite = ("$cell", "Cell ID for First Cell: MCC: 310 MNC: 64 LAC: + x'44D CI: x'MAD023", "$cell1", "Cell ID for First Cell: MCC: 310 M +NC: 64 LAC: x'44D CI: x'APP03"); $/ = ""; # read paragraphs while ($para = <>) { @lines = split(/\n/, $para); if (@lines == 1) # A Heading { $lastHeading = $lines[0]; next; } if ($lastHeading eq "MSTerminating") { if ($lines[8] =~ m/$msisdn/) { if ( $cellsite{$cell} ) { print "$cellsite{$cell}\n"; } if ( $cellsite{$cell1} ) { print "$cellsite{$cell1}\n"; } print "MSTerminating\n"; print "\n"; print "$lines[8]\n"; print "$lines[4]\n"; } } elsif ($lastHeading eq "MSORIGINATING") { if ($lines[7] =~ m/$msisdn/) { print "MSORIGINATING\n"; print "\n"; print "$lines[7]\n"; # sixth line } } elsif ($lastHeading eq "TRANSIT") { if ($lines[7] =~ m/$msisdn/) { print "$lines[7]\n"; } } elsif ($lastHeading eq "mSOriginatingSMSinSMSIWMSC") { if ($lines[4] =~ m/$msisdn/) { print "$lines[4]\n"; } } }
    Command line/Script output.
    $ exp4.53 output #Command line 6082572086 #Command line OUTPUT Cell ID for First Cell: MCC: 310 MNC: 64 LAC: x'44D CI: x'MAD023 Cell ID for First Cell: MCC: 310 MNC: 64 LAC: x'44D CI: x'APP03 MSTerminating Calling Party Number: TON: NPI: 1 MSISDN: "phone number" MSC Identification: TON: 1 NPI: 1 MSISDN: "phone number"
    So you can see my problem. For the phone number I looked up in this record block the "Cell ID" line has "4F07" at the end - So the script looks at the hash and prints out the appropriate garbage. However for the "Cell ID" line "503E" isn't there and that when translated to it's name is "APP03" so that shouldn't have printed but it did anyway even though that number isn't in the "Cell ID" line for this particular record block. So it's printing one line of garbage. What's the problem, I can't figure this out.

    The Brassmon_k
      Would it be possible for you to give mock lists with saaaay, three records, the tranlation list your boss gave you (that have corresponding data in it that match up to the cdr's) and anything else that would make testing in a duplicate environment easier?

      - Jim

        Well I won't have to do that now. The only problem I had left was an incorrectly formed regex. I knew a regex was my answer but I wasn't putting in the right one. I also knew I had to use a hash. I never really had to use them before because I never had to get a 387 different line match to work before with a hash. Hashes are beautiful, it knew what to print out. Well it's been fixed now so it's cool.

        Well if you still want to take a look let me know.

        The Brassmon_k

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://101987]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2015-03-30 06:30 GMT
Find Nodes?
    Voting Booth?

    When putting a smiley right before a closing parenthesis, do you:

    Results (633 votes), past polls