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

I need to implement what I think will be a very easy perl program. I've spent a few hours on it already and figure someone more experienced could give me the answer in less than 5 minutes.

I have a file with many lines, "output.txt", that I need to look for the string 'AGF30'.

Once I find that string, I need to search from that point to the first percentage symbol (%), and take the last 10 characters off the end of that line and put them into a variable (let's say $ir_value).

After that, I need to skip down 2 more lines and take the last 10 characters from that line and put that into $ysp_value.

Any takers? This would be unbelievably helpful in getting me started on my path to perl understanding. Thanks in advance.

Replies are listed 'Best First'.
Re: Simple parse of file - getting started
by Abigail-II (Bishop) on Sep 30, 2003 at 16:01 UTC
    Sounds like homework to me. Anyway, here's a program (reading from DATA) that will do what you want. Requires 5.8.1 with the defined-or patch.
    #!/usr/bin/perl use strict; use warnings; while (<DATA>) { last if /AGF30/; } exit unless defined; unless (/%/) { while (<DATA>) { last if /%/; } } exit unless defined; my $ir_value = substr $_ => -10; for my $c (1 .. 2) { no warnings 'misc'; $_ = <DATA> err exit; } my $ysp_value = substr $_ => -10; print "\$ir_value = $ir_value"; print "\$ysp_value = $ysp_value"; __DATA__ one line two line three line bla AGF30 foor four line feeble % 0123456789 one more two more ABCDEFGHIJ end

    Running this gives:

    $ir_value = 123456789 $ysp_value = BCDEFGHIJ
    (That's 10 characters, including the newline).

    Abigail

      Abigail, thank you SO much. I'm a Perl newb, but experienced programmer of Java, VB (ugh), C and a hack at half a dozen other languages. I have received a number of projects where I need to do a lot of text parsing of data files and figured maybe it's time to learn some Perl. Thanks again for your help. Rick
Re: Simple parse of file - getting started
by flounder99 (Friar) on Sep 30, 2003 at 18:42 UTC
    use strict; my $filedata = do {local $/; <DATA>}; my ($ir_value, $ysp_value) = $filedata =~ m/AGF30.*?%.*?(.{10})\n.*?\n +.*?(.{10})\n/s; print $ir_value,"\n",$ysp_value; __DATA__ one line two line three line bla AGF30 foor four line feeble % 0123456789 one more two more ABCDEFGHIJ end
    Outputs:
    0123456789 ABCDEFGHIJ
    This only works if there are in fact 10 or more characters on the line following the % and there are 10 or more characters in the 2nd and 4th lines following. The ten characters do not include the newline.

    --

    flounder

Re: Simple parse of file - getting started
by InfiniteSilence (Curate) on Sep 30, 2003 at 18:51 UTC
    Or you could use \G:
    #!/usr/bin/perl -w use strict; my ($one,$two); open(H,qq(<homework.txt))or die $!; local($/); my $file=<H>; close(H); if($file=~m/AGF30/g){if($file=~m/\G(?:.*\n?)*%(.*)\n.*\n(.*)/g){$one=$ +1;$two=$2}}; for($one,$two){$_=substr $_ ,length($_)-10,10}; print $one,$two; 1; __DATA__ 0123456789ABCDEFGHIJ <b>Update</b> Aw hell. Should've used \s!

    Celebrate Intellectual Diversity