Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > regex problem -- capturing variables

Reply
Thread Tools

regex problem -- capturing variables

 
 
tickfigure
Guest
Posts: n/a
 
      03-17-2005
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";
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      03-17-2005
"tickfigure" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> 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

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      03-17-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
Regex: Capturing and replacing question Hal Vaughan Java 9 01-04-2008 06:38 AM
more than 100 capturing groups in a regex Joerg Schuster Python 33 10-27-2005 04:57 PM
REGEX: capturing on optional groups which fail Charles Shannon Hendrix Perl 0 06-13-2004 11:22 PM
Re: Regex: Limiting Scope without capturing results Skip Montanaro Python 1 07-25-2003 09:57 AM



Advertisments