siegfr...@heintze.com wrote:
Darn -- I forgot to switch to plain text again. I hope this does not
appear twice -- I apologize if it does!

This works and produces the desired result (I've simplified it a bit):

$default= ((((`grep pat file-name`)[0])=~/[0-9]+/)[0]);

I think you mean either:

$default= ((((`grep pat file-name`)[0])=~/[0-9]+/g)[0]);

Or:

$default= ((`grep pat file-name`)[0])=~/[0-9]/;

Because the match operator /[0-9]+/ will only return true or false in either list or scalar context

$ perl -le'print "List context: ", "5" =~ /[0-9]+/'
List context: 1
$ perl -le'print "Scalar context: ", scalar( "5" =~ /[0-9]+/ )'
Scalar context: 1


Why does it take so many parentheses?

It doesn't, this will work as well:

$default = ( ( `grep pat file-name` )[ 0 ] =~ /[0-9]+/g )[ 0 ];

Or:

( $default ) = ( `grep pat file-name` )[ 0 ] =~ /[0-9]+/g;

Or even:

$default = ( `grep pat file-name` =~ /[0-9]+/g )[ 0 ];

Or:

( $default ) = `grep pat file-name` =~ /[0-9]+/g;



I don't think it should work, however.


(1) Why cannot I just index the results of the sub-process directly and
say `grep pat file-name`[0]?

Because `grep pat file-name` is not a list (or array).


If Perl is confused I would think I might
need to explicitly convert it like this:
   @{`grep pat file-name`}[0]
but that does not work. I think it should.

@{} dereferences an array reference but `grep pat file-name` is not an array reference.

You could copy the output from `grep pat file-name` to an array or anonymous array and index that:

[ `grep pat file-name` ]->[ 0 ]

But then you might as well just use a list slice in the first place.


(2) I have the same question about the =~ operator -- it returns an
array too.

The binding operators (=~ and !~) do not "return" anything, they just bind the expression on their left hand side to the operator on the right hand side. It is the operator on the right hand side which returns something. The binding operators can bind to either the match operator (m//) or the substitution operator (s///) or the transliteration operator (tr/// or y///).


So why cannot I just type
print @{$a=~/([0-9]+)/}[0] ?

Again, @{} dereferences an array reference and $a=~/([0-9]+)/ is not an array reference.


Instead I have to type
  print (($a=~/([0-9]+/)[0]);


Why are the extra outer parens required?

perldoc -f print

     Also be careful not to follow the print keyword with a left
     parenthesis unless you want the corresponding right parenthesis to
     terminate the arguments to the print--interpose a "+" or put
     parentheses around all the arguments.

In other words:

print ($a=~/([0-9]+/)[0];

Is just:

print($a=~/([0-9]+/);

Followed by a syntax error.



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


Reply via email to