Is there a perl array for...

  • Thread starter advice please wireless 802.11 on RH8
  • Start date
A

advice please wireless 802.11 on RH8

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

BB
 
S

Sherm Pendley

advice said:
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--
 
A

Anno Siegel

Sherm Pendley said:
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
 
A

advice please wireless 802.11 on RH8

mike said:
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
 
T

Tad McClellan

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+)/;
 
S

Sherm Pendley

advice said:
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--
 
A

advice please wireless 802.11 on RH8

Sherm said:
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:
 
B

Brian McCauley

advice said:
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 @-.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top