Is there a perl array for...

Discussion in 'Perl Misc' started by advice please wireless 802.11 on RH8, Mar 9, 2005.

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

    BB
    advice please wireless 802.11 on RH8, Mar 9, 2005
    #1
    1. Advertising

  2. 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
    Sherm Pendley, Mar 9, 2005
    #2
    1. Advertising

  3. advice please wireless 802.11 on RH8

    Anno Siegel Guest

    Sherm Pendley <> 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
    Anno Siegel, Mar 9, 2005
    #3
  4. 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
    advice please wireless 802.11 on RH8, Mar 15, 2005
    #4
  5. advice please wireless 802.11 on RH8 <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 15, 2005
    #5
  6. 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
    Sherm Pendley, Mar 15, 2005
    #6
  7. 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:
    advice please wireless 802.11 on RH8, Mar 17, 2005
    #7
  8. 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 @-.
    Brian McCauley, Mar 18, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,792
  2. Baby Lion
    Replies:
    2
    Views:
    398
    Baby Lion
    Oct 2, 2006
  3. ngoc
    Replies:
    7
    Views:
    171
    zentara
    Jul 14, 2005
  4. Cyrus
    Replies:
    1
    Views:
    108
    Cyrus
    Dec 20, 2006
  5. Xu, Qian
    Replies:
    2
    Views:
    77
Loading...

Share This Page