newbie question

Discussion in 'Perl' started by torjon@NOSPAMnterport.net, Aug 13, 2004.

  1. Guest

    I need to divide a series of fields, abcd, into single fields.

    I thought of using the pattern .{1}.{1}.{1}.{1} etc but what this
    returns is:
    $1 = abcdfg
    $2=a
    $3=b
    $4=c.

    Now I can work around this, just slipping $1, but I need to know how
    to make a regexp that will do this. Doesn't perl view the above
    regexp and say I need 1 single character 5 times, then work its way
    throughthe fields?

    Also, when a pattern matches in a reg exp it is stored in a variable
    $1. If a regexp has three matches it is stored in $1, $2 ,$3. I need
    to find the number of matches, the length of this array in order to
    iterate through it, or at least I need the name of the array so i can
    do a foreach..

    Thanks!
     
    , Aug 13, 2004
    #1
    1. Advertising

  2. wrote:
    > I need to divide a series of fields, abcd, into single fields.
    >
    > I thought of using the pattern .{1}.{1}.{1}.{1} etc but what this
    > returns is:
    > $1 = abcdfg
    > $2=a
    > $3=b
    > $4=c.


    Seems as if you have some reading to do about regular expressions.
    Start here:

    perldoc perlrequick

    However, even if you haven't showed us any code, I think you'd better
    use the split() function instead.

    perldoc -f split

    my @parts = split //, $field;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Aug 14, 2004
    #2
    1. Advertising

  3. wrote:
    > I need to divide a series of fields, abcd, into single fields.
    >
    > I thought of using the pattern .{1}.{1}.{1}.{1} etc but what this
    > returns is:
    > $1 = abcdfg
    > $2=a
    > $3=b
    > $4=c.


    Why not use a simple
    split(//, 'abcd');

    [...]
    > Also, when a pattern matches in a reg exp it is stored in a variable
    > $1.


    Hmmmm, no. From "perldoc perlvar":
    $<*digits*>
    Contains the subpattern from the corresponding set of capturing
    parentheses from the last pattern match, [...]

    There are no capturing parenthesis in your pattern /.{1}.{1}.{1}.{1}/

    > If a regexp has three matches it is stored in $1, $2 ,$3.


    Only if those "matches" are captured.

    > I need to find the number of matches,


    But you know already how many pairs capturing parenthesis are in your
    pattern. That is the number of captured matches.
    If there were less matches, then the whole RE would not have matched to
    begin with. And if there were more matches, then there is still unmatched
    text left.

    > the length of this array in order to
    > iterate through it, or at least I need the name of the array so i can
    > do a foreach..


    There is no array. $1, $2, ... are predefined standalone scalars, see
    perldoc perlvar.

    jue
     
    Jürgen Exner, Aug 14, 2004
    #3
  4. Joe Smith Guest

    wrote:

    > Also, when a pattern matches in a reg exp it is stored in a variable
    > $1. If a regexp has three matches it is stored in $1, $2 ,$3. I need
    > to find the number of matches, the length of this array in order to
    > iterate through it, or at least I need the name of the array so i can
    > do a foreach..


    The name of the array is whatever you want it to be.
    Use the /g option and put the results into an array of your choosing.

    @matches = $string =~ /(.)/g;
    print "Total of ",scalar @matches," found.\n"
    foreach $field (@matches) { ... }

    -Joe

    P.S. Post to comp.lang.perl.misc instead of comp.lang.perl next time.
     
    Joe Smith, Aug 14, 2004
    #4
  5. Guest

    ahh, you can split without a delim, perfect.

    Thans for all your help
    >However, even if you haven't showed us any code, I think you'd better
    >use the split() function instead.
    >
    > perldoc -f split
    >
    > my @parts = split //, $field;
    >
    >--
    >Gunnar Hjalmarsson
    >Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    , Aug 14, 2004
    #5
  6. Guest

    Thanks!
     
    , Aug 14, 2004
    #6
    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. Jerry C.
    Replies:
    8
    Views:
    256
    Uri Guttman
    Nov 23, 2003
  2. Kruno Saho
    Replies:
    0
    Views:
    149
    Kruno Saho
    Apr 7, 2013
  3. Dave Angel
    Replies:
    0
    Views:
    130
    Dave Angel
    Apr 7, 2013
  4. rusi
    Replies:
    0
    Views:
    117
  5. Miki Tebeka
    Replies:
    0
    Views:
    88
    Miki Tebeka
    Apr 7, 2013
Loading...

Share This Page