Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Is there a better way than using $+ ?

Reply
Thread Tools

Is there a better way than using $+ ?

 
 
Neil Shadrach
Guest
Posts: n/a
 
      10-23-2003
Ok I worked out an answer in the process of putting the question together so it's not desperate
However a neater solution would be welcome if anyone has the enthusiasm.

#!/usr/bin/perl
use strict;
use warnings;

my $result;
foreach my $count (1..2)
{
my $s=qq(line $count goes first);
foreach my $p (qr/line . goes first/,qr/line (.) goes first/,qr/line (.) goes (\w+)/)
{
my @a;
print q(String="),$s,q(" Pattern="),$p,q(" Result=),$result++,q( match=),join(q(,@a),q( $+=),defined $+?"def":"not","\
n" if @a=$s=~$p;
}
}

The above script produces the following results.

String="line 1 goes first" Pattern="(?-xism:line . goes first)" Result=0 match=1 $+=not
String="line 1 goes first" Pattern="(?-xism:line (.) goes first)" Result=1 match=1 $+=def
String="line 1 goes first" Pattern="(?-xism:line (.) goes (\w+))" Result=2 match=1:first $+=def
String="line 2 goes first" Pattern="(?-xism:line . goes first)" Result=3 match=1 $+=not
String="line 2 goes first" Pattern="(?-xism:line (.) goes first)" Result=4 match=2 $+=def
String="line 2 goes first" Pattern="(?-xism:line (.) goes (\w+))" Result=5 match=2:first $+=def

I want to discriminate between result 0 and result 1.
In the first case match equals 1 because the match succeeded while in the second case it equals 1 because
that happens to be the string matched by the first () - that is @a always has at least one value in the
event of a match with no parenthesis in the pattern or one pair both resulting in exactly one value.
I want to call a function in the event of a match with an array of the captured () values.
I could use (defined $+)?@a)) but I'm wondering if I could have arranged it such that @a was empty in
the result 0 case.

Thanks

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      10-23-2003
Neil Shadrach <(E-Mail Removed)> writes:

> @a=$s=~$p;


> I could use (defined $+)?@a)) but I'm wondering if I could have
> arranged it such that @a was empty in the [ pattern has no captures
> ] case.


I don't think so.

But you should use $#+ not defined($+). Consider

@a = 'foo' =~ /foo|(bar)|(baz)/;

Here @a is correctly set to ( undef, undef ) not () but $+ is left
undefined.

 
Reply With Quote
 
 
 
 
Neil Shadrach
Guest
Posts: n/a
 
      10-24-2003
Brian McCauley wrote:

> But you should use $#+ not defined($+). Consider
>
> @a = 'foo' =~ /foo|(bar)|(baz)/;
>
> Here @a is correctly set to ( undef, undef ) not () but $+ is left
> undefined.


Ah. Thanks for that.


 
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
GL2 better than the XLs? Consumer grade HDs better than pro-sumer Mini DVs? dh@. DVD Video 1 08-28-2008 07:20 PM
Is splint really better than lint? Is there a better tool than splint? Peter Bencsik C Programming 2 09-21-2006 10:02 PM
Is there a better way to search CPAN than search.cpan.org? usenet@DavidFilmer.com Perl Misc 5 10-12-2005 04:50 AM
Is there a better way to delete the image from a Tkinter button other than the following: Casey Hawthorne Python 0 05-12-2005 03:44 PM
Is there a better way of listing Windows shares other than using"os.listdir" Doran_Dermot@emc.com Python 1 12-30-2004 05:39 PM



Advertisments