in reply to Re: Perl = Greek to me
in thread Perl = Greek to me

It looks like you pasted together a couple bits of code that do something similar to what you're trying to do, but together they don't actually work, and they're really not what you want anyway. You could do this task by slurping the entire file into a single string and then stepping through it by using a //g regex in a while test, but it'd look more like this:

while($huge_multi_line_string =~ /ccParty<(\S+)> Port<(\S+)> DTMF<(\S+ +)>/g){ # do something with $1, $2, and $3 }

But don't do that, because that's an ugly way to do it, especially with large files, since it means pulling the whole file into memory. You don't want to do that unless it's necessary, and it's not in your case. Start by thinking through your problem, and how you'd solve it logically, before getting into the code. Write pseudo-code if you have to. You've got a bunch of lines, and you want to pick out the lines that contain certain strings. So your logic will be:

open the file while there are more lines, get a line if the line matches certain strings do something with it close the file

Then it's just a matter of turning it into code. In perl, "while there are more lines, get a line" is normally done by using <angle brackets> around a filehandle in a while test. This returns one line from the filehandle to a scalar variable, or to $_ if you didn't specify a variable. Then within your while loop, you can use a regex (or regexes) to see if your line matches your requirements. A little time spent reading about using a while loop to read a file line-by-line, and some more time spent in perlretut, and you should be close, at least.

Aaron B.
My Woefully Neglected Blog, where I occasionally mention Perl.

Replies are listed 'Best First'.
Re^3: Perl = Greek to me
by smolikmd (Initiate) on Dec 23, 2011 at 18:12 UTC

    First, thank you, everyone, for your tips, suggestions, etc.

    I'm getting extremely frustrated which isn't helping solve anything. Here's my latest bit of code.

    { open my $textfile, '<', 'C:\Users\msmolik\Desktop\PERL test stuff\tes +tfile.log' or die $!; while ( my $line =~ m/ccParty<(.+)> DTMF<(.+)>/ ) { print "$line\n"; } } { close $textfile; }

    This returns nothing:

    C:\Users\msmolik\Desktop\PERL test stuff>testingperl.pl C:\Users\msmolik\Desktop\PERL test stuff>

    I've shortened my data file to 5 lines which contains 2 lines (the first and last) that "should" match my search. My data file contains the following lines:

    2011-12-21 00:23:06.904520%%0f-1f-16%%DB1%% ccParty<0x31A99> Port<1-14 +-7-22> DTMF<4> 2011-12-21 00:21:57.729881%%0f-1f-15%%TRF%% Received event <lmevtVOICE +_PATH_MODE_CHANGE> cc<lmccSTATE_CHANGED> R<otLIF_PORT:0x4107003> D<ot +PARTY_OBJECT:0x80B4B> T<otUNKNOWN:0x0> SRC<otLIF_PORT:0x4107003> Addr +<01-15-0c> 2011-12-21 00:23:06.904667%%0f-1f-16%%DB2%% ccParty<0x31A99> next CF<2 +1> 2011-12-21 00:23:06.904765%%0f-1f-16%%DB2%% ccParty<0x31A99> CF<21> I< +0> 2011-12-21 00:24:20.423024%%0f-1f-16%%DB1%% ccParty<0x31A9A> Port<1-14 +-7-23> DTMF<4>

    At this point I'm done. I have to take a timeout or I'll just leave this endeavor because I'm so frustrated.

      You're opening the file, but you're not reading it. $line never gets any data in it, yet you're trying to do a pattern match against it. First thing you should do is Use strict and warnings and you should get uninitialized warnings about $line (Update: That is, after you fix the scoping problem with $textfile). The basic pattern of your program should be something like:
      use strict; use warnings; open my $fh, .... or die "Err: $!"; while (my $line = <$fh>) { if ( $line =~ /...some pattern.../ ) { print $line; } } close $fh;
      Although you really should have done what I suggested in my first post and started with something like:
      use strict; use warnings; open my $fh, .... or die "Err: $!"; while (my $line = <$fh>) { print $line; } close $fh;
      Because until you get past that, you can't expect to run with scissors before you can walk :-)

        I'm still trying to crawl to the table to grab the scissors...

        So, I've done this and it works:

        use strict; use warnings; open my $textfile, '<', 'C:\Users\msmolik\Desktop\PERL test stuff\tes +tfile.log' or die $!; while ( my $dtmf = <$textfile>) { if ( $dtmf =~ m/ccParty<(.+)>.+DTMF<(\*\d)>/ ) { print "$dtmf\n"; } } { close $textfile; }

        and was able to do this:

        use strict; use warnings; print "What is the conference hexidecimal name?\nInput Name:"; my $Conf_Hex = <>; chomp ($Conf_Hex); while ( my $polling = <$textfile>) { if ( $polling =~ m/VtO<.+> VtTp<(?!0)(\d{1,3})>.+MCfID<0x$Conf_Hex +>/ ) { print "$polling\n"; } } { close $textfile; }

        Now, I've code that will convert a number in decimal to a hexidecimal. But I can't figure out how to combine that in the second code above to ask for the number then use the hexidecimal output instead of asking for the hexidecimal input.

        I guess in simple form, I want the conversion code to ask for a number. I give it 18. The output is 12. I then want to take that 12 and put it in the line of code above in place of the $Conf_Hex value in the "if" statement.

        Here's the code for the conversion:

        print "What is the Conference IDX?"; my $num = <>; chomp ($num); exit unless defined $num; $num = oct($num) if $num =~ /^0/; printf = ("%x\n", $num) ;

        Anyway, I'll keep plugging along. Thank you to everyone for suggestions and pushing me in the right direction.

      How do you expect this

      m/ccParty<(.+)> DTMF<(.+)>/ #..............^

      to match this?;

      2011-12-21 00:23:06.904520%%0f-1f-16%%DB1%% ccParty<0x31A99> Port<1-14 +-7-22> DTMF<4> #............................................................^^^^^^^^^ +^^^^^^

      If you changed your regex to m/ccParty<(.+)> .+DTMF<(.+)>/ it stands a chance of working.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?