regex help

S

si

hi all, i had a regex question for the group:

i'm trying to do parse a string 'a = 3 b = 4 c = 5 d = 6'

and would like to extract an array of pairs of values


ie: myArray(a) = 3
myArray(b) = 4
myArray(c) = 5 etc

one complication is that i don't know how many pairs of values will be
in the original string

any idea of how I can do this with regex?
 
G

Gunnar Hjalmarsson

si said:
hi all, i had a regex question for the group:

i'm trying to do parse a string 'a = 3 b = 4 c = 5 d = 6'

and would like to extract an array of pairs of values

In Perl such an associative array is called a hash.
ie: myArray(a) = 3
myArray(b) = 4
myArray(c) = 5 etc

one complication is that i don't know how many pairs of values will be
in the original string

One approach:

my %myHash;
while ( $string =~ /(\w+)\s*=\s*(\w+)/g ) {
$myHash{$1} = $2;
}
 
S

Scott Bryce

si said:
hi all, i had a regex question for the group:

i'm trying to do parse a string 'a = 3 b = 4 c = 5 d = 6'

and would like to extract an array of pairs of values


ie: myArray(a) = 3
myArray(b) = 4
myArray(c) = 5 etc

one complication is that i don't know how many pairs of values will be
in the original string

any idea of how I can do this with regex?

Do you HAVE to use a regex?


use strict;
use warnings;

my $string = 'a = 3 b = 4 c = 5 d = 6';
$string =~ s/=//g;

my %hash = split / */, $string;

foreach my $key (keys %hash)
{
print "$key = $hash{$key}\n";
}
 
A

Anno Siegel

si said:
hi all, i had a regex question for the group:

i'm trying to do parse a string 'a = 3 b = 4 c = 5 d = 6'
[...]

any idea of how I can do this with regex?

my $str = 'a = 3 b = 4 c = 5 d = 6';
my %hash = $str =~ /(\w+)\s+=\s+(\d+)/g;

Anno
 
A

Anno Siegel

si said:
hi all, i had a regex question for the group:

i'm trying to do parse a string 'a = 3 b = 4 c = 5 d = 6'
[...]

any idea of how I can do this with regex?

my $str = 'a = 3 b = 4 c = 5 d = 6';
my %hash = $str =~ /(\w+)\s*=\s*(\d+)/g;

Anno
 
A

axel

Scott Bryce said:
my $string = 'a = 3 b = 4 c = 5 d = 6';
$string =~ s/=//g;

my %hash = split / */, $string;

This would not work as expected if a value such as 'e = 23' appeared.

my %hash = split / +/, $string;

might be better.

Axel
 
I

ioneabu

Anno said:
si said:
hi all, i had a regex question for the group:

i'm trying to do parse a string 'a = 3 b = 4 c = 5 d = 6'
[...]

any idea of how I can do this with regex?

my $str = 'a = 3 b = 4 c = 5 d = 6';
my %hash = $str =~ /(\w+)\s*=\s*(\d+)/g;

Anno

not as good but different:

#!/usr/bin/perl

use strict;
use warnings;

my $str = 'a = 3 b = 4 c = 5 d = 6';
$str =~ s/[^a-z0-9]+/ /g;
my %hash = split / /, $str;
print "$_ = $hash{$_}\n" for keys %hash;

wana
 
I

ioneabu

John said:
Is someone actually teaching people to use split() like this?
There must be a lot of bad examples out there on the web. :)
yes, the documentation that comes with Perl. From perlfunc:

split /PATTERN/,EXPR,LIMIT
split /PATTERN/,EXPR
split /PATTERN/

Both examples above fit the format. What is your problem with them?

wana
 
J

John W. Krahn

yes, the documentation that comes with Perl. From perlfunc:

split /PATTERN/,EXPR,LIMIT
split /PATTERN/,EXPR
split /PATTERN/

You missed one:

perldoc -f split
split /PATTERN/,EXPR,LIMIT
split /PATTERN/,EXPR
split /PATTERN/
split

Both examples above fit the format. What is your problem with them?

Do you know the difference between:

split;
split //, $_;
split / /, $_;
split / */, $_;
split / +/, $_;
split /\s/, $_;
split /\s*/, $_;
split /\s+/, $_;
split ' ', $_;



John
 
I

ioneabu

John said:
You missed one:

perldoc -f split
split /PATTERN/,EXPR,LIMIT
split /PATTERN/,EXPR
split /PATTERN/
split

You're right. But, there is no:

split EXPR

because it would be ambiguous, so if you don't want to use $_, you have
to specify a /PATTERN/.
them?

Do you know the difference between:

I think I do
split; #like split ' ', $_ which has special complex properties
split //, $_; #split into all individual characters
split / /, $_; #split on spaces
split / */, $_; #split on 0 or more spaces
split / +/, $_; #split on 1 or more spaces
split /\s/, $_; #split on single spaces
split /\s*/, $_; #split on 0 or more spaces
split /\s+/, $_; #split on 1 or more spaces
split ' ', $_; #split on space but with special properties (like awk,
refer to perldocs)
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top