regex problem -- capturing variables

T

tickfigure

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";
 
P

Paul Lalli

tickfigure said:
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
 
G

Gunnar Hjalmarsson

tickfigure said:
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";
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top