On Jan 4, Prahlad Vaidyanathan said:

>I tried this :
>
>my $text = "       this is a test" ;
>
># Test 1
>my $leading_spaces = ($text =~ m/^(\s+)/) ; # This doesn't work
>print $leading_spaces ;                     # Prints 1

You have executed the regex in scalar context.  Why?  Because the
left-hand side of the = is a single value.  You can make this into a
one-element list by using parentheses:

  my ($leading_spaces) = $text =~ /^(\s+)/;

># Test 2
>print ($text =~ m/^(\s+)/) ;                # this prints the spaces

print() takes a list of values, so the regex is executed in list context
already.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to