assign s/// value to a scalar

2010-03-11 Thread raphael()
#!/usr/bin/env perl

use strict;
use warnings;

my @array = qw (
http://abc.com/files/randomthings/A/1.html
http://abc.com/files/randomthings/A/2.html
);

for ( @array ) {

#   This works
#   s!/A/\d+.html$!!; $url = $_;

#   Doesn't work ~ gives 1
( my $url ) = ( $_ ) =~ s!/A/\d+.html$!!;
print $url . \n;

}

__END__

I want to remove the '/A/1.html' and assign the remaining value i.e. link to
$url
But all I get is 1 as value of $url which I think is return value that
substitution worked.

How can I remove '/A/1.html' and assign remaining $_ to $url?


Re: assign s/// value to a scalar

2010-03-11 Thread John W. Krahn

raphael() wrote:

#!/usr/bin/env perl

use strict;
use warnings;

my @array = qw (
http://abc.com/files/randomthings/A/1.html
http://abc.com/files/randomthings/A/2.html
);

for ( @array ) {

#   This works
#   s!/A/\d+.html$!!; $url = $_;


Not quite, that should be:

  s!/A/\d+\.html$!!; $url = $_;

Unless the . character is escaped it will match *any* character.



#   Doesn't work ~ gives 1
( my $url ) = ( $_ ) =~ s!/A/\d+.html$!!;
print $url . \n;

}

__END__

I want to remove the '/A/1.html'


That means that you want to modify @array?  Do you really need to?



and assign the remaining value i.e. link to
$url
But all I get is 1 as value of $url which I think is return value that
substitution worked.

How can I remove '/A/1.html' and assign remaining $_ to $url?


If you just want to assign everything before '/A/1.html' to $url then:

( my $url ) = m!(.*)/A/\d+\.html$!;

If you really need to modify @array then go with your commented out code 
at the top of the loop.




John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.   -- Damian Conway

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: assign s/// value to a scalar

2010-03-11 Thread Steve Bertrand
On 2010.03.12 00:33, John W. Krahn wrote:
 raphael() wrote:
 #!/usr/bin/env perl

 use strict;
 use warnings;

 my @array = qw (
 http://abc.com/files/randomthings/A/1.html
 http://abc.com/files/randomthings/A/2.html
 );

 for ( @array ) {

 #   This works
 #   s!/A/\d+.html$!!; $url = $_;
 
 Not quite, that should be:
 
   s!/A/\d+\.html$!!; $url = $_;
 
 Unless the . character is escaped it will match *any* character.
 
 
 #   Doesn't work ~ gives 1
 ( my $url ) = ( $_ ) =~ s!/A/\d+.html$!!;
 print $url . \n;

 }

 __END__

 I want to remove the '/A/1.html'
 
 That means that you want to modify @array?  Do you really need to?
 
 
 and assign the remaining value i.e. link to
 $url
 But all I get is 1 as value of $url which I think is return value
 that
 substitution worked.

 How can I remove '/A/1.html' and assign remaining $_ to $url?
 
 If you just want to assign everything before '/A/1.html' to $url then:
 
 ( my $url ) = m!(.*)/A/\d+\.html$!;
 
 If you really need to modify @array then go with your commented out code
 at the top of the loop.

I applaud the OP for his question ;)

After I changed some of the sample by removing the use of $_, there were
certain circumstances where having the parens were necessary, and other
times not.

Am I correct in thinking that this:

$url = $file =~ m{ (.*) /A/\d+.html }x;

...assigns '1' to $url because =~ binds tighter and assigns a 'true'
value to $url, whereas:

( $url ) = $file =~ m{ (.*) /A/\d+.html }x;

...$url here is evaluated first, and assigned the actual string
afterwards? iow, is it simply an arithmetic thing, that can also be seen
as this:?

( $url ) = ( $file =~ m! (.*) /A/\d+.html !x );

D'oh! I just answered my own question. Learn something new everyday,
even though it's a principle that I've known for years, but just didn't
apply it...

   ( my $this ) = ( ( $url ) = ( $file =~ m! (.*) /A/\d+.html !x ) );
print $url :: $this\n;

Steve

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: assign s/// value to a scalar

2010-03-11 Thread Uri Guttman
 SB == Steve Bertrand st...@ibctech.ca writes:

  SB Am I correct in thinking that this:

  SB $url = $file =~ m{ (.*) /A/\d+.html }x;

  SB ...assigns '1' to $url because =~ binds tighter and assigns a 'true'
  SB value to $url, whereas:

  SB ( $url ) = $file =~ m{ (.*) /A/\d+.html }x;

  SB ...$url here is evaluated first, and assigned the actual string
  SB afterwards? iow, is it simply an arithmetic thing, that can also be seen
  SB as this:?

nope. it is scalar vs list context, not precedence.

the () makes the assignment to $url happen in list context. the m// op
in list context will return the list of grabs (with some variations
based on the /g modifier). in scalar context m// only returns a boolean
if the match succeeded or not.

  SB ( $url ) = ( $file =~ m! (.*) /A/\d+.html !x );

  SB D'oh! I just answered my own question. Learn something new everyday,
  SB even though it's a principle that I've known for years, but just didn't
  SB apply it...

that didn't change anything. =~ binds tighter than = anyway. it is still
context that does it.

  SB( my $this ) = ( ( $url ) = ( $file =~ m! (.*) /A/\d+.html !x ) );
  SB print $url :: $this\n;

that shouldn't make any difference to assigning $url in list context.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/