newbie question

T

torjon

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!
 
G

Gunnar Hjalmarsson

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;
 
J

Jürgen Exner

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

Joe Smith

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.
 

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,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top