retrieving info from file

P

Peter

i'm using the following line to try and extract some data from a file

($probe) =~ m/\(-vop crop\=(\d+)\:(\d+):(\d+):(\d+)/;

This is the information i'm sorting through in the file.

crop area: X: 0..718 Y: 0..479 (-vop crop=718:480:0:0)

I want to get the 4 crop values 718, 480, 0, 0 all stored
in separate variables.
using
($probe) =~ m/\(-vop crop\=(\d+)/;
gets me the 718, but using the whole line at the top doesn't get me the
last 0
it also returns 718.. any idea how to get the other 3 values.
I really need the last 0.
Thanks
 
S

Sam Holden

i'm using the following line to try and extract some data from a file

($probe) =~ m/\(-vop crop\=(\d+)\:(\d+):(\d+):(\d+)/;

This is the information i'm sorting through in the file.

crop area: X: 0..718 Y: 0..479 (-vop crop=718:480:0:0)

I want to get the 4 crop values 718, 480, 0, 0 all stored
in separate variables.
using
($probe) =~ m/\(-vop crop\=(\d+)/;
gets me the 718, but using the whole line at the top doesn't get me the
last 0
it also returns 718.. any idea how to get the other 3 values.
I really need the last 0.


$1 == 780
$2 == 480
$3 == 0
$4 == 0

Will be true after the first regex sauccessfully matches, if the
sample line shown is in $probe.

The parenthesis around $probe are strange, so maybe you didn't paste you
real code and meant:

($probe) = m/\(-vop crop\=(\d+)\:(\d+):(\d+):(\d+)/;

In which case:

You are only asking perl to keep the first value returned...

Try:

($probe, $x, $y, $z) = m/\(-vop crop=(\d+):(\d+):(\d+):(\d+)/;

Programming is far easier, if you try and understand why code does what
it does, instead of guessing.
 
T

Tad McClellan

Peter said:
i'm using the following line to try and extract some data from a file

($probe) =~ m/\(-vop crop\=(\d+)\:(\d+):(\d+):(\d+)/;
^ ^
^ ^

Why do you have those parenthesis there?
 
P

Peter

$1 == 780
$2 == 480
$3 == 0
$4 == 0

Will be true after the first regex sauccessfully matches, if the
sample line shown is in $probe.

The parenthesis around $probe are strange, so maybe you didn't paste you
real code and meant:

($probe) = m/\(-vop crop\=(\d+)\:(\d+):(\d+):(\d+)/;

In which case:

You are only asking perl to keep the first value returned...

Try:

($probe, $x, $y, $z) = m/\(-vop crop=(\d+):(\d+):(\d+):(\d+)/;

Programming is far easier, if you try and understand why code does what
it does, instead of guessing.
i used the following
$probe =~ m/\(-vop crop\=(\d+):(\d+):(\d+):(\d+)/;
print "$1, $2, $3, $4\n";
and it worked perfectly. Thanks a lot for your help!
 
S

Sam Holden

i used the following
$probe =~ m/\(-vop crop\=(\d+):(\d+):(\d+):(\d+)/;
print "$1, $2, $3, $4\n";
and it worked perfectly. Thanks a lot for your help!

if ($probe =~ m/\(-vop crop\=(\d+):(\d+):(\d+):(\d+)/) {
print "$1, $2, $3, $4\n";
} else {
warn "Couldn't extract data from: $_";
}

or something to that effect.

Always check if the regex matched, otherwise old values of $1, etc. will
be used and hard to find bugs will result.

Yes, always.

And you don't need to escape '=', it's just a plain old character.
 
T

Tad McClellan

Peter said:
My mistake...i didn't use them..sorry for the confusion!


Do not re-type Perl code
Use copy/paste or your editor's "import" function rather than
attempting to type in your code. If you make a typo you will get
followups about your typos instead of about the question you are
trying to get answered.
 

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

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top