Perl Regexp that deals with variable parameter-string length.

A

asspenm

Hello all,

I've been writing a perl script that requires dealing with a file
containing lines with strings in the form of:

XXX-YYY-ZZZ (3 parameters pattern)

or

XXX-YYY-ZZZ-AAA (4 parameters pattern)

or

XXX-YYY-ZZZ-AAA-BBB.... (N parameters pattern)

While XXX, YYY, etc are strings (locations).

I need to calclulate a total distance between those locations:

$total_distance += $DISTANCE{$2} - $DISTANCE{$1} ;

In other words - i've got from 3 to N parameters per line, seperated
by a dash.

I've tried to think of a smart way to assign them to the back-
refference variables, lets say $1 to $N, but did not succeed.

Currently i'm just breaking up the pattern with split and running over
it with foreach, but this is ugly, i'm sure there's a way to include
them in a regexp that assigns all $1..$N accordingly.

Any ideas?
Thanks
Assaf
 
B

Brian McCauley

Hello all,

I've been writing a perl script that requires dealing with a file
containing lines with strings in the form of:

XXX-YYY-ZZZ (3 parameters pattern)

or

XXX-YYY-ZZZ-AAA (4 parameters pattern)

or

XXX-YYY-ZZZ-AAA-BBB.... (N parameters pattern)

While XXX, YYY, etc are strings (locations).

I need to calclulate a total distance between those locations:

$total_distance += $DISTANCE{$2} - $DISTANCE{$1} ;

There is no obvious way in which that can be extended to more than 2
items.
In other words - i've got from 3 to N parameters per
line,seperated by a dash.

I've tried to think of a smart way to assign them to the back-
refference variables, lets say $1 to $N, but did not succeed.

Currently i'm just breaking up the pattern with split and
running over it with foreach, but this is ugly, i'm sure
there's a way to
include them in a regexp that assigns all $1..$N accordingly.

You need to update your aesthetic metric. The natural representation
storing a for a list of values in Perl is an array not several named
variables. The $1 etc variables are not an array because in general
the 1st,2nd,3rd capture in a regex is not semantically related as
members of a list.
Any ideas?
From what we can guess about your real problem and the solution you
are trying (using split) you are doing it right.

Of course without seeing what you are actually doing or what you are
actually trying to achieve it's hard to be sure.
 
A

attn.steven.kuo

Hello all,

I've been writing a perl script that requires dealing with a file
containing lines with strings in the form of:

XXX-YYY-ZZZ (3 parameters pattern)

or

XXX-YYY-ZZZ-AAA (4 parameters pattern)

or

XXX-YYY-ZZZ-AAA-BBB.... (N parameters pattern)

While XXX, YYY, etc are strings (locations).

I need to calclulate a total distance between those locations:

$total_distance += $DISTANCE{$2} - $DISTANCE{$1} ;


Does that mean you want the sum of differences
between overlapping, adjacent "locations"?

while (<DATA>) {
my $total = 0;
$total += ($2 - $1) while /\G(\d{3})-(?=(\d{3}))/gc;
print $total, "\n";
}

__DATA__
111-222-333
111-222-333-444
000-123-246-369
 
B

Brian McCauley

On 25 Mar, 18:41, "(e-mail address removed)"
Does that mean you want the sum of differences
between overlapping, adjacent "locations"?

Er, the sum of differences between overlapping adjacent pairs of
elements in a list is just the difference between the first and last
elements in the list.

This, of course, has nothing to do with Perl.
 
A

attn.steven.kuo

On 25 Mar, 18:41, "(e-mail address removed)"


Er, the sum of differences between overlapping adjacent pairs of
elements in a list is just the difference between the first and last
elements in the list.

This, of course, has nothing to do with Perl.


Er, true. But probably I had misinterpreted the O.P.'s
intent; (sum of) *distance* between locations should be, then,

$total += abs($2 - $1) while /\G(\d{3})-(?=(\d{3}))/gc;

(added 'abs'). If applied to '111-333-222', it would yield
a result of 333.
 

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
473,999
Messages
2,570,244
Members
46,839
Latest member
MartinaBur

Latest Threads

Top