Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How these $1 and $2 is decided?

Reply
Thread Tools

How these $1 and $2 is decided?

 
 
yl
Guest
Posts: n/a
 
      11-02-2005
Could anyone help explain this line of code?
$_->[/^(?>(.)?(.)+.*)(?!\1)(??{print(($1..$2)[15,4,17,11])})/]

I think it can be divided into 3 parts
1. (?>(.)?(.)+.*)
2. (?!\1)
3. (??{print(($1..$2)[15,4,17,11])})
and the 3rd part will print 'PERL'. But I got no idea how the variables
$1 and $2 was decided to form an array including 'A'..'Z' (though $2 is
not 'Z').

Another question is that $_ is undef at program start, how could it
become an array reference?

Thanks.

 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      11-02-2005
yl wrote:
> Could anyone help explain this line of code?
> $_->[/^(?>(.)?(.)+.*)(?!\1)(??{print(($1..$2)[15,4,17,11])})/]
>
> I think it can be divided into 3 parts
> 1. (?>(.)?(.)+.*)
> 2. (?!\1)
> 3. (??{print(($1..$2)[15,4,17,11])})
> and the 3rd part will print 'PERL'. But I got no idea how the variables
> $1 and $2 was decided to form an array including 'A'..'Z' (though $2 is
> not 'Z').


$_->[] autovivifies an array reference so $_ becomes a text string something
like ARRAY(0x1234567 which the regex operates on. ^(?>(.)?(.)+.*)(?!\1)
puts 'A' in $1 and 'R' in $2 giving the list 'A' .. 'R' from which the letters
'P', 'E', 'R' and 'L' are sliced.

> Another question is that $_ is undef at program start, how could it
> become an array reference?


Autovivification.


John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
 
yl
Guest
Posts: n/a
 
      11-02-2005

John W. Krahn 寫道:

>
> $_->[] autovivifies an array reference so $_ becomes a text string something
> like ARRAY(0x1234567 which the regex operates on. ^(?>(.)?(.)+.*)(?!\1)


The $_->[/regex/] thing looks quite strange to me. I never thought a
regex can be placed in a bracket like this. Is it a useful expression
that I can use in many situations or just for demonstration of an
advanced Perl skill?

Thanks

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      11-02-2005
yl wrote:
> John W. Krahn 寫道:
>
>>$_->[] autovivifies an array reference so $_ becomes a text string something
>>like ARRAY(0x1234567 which the regex operates on. ^(?>(.)?(.)+.*)(?!\1)

>
> The $_->[/regex/] thing looks quite strange to me. I never thought a
> regex can be placed in a bracket like this. Is it a useful expression
> that I can use in many situations or just for demonstration of an
> advanced Perl skill?


In the example the regex wasn't used for its return value but for the side
effect of the print function.

An example of a useful use of a regex in an array slice:

my @array = 'a' .. 'z';

$_ = ' 14 1 5 20 18 2 0 19 4 ';

print @a[ /\d+/g ], "\n";



John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Ilya Zakharevich
Guest
Posts: n/a
 
      11-02-2005
[A complimentary Cc of this posting was sent to
John W. Krahn
<(E-Mail Removed)>], who wrote in article <FLX9f.73947$S4.15830@edtnps84>:
> yl wrote:
> > Could anyone help explain this line of code?
> > $_->[/^(?>(.)?(.)+.*)(?!\1)(??{print(($1..$2)[15,4,17,11])})/]
> >
> > I think it can be divided into 3 parts
> > 1. (?>(.)?(.)+.*)
> > 2. (?!\1)
> > 3. (??{print(($1..$2)[15,4,17,11])})
> > and the 3rd part will print 'PERL'. But I got no idea how the variables
> > $1 and $2 was decided to form an array including 'A'..'Z' (though $2 is
> > not 'Z').

>
> $_->[] autovivifies an array reference so $_ becomes a text string something
> like ARRAY(0x1234567 which the regex operates on. ^(?>(.)?(.)+.*)(?!\1)
> puts 'A' in $1 and 'R' in $2 giving the list 'A' .. 'R' from which the letters


Are you sure? Actually, $2 is ')'.

perl -wle "[1] =~ /^(?>(.)?(.)+.*)(?!\1)/ and print $2"
)

Looks like the .. operator (as opposed to ++ !!!) has alphanumeric
*output*, but arbitrary *input*: here 'A' .. ')' is the same as 'A' .. 'Z'.
Probably won't be so on EBCDIC.

Hope this helps,
Ilya
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      11-02-2005
John W. Krahn <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> yl wrote:
> > John W. Krahn 寫道:
> >
> >>$_->[] autovivifies an array reference so $_ becomes a text string something
> >>like ARRAY(0x1234567 which the regex operates on. ^(?>(.)?(.)+.*)(?!\1)

> >
> > The $_->[/regex/] thing looks quite strange to me. I never thought a
> > regex can be placed in a bracket like this. Is it a useful expression
> > that I can use in many situations or just for demonstration of an
> > advanced Perl skill?

>
> In the example the regex wasn't used for its return value but for the side
> effect of the print function.
>
> An example of a useful use of a regex in an array slice:
>
> my @array = 'a' .. 'z';

^^^^^
>
> $_ = ' 14 1 5 20 18 2 0 19 4 ';
>
> print @a[ /\d+/g ], "\n";

^
Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      11-02-2005
Anno Siegel wrote:
> John W. Krahn <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>>yl wrote:
>>>John W. Krahn 寫道:
>>>
>>>>$_->[] autovivifies an array reference so $_ becomes a text string something
>>>>like ARRAY(0x1234567 which the regex operates on. ^(?>(.)?(.)+.*)(?!\1)
>>>The $_->[/regex/] thing looks quite strange to me. I never thought a
>>>regex can be placed in a bracket like this. Is it a useful expression
>>>that I can use in many situations or just for demonstration of an
>>>advanced Perl skill?

>>In the example the regex wasn't used for its return value but for the side
>>effect of the print function.
>>
>>An example of a useful use of a regex in an array slice:
>>
>>my @array = 'a' .. 'z';

> ^^^^^
>>$_ = ' 14 1 5 20 18 2 0 19 4 ';
>>
>>print @a[ /\d+/g ], "\n";

> ^


Oops, sorry, I copied and pasted the wrong code, it should be:

my @array = 'a' .. 'z';

$_ = ' 14 1 5 20 18 2 0 19 4 ';

print @array[ /\d+/g ], "\n";




John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      11-02-2005
Ilya Zakharevich wrote:
> [A complimentary Cc of this posting was sent to
> John W. Krahn
> <(E-Mail Removed)>], who wrote in article <FLX9f.73947$S4.15830@edtnps84>:
>>yl wrote:
>>>Could anyone help explain this line of code?
>>>$_->[/^(?>(.)?(.)+.*)(?!\1)(??{print(($1..$2)[15,4,17,11])})/]
>>>
>>>I think it can be divided into 3 parts
>>>1. (?>(.)?(.)+.*)
>>>2. (?!\1)
>>>3. (??{print(($1..$2)[15,4,17,11])})
>>>and the 3rd part will print 'PERL'. But I got no idea how the variables
>>>$1 and $2 was decided to form an array including 'A'..'Z' (though $2 is
>>>not 'Z').

>>$_->[] autovivifies an array reference so $_ becomes a text string something
>>like ARRAY(0x1234567 which the regex operates on. ^(?>(.)?(.)+.*)(?!\1)
>>puts 'A' in $1 and 'R' in $2 giving the list 'A' .. 'R' from which the letters

>
> Are you sure? Actually, $2 is ')'.
>
> perl -wle "[1] =~ /^(?>(.)?(.)+.*)(?!\1)/ and print $2"
> )
>
> Looks like the .. operator (as opposed to ++ !!!) has alphanumeric
> *output*, but arbitrary *input*: here 'A' .. ')' is the same as 'A' .. 'Z'.
> Probably won't be so on EBCDIC.


That is weird. According to perlop: "If the left value is greater than the
right value then it returns the empty list.". And 'A' gt ')' (at least in
ASCII) so it should be an empty list.


John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-02-2005
yl <(E-Mail Removed)> wrote:

> The $_->[/regex/] thing looks quite strange to me. I never thought a
> regex can be placed in a bracket like this.



It is nothing more than a m// in a list context.


> Is it a useful expression
> that I can use in many situations



Yes, m// in a list context is often useful.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Vronans
Guest
Posts: n/a
 
      11-02-2005
John W. Krahn wrote:
> Anno Siegel wrote:
>> John W. Krahn <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>>> yl wrote:
>>>> John W. Krahn 寫道:
>>>>
>>>>> $_->[] autovivifies an array reference so $_ becomes a text
>>>>> string something like ARRAY(0x1234567 which the regex operates
>>>>> on. ^(?>(.)?(.)+.*)(?!\1)
>>>> The $_->[/regex/] thing looks quite strange to me. I never thought
>>>> a regex can be placed in a bracket like this. Is it a useful
>>>> expression that I can use in many situations or just for
>>>> demonstration of an advanced Perl skill?
>>> In the example the regex wasn't used for its return value but for
>>> the side effect of the print function.
>>>
>>> An example of a useful use of a regex in an array slice:
>>>
>>> my @array = 'a' .. 'z';

>> ^^^^^
>>> $_ = ' 14 1 5 20 18 2 0 19 4 ';
>>>
>>> print @a[ /\d+/g ], "\n";

>> ^

>
> Oops, sorry, I copied and pasted the wrong code, it should be:
>
> my @array = 'a' .. 'z';
>
> $_ = ' 14 1 5 20 18 2 0 19 4 ';
>
> print @array[ /\d+/g ], "\n";


Even though it wasn't very difficult (for some of us) to mentally
translate it anyway


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
dynamically add new rows and save data typed in these rows to a database Arjen Hoekstra ASP .Net 0 08-02-2005 12:15 PM
Is there a correlation between measureup % and these 700-900 numbers I'm seeing? Steve Smith MCSD 1 10-29-2004 02:43 PM
Re: Are these pictures too dark or/and too large? Luigi Donatello Asero HTML 0 05-21-2004 09:40 PM
Are these pictures too dark or/and too large? Luigi Donatello Asero HTML 13 05-21-2004 04:54 AM



Advertisments