regex problem -- capturing variables

Discussion in 'Perl Misc' started by tickfigure, Mar 17, 2005.

  1. tickfigure

    tickfigure Guest

    Hello,

    I've inherited some code and have to make a few changes. I'm sure this is
    not the way to go about this.... Hopefully someone can help me.

    I'm trying to generate the number of "captures" (variables $1, $2, etc.)
    of a regex dynamically. I don't know how many are required. I've included
    a small example below, so it is hopefully easier to understand.

    Thanks for any help!

    tick



    #! /usr/bin/perl

    use strict;
    my %caphash = (
    test1 => {
    a => "1",
    b => "1",
    c => "1",
    },
    test2 => {
    a => "1",
    b => "1",
    c => "1",
    }
    );

    my %hashresult;
    my %hash1;
    my $string = "another string here";

    my $pattern = '(\S+)\s+(\S+)\s+(\S+)';
    my $caps = 3; # Because there are three capturing parenths in $pattern.
    This variable
    # exists purely for this example.

    foreach my $ele (keys %caphash) { # foreach no.1
    my $test = $caphash{$ele};
    %hash1 = %$test;

    foreach my $ele2 (keys %hash1) { # foreach no.2

    # Here lies the problem.. since $pattern will change after each
    # complete iteration of foreach no.1, the number of "captures" will
    also change.
    # (I have only included 1 pattern for the example.)
    # How can I dynamically create the $1, $2, variables so that no
    # matter how many "captures" the values will be inserted into the hash
    # properly?


    # This obviously does not work.
    for (my $x = 1; $x <= $caps; $x++) {
    $string =~ /$pattern/;
    $hashresult{$ele}{$ele2} = $x;
    }
    }
    }
    print "End\n";
     
    tickfigure, Mar 17, 2005
    #1
    1. Advertising

  2. tickfigure

    Paul Lalli Guest

    "tickfigure" <> wrote in message
    news:eek:...
    > Hello,
    >
    > I've inherited some code and have to make a few changes. I'm sure

    this is
    > not the way to go about this.... Hopefully someone can help me.
    >
    > I'm trying to generate the number of "captures" (variables $1, $2,

    etc.)
    > of a regex dynamically.


    A pattern match in list context returns a list of all captures. Is this
    what you're looking for?

    my @caps = 'foo bar baz' =~ /(\w)(\w+)\s(\w+)/;
    # @caps contains ('f', 'oo', 'bar')


    > I don't know how many are required. I've included
    > a small example below, so it is hopefully easier to understand.
    >
    >
    > #! /usr/bin/perl
    >
    > use strict;


    you forgot
    use warnings;

    > my %caphash = (
    > test1 => {
    > a => "1",
    > b => "1",
    > c => "1",
    > },
    > test2 => {
    > a => "1",
    > b => "1",
    > c => "1",
    > }
    > );
    >
    > my %hashresult;
    > my %hash1;
    > my $string = "another string here";
    >
    > my $pattern = '(\S+)\s+(\S+)\s+(\S+)';
    > my $caps = 3; # Because there are three capturing parenths in

    $pattern.
    > This variable
    > # exists purely for this example.
    >
    > foreach my $ele (keys %caphash) { # foreach no.1
    > my $test = $caphash{$ele};
    > %hash1 = %$test;


    ....ew.

    while my ($key, $href) = each %caphash) {
    my %hash1 = %$href;

    >
    > foreach my $ele2 (keys %hash1) { # foreach no.2
    >
    > # Here lies the problem.. since $pattern will change after each
    > # complete iteration of foreach no.1, the number of "captures"

    will
    > also change.
    > # (I have only included 1 pattern for the example.)
    > # How can I dynamically create the $1, $2, variables so that no
    > # matter how many "captures" the values will be inserted into the

    hash
    > # properly?


    I really have no idea what you're getting at here. How will $pattern be
    changing? If that's the central issue, why did you make a test program
    in which $pattern doesn't change?!

    >
    >
    > # This obviously does not work.


    I don't doubt that, but you haven't explained what constitutes "working"
    in this case. What is your desired result? How should the finished
    %hashresult be structured?

    > for (my $x = 1; $x <= $caps; $x++) {
    > $string =~ /$pattern/;
    > $hashresult{$ele}{$ele2} = $x;


    My only guess is to go back to what I said originally. Do the pattern
    match in list context, and use the list of resulting captures. Without
    knowing more about your actual goal, it's difficult to be more precise
    than that.

    > }
    > }
    > }
    > print "End\n";



    Paul Lalli
     
    Paul Lalli, Mar 17, 2005
    #2
    1. Advertising

  3. tickfigure wrote:
    > I'm trying to generate the number of "captures" (variables $1, $2,
    > etc.) of a regex dynamically. I don't know how many are required. I've
    > included a small example below, so it is hopefully easier to understand.


    Well, the example didn't make it easier for me to understand. :)

    Maybe this code might point you in the right direction:

    my $string = 'another string here';
    my $pattern = '(\S+)\s+(\S+)\s+(\S+)';
    my @capvalues = $string =~ /$pattern/;
    print '@capvalues: ', join(', ', @capvalues), "\n";
    print 'Number of captures: ', scalar @capvalues, "\n";

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Mar 17, 2005
    #3
    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. Charles Shannon Hendrix

    REGEX: capturing on optional groups which fail

    Charles Shannon Hendrix, Jun 14, 2004, in forum: Perl
    Replies:
    0
    Views:
    566
    Charles Shannon Hendrix
    Jun 14, 2004
  2. Skip Montanaro
    Replies:
    1
    Views:
    348
    Gordon Chapman
    Jul 25, 2003
  3. Joerg Schuster

    more than 100 capturing groups in a regex

    Joerg Schuster, Oct 24, 2005, in forum: Python
    Replies:
    33
    Views:
    1,103
    Tim Peters
    Oct 27, 2005
  4. Hal Vaughan
    Replies:
    9
    Views:
    302
  5. Replies:
    3
    Views:
    808
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page