Re: extract a value from a field in a file



nufin wrote:
Hello,

I need to extract from a file values of the "QRKPageBegin" field.
Into the file, the fields appear like following:
%%QRKPageBegin: 2
%%QRKPageBegin: 4
%%QRKPageBegin: 5

I would like to extract each of the values of this field, to set a list.

Actually, (I am a perl newbie), I wrote the following code

open (PS,$PSFile) or die "Could not open the file $PSFile: $! \n";
        $/ = "\r";

        while (<PS>) {
                while (/([%%QRKPageBegin\s]{14,})/g) {
                        print $1, "\n";
                }
        }
        close PS;

However it display/get the field but not the value, so any help would be greatly appreciated !


For a start, make sure that you have

use warnings;
use strict;

at the top of every Perl script. This will catch a lot of otherwise hard-to-find errors.

You've misunderstood regular expressions (or at least partly misunderstood). [] defines a character class - you have no need for this here. You need a regex like

/^%%QRKPageBegin:\s*(\d+)$/

assuming that this is all that will appear on one line. Putting

use re 'debug';

at the start of your script will help you to debug regexs. See

perldoc re

for more details on this and

perldoc perlre

for general information about regexs.

regards,

Mark


.