Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Perl oddities

by Not_a_Number (Parson)
on Mar 01, 2005 at 19:36 UTC ( #435587=note: print w/ replies, xml ) Need Help??


in reply to Re: Perl oddities
in thread Perl oddities

I generally do as gaal suggests. However, if this were impossible, and if you had a long list of regex capture variables, you could always do:

   my ( $this, $that, $some, $other, $foo, $bar, $baz ) = ( $1 .. $+ );

dave


Comment on Re^2: Perl oddities
Download Code
Re^3: Perl oddities
by YuckFoo (Abbot) on Mar 01, 2005 at 20:19 UTC
    I am aware of the method suggested by gaal. I usually do not use it because I almost always wrap the regex up with an 'if'. Throw in a my function and it starts to get messy.
    # Ugly to me... if (my ($this, $that, $some, $other) = $line =~ /(this).*(that).*(some +).*(other)/) { do_it(); }; # Better, I think... if ($line =~ /(this).*(that).*(some).*(other)/) { my ($this, $that, $some, $other) = ($1, $2, $3, $4); do_it(); }
    This business doesn't DWYM:

    my ( $this, $that, $some, $other, $foo, $bar, $baz ) = ( $1 .. $+ );
    The magical string incrementer is summoned to build a list of strings starting with the first capture, ending with the last.

    YuckFoo

      You can do

      if (my @r = $line =~ /(this).*(that).*(some).*(other)/) { my ($this, $that, $some, $other) = @r; do_it(); }
      if you want the conditional expression shorter and don't want to repeat the dollar-digit vars.

      ihb

      See perltoc if you don't know which perldoc to read!

Re^3: Perl oddities
by Anonymous Monk on Mar 02, 2005 at 13:47 UTC
    That doesn't work:
    #!/usr/bin/perl use strict; use warnings; $_ = "this that some other foo bar baz"; if (/(this)\s*(that)\s*(some)\s*(other)\s*(foo)\s*(bar)\s*(baz)/) { my ($this, $that, $some, $other, $foo, $bar, $baz) = ($1 .. $+); print "$this, $that, $some, $other, $foo, $bar, $baz\n"; } __END__ Use of uninitialized value in concatenation (.) or string at Use of uninitialized value in concatenation (.) or string at Use of uninitialized value in concatenation (.) or string at Use of uninitialized value in concatenation (.) or string at Use of uninitialized value in concatenation (.) or string at Use of uninitialized value in concatenation (.) or string at Use of uninitialized value in concatenation (.) or string at , , , , , ,
    What does work is:
    #!/usr/bin/perl use strict; use warnings; $_ = "this that some other foo bar baz"; if (/(this)\s*(that)\s*(some)\s*(other)\s*(foo)\s*(bar)\s*(baz)/) { my ($this, $that, $some, $other, $foo, $bar, $baz) = map {;no strict 'refs'; $$_} 1 .. $#-; print "$this, $that, $some, $other, $foo, $bar, $baz\n"; } __END__ this, that, some, other, foo, bar, baz
    A trick I've been using for quite a long time. ($#+ instead of $#- works as well).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://435587]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (15)
As of 2014-07-22 16:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (119 votes), past polls