Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > newbie question

Reply
Thread Tools

newbie question

 
 
torjon@NOSPAMnterport.net
Guest
Posts: n/a
 
      08-13-2004
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!
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-13-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      08-14-2004
(E-Mail Removed) wrote:
> 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


 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      08-14-2004
(E-Mail Removed) wrote:

> 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.
 
Reply With Quote
 
torjon@NOSPAMnterport.net
Guest
Posts: n/a
 
      08-14-2004
ahh, you can split without a delim, perfect.

Thans for all your help
>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;
>
>--
>Gunnar Hjalmarsson
>Email: http://www.gunnar.cc/cgi-bin/contact.pl


 
Reply With Quote
 
torjon@NOSPAMnterport.net
Guest
Posts: n/a
 
      08-14-2004
Thanks!
 
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
VONAGE Newbie w/newbie question New_kid@nowhere.new VOIP 0 08-11-2007 01:40 PM
another newbie question from another newbie.... Lee UK VOIP 4 05-17-2005 04:10 PM
newbie: cisco vlan newbie question No Spam Cisco 3 06-07-2004 10:02 AM
dumb newbie question (or newbie dumb question) Jerry C. Perl Misc 8 11-23-2003 04:11 AM
Newbie! I'm a newbie! What's wrong with this program? Id0x Python 4 07-20-2003 11:40 PM



Advertisments