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

I am trying to fetch whatever is located where the number "113" is located. The number could be "145" or "345" or any three digit number. The number always changes so I am writing a script to notify me of the new number it changed to. It will always be 3 numbers and I just need to fetch the number. My problem is trying to make a reg expression to fetch the three digit number along with the unique part (which is on the next line):
<A HREF="JavaScript:AFunction('AA', 'B','0','Project','113')"> San Francisco (Manager)
There are many lines like this and the unique part is on the second line. In this case it is:
San Francisco (Manager).
<A HREF="JavaScript:AFunction('AA', 'B','0','Project','113')"> San Francisco (Manager) <A HREF="JavaScript:AFunction('AA', 'B','0','Project','680')"> Los Angeles (Worker) <A HREF="JavaScript:AFunction('AA', 'B','0','Project','456')"> San Jose (People)
Here is my attempt at it but having a problem fetching this number because I dont know how to handle the "San Francisco (Manager)" part which does not work in my attempt below:
my ($number) =~ /'(\d{3})'\)">San Francisco (Manager)/; print "$number";

Replies are listed 'Best First'.
Re: Fetching unique info
by PrimeLord (Pilgrim) on Jun 05, 2003 at 16:15 UTC
    I think the following code does what you are looking for.
    #!/usr/bin/perl -w use strict; open IN, "filename" or die "$!"; while (<IN>) { chomp; my ($first, $info) = (split />/); my $number = (split /'/, $first)[9]; print "$number => $info\n"; } close IN or warn "$!";
    So the output would look like the following:
    113 => San Francisco (Manager) 680 => Los Angeles (Worker) 456 => San Jose (People)


    Hope that helps.

    -Prime
      Thanks for all the responses. I still cant fetch the information. I know its coming down to the newline problem because I tried a test by putting it on one line:  <A HREF="JavaScript:AFunction('AA', 'B','0','Project','113')">San Francisco (Manager)
      and was able to fetch the different the number that was located where "113" was located:
      use LWP::Simple; my $url = 'www.website.com'; my $content = get($url); my ($number) = $content =~ /'(\d{3})'\)">San Francisco (Manager)/; print "$number\n";
      BUT I really need to fetch the information where it is split on two lines:
      <A HREF="JavaScript:AFunction('AA', 'B','0','Project','113')"> San Francisco (Manager)
      I tried both these and still didnt get it to work:
      my ($number) = $content =~ /'(\d{3})'\)">\nSan Francisco (Manager)/;
      and:
      my ($number) = $content =~ /'(\d{3})'\)">San Francisco (Manager)/s;
        Your use of my ($number) = $content =~ /'(\d{3})'\)">\nSan Francisco (Manager)/; seems almost correct. Why don't you modify it slightly to:
        my ($number) = $content =~ /'(\d{3})'\)">\s+San Franscisco \(Manager\) +/;
        You probably can't guarantee that the newline is a literal "\n", and you didn't escape the parens around the string "Manager".
Re: Fetching unique info
by hardburn (Abbot) on Jun 05, 2003 at 14:18 UTC

    The code you show does not do what you think it does. It's declaring a new variable, $number, and then matching the regex against it. Obviously, the regex won't match against an empty string.

    I think this code does what you meant to do:

    my $got = q!<A HREF="JavaScript:AFunction('AA', 'B','0','Project','113 +')">San Francisco (Manager)!; $got =~ /'(\d{3})'\)">San Francisco \(Manager\)/; my $number = $1; print $number;

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      Thanks, But the problem is the number 113 changes and that is the number I am trying to print. Whatever the new number that replaced 113 is the number I am trying to fetch and print. I tried this and it doesnt work:
      my $got = q!<A HREF="JavaScript:AFunction('AA', 'B','0','Project','(\d +{3}')">San Francisco (Manager)!; print $got;
        What about something like this, assuming all of the lines will follow the same pattern. Given file lines such as:
        <A HREF="blah('aa','b','0','Project','112)"> Some Label Without Any Digits
        Do:
        open(F,"file.html"); @array = <F>; close (F); foreach (@array) { if (/\d/) { my ($s1,$s2,$s3,$s4,$needednumber) = split(/,/); $needednumber =~ s/[^\d]//g; print $needednumber . "\n"; } }
Re: Fetching unique info
by jonnyfolk (Vicar) on Jun 05, 2003 at 15:19 UTC
    I'm not sure if you need to match the number to the unique part or not. The following splits your lines into 3 variables so that might help:
    #!/usr/bin/perl -w use strict; my $regex = q{<A HREF="JavaScript:AFunction('AA', 'B','0','Project','4 +56')"> San Jose (People) }; $regex =~ /'(\d{3})'\)">\n(.*?) (\(.*\))/; my $number = $1; my $area = $2; my $position = $3; print $number; print $area; print $position;