I need get many results from regular express

Discussion in 'Perl Misc' started by inuy, Feb 5, 2007.

  1. inuy

    inuy Guest

    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.
     
    inuy, Feb 5, 2007
    #1
    1. Advertising

  2. 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
     
    John W. Krahn, Feb 5, 2007
    #2
    1. Advertising

  3. inuy

    Mirco Wahab Guest

    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, Feb 5, 2007
    #3
  4. inuy

    Mirco Wahab Guest

    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
     
    Mirco Wahab, Feb 5, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ad
    Replies:
    7
    Views:
    685
    Scott Allen
    Apr 11, 2005
  2. =?Utf-8?B?Q2FybCBNLg==?=
    Replies:
    4
    Views:
    2,494
    sivlookingforu
    Aug 7, 2006
  3. brooksr
    Replies:
    3
    Views:
    339
    Jon Paal
    Dec 24, 2006
  4. Jake Henderson

    Visual Web Developer 2005 Express and SQL 2005 Express

    Jake Henderson, Mar 10, 2006, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    248
    Jake Henderson
    Mar 10, 2006
  5. Replies:
    6
    Views:
    178
    Bob Barrows [MVP]
    Aug 17, 2006
Loading...

Share This Page