Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Is there a perl array for...

Reply
Thread Tools

Is there a perl array for...

 
 
advice please wireless 802.11 on RH8
Guest
Posts: n/a
 
      03-09-2005
Is there some perl system array @? that has the values for ( $1 $2 $3
.... ) ? I don't see any in Camel.

BB

 
Reply With Quote
 
 
 
 
Sherm Pendley
Guest
Posts: n/a
 
      03-09-2005
advice please wireless 802.11 on RH8 wrote:

> Is there some perl system array @? that has the values for ( $1 $2 $3
> ... ) ? I don't see any in Camel.


None that I know of, but you can easily assign the result of a match to an
array of your own:

my $string = 'foo:bar';
my @values = ($string =~ /(\w+)\w+)/);

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      03-09-2005
Sherm Pendley <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> advice please wireless 802.11 on RH8 wrote:
>
> > Is there some perl system array @? that has the values for ( $1 $2 $3
> > ... ) ? I don't see any in Camel.

>
> None that I know of, but you can easily assign the result of a match to an
> array of your own:
>
> my $string = 'foo:bar';
> my @values = ($string =~ /(\w+)\w+)/);


Also, the OP must have overlooked @- and @+. They don't store the
captures, but their positions. perldoc perlvar.

Anno
 
Reply With Quote
 
advice please wireless 802.11 on RH8
Guest
Posts: n/a
 
      03-15-2005

mike wrote:
> I think the OP may mean $1 $2 $3 as in command line arguments, rather
> than captured matches. (He refers to a "system array.") If so,

@ARGV
> is what you're looking for. @ARGV has any arguments as entered on

the
> command line.


No, I meant an array to contain matches such as for:

$_ = 'cat 12 Felix 17 Anytown NY';

print "yes!"\n" if /^(cat|dog)\s+(\d+)\s+(\w+)\s+(\d+)/;
# this mystery array would now contain qw(cat 12 Felix 17)

---------------------------------------

It seems to me this would be common enough to store as a system array
somewhere? By system array I mean one like @_ or @ARGV, etc.. ,
something created for us by Perl...


BB

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      03-15-2005
advice please wireless 802.11 on RH8 <(E-Mail Removed)> wrote:


> I meant an array to contain matches such as for:
>
> $_ = 'cat 12 Felix 17 Anytown NY';
>
> print "yes!"\n" if /^(cat|dog)\s+(\d+)\s+(\w+)\s+(\d+)/;

^ ^ ^
^ ^ ^

You should post real code if you want a real answer...


> # this mystery array would now contain qw(cat 12 Felix 17)



print "yes!\n" if my @mystery = /^(cat|dog)\s+(\d+)\s+(\w+)\s+(\d+)/;


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      03-15-2005
advice please wireless 802.11 on RH8 wrote:

> No, I meant an array to contain matches such as for:
>
> $_ = 'cat 12 Felix 17 Anytown NY';
>
> print "yes!"\n" if /^(cat|dog)\s+(\d+)\s+(\w+)\s+(\d+)/;
> # this mystery array would now contain qw(cat 12 Felix 17)
>
> ---------------------------------------
>
> It seems to me this would be common enough to store as a system array
> somewhere? By system array I mean one like @_ or @ARGV, etc.. ,
> something created for us by Perl...


Like I said before, just assign the results of the comparison to any array
you like:

#!/usr/bin/perl

use warnings;
use strict;

my @animals = ('cat 12 Felix', 'horse 20 Ed');

foreach (@animals) {
if (my @matches = /^(cat|dog)\s+(\d+)\s+(\w+)/) {
print join(',', @matches), "\n";
}
if (my ($species, $age, $name) = /^(cat|dog)\s+(\d+)\s+(\w+)/) {
print "Name=$name, age=$age, species=$species\n";
}
}

Why are you so insistent on a built-in array, anyway? I can understand being
lazy, but c'mon - is typing "my @matches = " such a hardship???

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
advice please wireless 802.11 on RH8
Guest
Posts: n/a
 
      03-17-2005

Sherm Pendley wrote:
> advice please wireless 802.11 on RH8 wrote:
>
> > No, I meant an array to contain matches such as for:
> >
> > $_ = 'cat 12 Felix 17 Anytown NY';
> >
> > print "yes!"\n" if /^(cat|dog)\s+(\d+)\s+(\w+)\s+(\d+)/;
> > # this mystery array would now contain qw(cat 12 Felix 17)
> >
> > ---------------------------------------
> >
> > It seems to me this would be common enough to store as a system

array
> > somewhere? By system array I mean one like @_ or @ARGV, etc.. ,
> > something created for us by Perl...

>
> Like I said before, just assign the results of the comparison to any

array
> you like:
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> my @animals = ('cat 12 Felix', 'horse 20 Ed');
>
> foreach (@animals) {
> if (my @matches = /^(cat|dog)\s+(\d+)\s+(\w+)/) {
> print join(',', @matches), "\n";
> }
> if (my ($species, $age, $name) =

/^(cat|dog)\s+(\d+)\s+(\w+)/) {
> print "Name=$name, age=$age, species=$species\n";
> }
> }

Sherm Pendley wrote:

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      03-18-2005
advice please wireless 802.11 on RH8 wrote:
> It also seemed inconsistent that whenever Perl "can" capture arrays or
> hashes, i.e. %ENV, @_, @ARGV, etc, it seems to like to do so. Here is a
> case where it would be perfectly natural to capture $1 $2 $3 ... as an
> array and it doesn't.


Yes it does seem odd. But no, there is no such array, the best you have
is @+ or @-.

 
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
Is there any difference between Array() and new Array() Xu, Qian Javascript 2 03-16-2008 03:57 AM
How to check if there is an existing entry in array of the hash in perl..please, please Cyrus Perl Misc 1 12-20-2006 09:20 PM
is there a good way to change between int array and byte array ? Baby Lion Java 2 10-02-2006 01:45 AM
Are there any other GUI toolkits for perl than perl/Tk? ngoc Perl Misc 7 07-14-2005 01:22 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments