I need get many results from regular express

I

inuy

Hello ,

I have a regular express like this:
(...)(..)\s*(.)(...)....(\d*)....

And I want to store some information from it , I can use $data[1] =
$1;$data[2]=$2; $data[3]=$3 an so on.
But if there are more than ten data I need to store and I don't like
to write them by hand , I hope to store them by for loop or what else.

So I write this "wrong" code:

/(...)(..)\s*(.)(...)....(\d*)..../;
for $i ( 1..10 ){
$data[$i] = ${$i};
}

Obviously , it doesn't work.
Could somebody knows how to deal with this kind of problem?Thank you.
 
J

John W. Krahn

inuy said:
I have a regular express like this:
(...)(..)\s*(.)(...)....(\d*)....

And I want to store some information from it , I can use $data[1] =
$1;$data[2]=$2; $data[3]=$3 an so on.
But if there are more than ten data I need to store and I don't like
to write them by hand , I hope to store them by for loop or what else.

So I write this "wrong" code:

/(...)(..)\s*(.)(...)....(\d*)..../;
for $i ( 1..10 ){
$data[$i] = ${$i};
}

Obviously , it doesn't work.
Could somebody knows how to deal with this kind of problem?Thank you.

my @data = /(...)(..)\s*(.)(...)....(\d*)..../;



John
 
M

Mirco Wahab

inuy said:
I have a regular express like this:
(...)(..)\s*(.)(...)....(\d*)....
And I want to store some information from it , I can use $data[1] =
$1;$data[2]=$2; $data[3]=$3 an so on.
But if there are more than ten data I need to store and I don't like
to write them by hand , I hope to store them by for loop or what else.

/(...)(..)\s*(.)(...)....(\d*)..../;
for $i ( 1..10 ){
$data[$i] = ${$i};
}
Obviously , it doesn't work.

You got lots of correct advice
already - how to pull the results
into an array.

In the end, there's the answer left
how your wrong approach *would* have
had worked under 'strict' at least ;-)

My shot at it:


...
use strict;

my $str=' 11 22 33 44 55 66 77 88 99 110 111 112 113 114 115 116 ';
my $reg='(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+'
.'(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+';

my @data;

if( $str =~ /$reg/ ) {
for my $i (1..@--1) {
$data[$i] = substr $str,$-[$i],$+[$i]-$-[$i];
print "\$$i => $data[$i]\n"
}
}
...


But thats only for educational purpose.
Don't use things like that ...

To find out what the @- and @+ ($-[..], $+[..])
things do (in substr $str,$-[$i],$+[$i]-$-[$i]),
check out LAST_MATCH_END and LAST_MATCH_START
from 'perldoc perlvar'.

Regards

Mirco
 
M

Mirco Wahab

inuy said:
I have a regular express like this:
(...)(..)\s*(.)(...)....(\d*)....

And I want to store some information from it , I can use $data[1] =
$1;$data[2]=$2; $data[3]=$3 an so on.
But if there are more than ten data I need to store and I don't like
to write them by hand , I hope to store them by for loop or what else.

So I write this "wrong" code:

/(...)(..)\s*(.)(...)....(\d*)..../;
for $i ( 1..10 ){
$data[$i] = ${$i};
}

You got lots of correct advice
already - how to pull the results
into an array.

In the end, there's the answer
left how your wrong approach
*would* have had worked at least ;-)

My shot at it:

...
use strict;

my $str=' 11 22 33 44 55 66 77 88 99 110 111 112 113 114 115 116 ';
my $reg='(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+'
.'(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+(\d+)\D+';

if( $str =~ /$reg/ ) {
for my $i (1 .. @--1) {
no strict 'refs';
print "\$$i", " => ", ${"$i"}, "\n"
}
}
...


But thats only for educational purpose.
Don't use things like that ...

Regards

Mirco
 

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,770
Messages
2,569,586
Members
45,087
Latest member
JeremyMedl

Latest Threads

Top