Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   I need get many results from regular express (http://www.velocityreviews.com/forums/t901736-i-need-get-many-results-from-regular-express.html)

inuy 02-05-2007 03:14 PM

I need get many results from regular express
 
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.


John W. Krahn 02-05-2007 03:39 PM

Re: I need get many results from regular express
 
inuy wrote:
>
> 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
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall

Mirco Wahab 02-05-2007 09:21 PM

Re: I need get many results from regular express
 
inuy wrote:
> 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

Mirco Wahab 02-05-2007 09:26 PM

Re: I need get many results from regular express
 
inuy wrote:
> 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


All times are GMT. The time now is 04:34 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.