Variable Assignment

What is the correct way to quickly assign the result of a regex against
a cmdline arg into a new variable:

my $var =3D ($ARGV[0] =3D~ s/(.*)foo/$1/i);

Obviously that's incorrect but is there a quick way without intermediate
assignment?

Thanks!
jlc

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
JCasale [ Di, 16 August 2011 20:27 ] [ ID #2063362 ]

Re: Variable Assignment

--20cf3005dd44da7cf104aaa3ba56
Content-Type: text/plain; charset=UTF-8

On Tue, Aug 16, 2011 at 8:27 PM, Joseph L. Casale <jcasale [at] activenetwerx.com
> wrote:

> What is the correct way to quickly assign the result of a regex against
> a cmdline arg into a new variable:
>
> my $var = ($ARGV[0] =~ s/(.*)foo/$1/i);
>
> Obviously that's incorrect but is there a quick way without intermediate
> assignment?
>
> Thanks!
> jlc
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
> For additional commands, e-mail: beginners-help [at] perl.org
> http://learn.perl.org/
>
>
>
Hi Joseph,

I would suggest naming your selectors like so: $ARGV[0] =~
s/(?'string'.*)foo/$1/i;

Now you can access this variable by simply using $+{string} as your
variable. That way you can identify your variables directly and assign them
meaningful names. Certainly in a more complex regular expressions with many
variables being retrieved this can be very useful. Of course the draw back
is that on the next regular expression this $+{<whatever>} will get reset
and you will loose what ever you retrieved similar to $1 etc...

Regards,

Rob

--20cf3005dd44da7cf104aaa3ba56--
Rob Coops [ Di, 16 August 2011 20:41 ] [ ID #2063363 ]

Re: Variable Assignment

From: "Joseph L. Casale" <jcasale [at] activenetwerx.com>
What is the correct way to quickly assign the result of a regex against
a cmdline arg into a new variable:

my $var =3D ($ARGV[0] =3D~ s/(.*)foo/$1/i);

Obviously that's incorrect but is there a quick way without intermediate
assignment?

Thanks!
jlc



Yes, you can use:

( my $var =3D $ARGV[0] ) =3D~ s/(.*)foo/$1/i;

Octavian


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Octavian Rasnita [ Di, 16 August 2011 20:44 ] [ ID #2063364 ]

RE: Variable Assignment

>Yes, you can use:
>
>( my $var =3D $ARGV[0] ) =3D~ s/(.*)foo/$1/i;

Rob/Octavian,
Thanks for the quick help!
jlc


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
JCasale [ Di, 16 August 2011 21:17 ] [ ID #2063365 ]

Re: Variable Assignment

Joseph L. Casale wrote:
> What is the correct way to quickly assign the result of a regex against
> a cmdline arg into a new variable:
>
> my $var = ($ARGV[0] =~ s/(.*)foo/$1/i);

my ( $var ) = $ARGV[0] =~ /(.*)foo/i;



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
jwkrahn [ Mi, 17 August 2011 03:46 ] [ ID #2063386 ]

Re: Variable Assignment

--90e6ba6e889e707cab04aaab4e88
Content-Type: text/plain; charset=ISO-8859-1

On Aug 16, 2011 9:48 PM, "John W. Krahn" <jwkrahn [at] shaw.ca> wrote:
>
> Joseph L. Casale wrote:
>>
>> What is the correct way to quickly assign the result of a regex against
>> a cmdline arg into a new variable:
>>
>> my $var = ($ARGV[0] =~ s/(.*)foo/$1/i);
>
>
> my ( $var ) = $ARGV[0] =~ /(.*)foo/i;

IIRC, that rederines $ARGV as well. I think (my $var = $ARGV) =~ /(.+)foo/;
might be better.

--90e6ba6e889e707cab04aaab4e88--
Shawn Wilson [ Mi, 17 August 2011 05:43 ] [ ID #2063387 ]

Re: Variable Assignment

At 11:43 PM -0400 8/16/11, shawn wilson wrote:
>On Aug 16, 2011 9:48 PM, "John W. Krahn" <jwkrahn [at] shaw.ca> wrote:
>>
>> Joseph L. Casale wrote:
>>>
>>> What is the correct way to quickly assign the result of a regex against
>>> a cmdline arg into a new variable:
>>>
>>> my $var = ($ARGV[0] =~ s/(.*)foo/$1/i);
>>
>>
>> my ( $var ) = $ARGV[0] =~ /(.*)foo/i;
>
>IIRC, that rederines $ARGV as well. I think (my $var = $ARGV) =~ /(.+)foo/;
>might be better.

The binding operator =~ does not modify the variable on its left-hand
side if the expression on the right-hand side is a regular
expression. The substitute operator (s///) does that. The only thing
modified are the various data elements associated with evaluating a
regular expression, including, in this instance, the global $1
variable.



--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Jim Gibson [ Mi, 17 August 2011 07:30 ] [ ID #2063388 ]

Re: Variable Assignment

From: "shawn wilson" <ag4ve.us [at] gmail.com>
> On Aug 16, 2011 9:48 PM, "John W. Krahn" <jwkrahn [at] shaw.ca> wrote:
>>
>> Joseph L. Casale wrote:
>>>
>>> What is the correct way to quickly assign the result of a regex =
against
>>> a cmdline arg into a new variable:
>>>
>>> my $var =3D ($ARGV[0] =3D~ s/(.*)foo/$1/i);
>>
>>
>> my ( $var ) =3D $ARGV[0] =3D~ /(.*)foo/i;
>
> IIRC, that rederines $ARGV as well. I think (my $var =3D $ARGV) =3D~ =
/(.+)foo/;
> might be better.


The code above doesn't use s/// but just m//, so it doesn't replace, but =
just match (and the 'm' is missing as usually).

I have shown how can s/// can be used without changing the original =
variable, but in this case it could be more clear and preferable to just =
extract the part you want from $ARGV[0] without using s///.

Octavian




--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Octavian Rasnita [ Mi, 17 August 2011 07:59 ] [ ID #2063389 ]
Perl » gmane.comp.lang.perl.beginners » Variable Assignment

Vorheriges Thema: Sorting a String
Nächstes Thema: How can I install a perl module without a root authority?