Variable Assignment

Variable Assignment

am 16.08.2011 20:27:52 von JCasale

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@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Variable Assignment

am 16.08.2011 20:41:18 von Rob Coops

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

On Tue, Aug 16, 2011 at 8:27 PM, 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);
>
> Obviously that's incorrect but is there a quick way without intermediate
> assignment?
>
> Thanks!
> jlc
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@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 $+{} will get reset
and you will loose what ever you retrieved similar to $1 etc...

Regards,

Rob

--20cf3005dd44da7cf104aaa3ba56--

Re: Variable Assignment

am 16.08.2011 20:44:23 von Octavian Rasnita

From: "Joseph L. Casale"
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@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

RE: Variable Assignment

am 16.08.2011 21:17:58 von JCasale

>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@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Variable Assignment

am 17.08.2011 03:46:42 von jwkrahn

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@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Variable Assignment

am 17.08.2011 05:43:58 von Shawn Wilson

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

On Aug 16, 2011 9:48 PM, "John W. Krahn" 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--

Re: Variable Assignment

am 17.08.2011 07:30:12 von Jim Gibson

At 11:43 PM -0400 8/16/11, shawn wilson wrote:
>On Aug 16, 2011 9:48 PM, "John W. Krahn" 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@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Variable Assignment

am 17.08.2011 07:59:26 von Octavian Rasnita

From: "shawn wilson"
> On Aug 16, 2011 9:48 PM, "John W. Krahn" 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;
>=20
> 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@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/