On Fri, Oct 24, 2008 at 5:00 PM, Sharan Basappa <[EMAIL PROTECTED]>wrote:

> Hi,
>
> I was just trying to match a string and save it in a single statement
> as follows:
>
> $extracted = "cp xyz";
> $state_var = $extracted =~ m/cp\s+(.*)/;
> print "$state_var $1 \n";
>
> The output is: 1 xyz
>
> So the assignment to $state_var does not work. Is this an incorrect way.
>
> Regards
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> http://learn.perl.org/
>
>
>

If it doesn't work I guess it is a wrong way. :-)

What you are doing is this:
Try and match $extracted to /cp\s+(.*)/ (the result bing true or false) and
then asiging the result to $state_var (true in your example als so written
as 1.

The actual matched value (the stuff between the backets) is stored in $1 so
when you print $state_var and $1 you will see the output that you are indeed
getting.

What you want to do to get the matched string stored in $state_var is this:

$extracted = "cp xyz";
$extracted =~ m/cp\s+(.*)/;
$state_var = $1;
print "$state_var\n";
Rob.

Reply via email to