extract a value from a field in a file

N

nufin

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 !

Best Regards,
Tof
 
M

Mark

nufin said:
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
 
N

nufin

Hi Mark,

Many thanks for your help !
I will test it and follow your recommandations.
Best Regards,
Christophe

nufin said:
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
 
G

Gunnar Hjalmarsson

nufin said:
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

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

In addition to Mark's comments, why do you think that line makes a
difference? Without knowing which platform you are on, I would suspect
it doesn't. Please read about newlines in "perldoc perlport".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top