[vox-tech] dumb perl question

Jeff Newmiller vox-tech@lists.lugod.org
Sat, 12 Jan 2002 18:59:00 -0800 (PST)


On Sat, 12 Jan 2002, Peter Jay Salzman wrote:

> 
> i know i should know this by now...
>
> how does one get assignments out of a backreference from a regex match?

By evaluating the match in a list context.  See "man perlop" under "Regexp
Quote-Like Operators".

Try:

   print join( ":", "100.extra" =~ /(\d+)\.(.*)/), "\n";

What you do with the resulting list is up to you, and if you only put one
pair of parentheses in the match, your list will only include one item.
When you join the list into a scalar, it will "concatenate" all "one" 
of the list items together, so

  $_ = "100.extra";

  $num = join '', /(\d+)/;

pulls the first sequence of one or more digits out of the string and puts
it into $num.

As far as I know, creating a list context means the left hand side of the
assignment must be a list, or you have to call a function that expects a
list (such as join).

[...]

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil@dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...2k
---------------------------------------------------------------------------