tags:

views:

82

answers:

2

The following script gives me what I want but Perl also throws me a warning saying "Useless use of a variable in void context". What does it mean?

use strict;
use warnings;

my $example = 'http\u003a//main\u002egslb\u002eku6\u002ecom/c0/q7LmJPfV4DfXeTYf/1260269522170/93456c39545857a15244971e35fba83a/1279582254980/v632/6/28/a14UAJ0CeSyi3UTEvBUyMuBxg\u002ef4v\u002chttp\u003a//main\u002egslb\u002eku6\u002ecom/c1/q7LmJPfV4DfXeTYf/1260269522170/3cb143612a0050335c0d44077a869fc0/1279582254980/v642/10/20/7xo2MJ4tTtiiTOUjEpCJaByg\u002ef4v\u002chttp\u003a//main\u002egslb\u002eku6\u002ecom/c2/q7LmJPfV4DfXeTYf/1260269522170/799955b45c8c32c955564ff9bc3259ea/1279582254980/v652/32/4/6pzkCf4iqTSUVElUA5A3PpMAoA\u002ef4v\u002chttp\u003a//main\u002egslb\u002eku6\u002ecom/c3/q7LmJPfV4DfXeTYf/1260269522170/cebbb619dc61b3eabcdb839d4c2a4402/1279582254980/v567/36/19/MBcbnWwkSJu46UoYCabpvArA\u002ef4v\u002chttp\u003a//main\u002egslb\u002eku6\u002ecom/c4/q7LmJPfV4DfXeTYf/1260269522170/1365c39355424974dbbe4ae8950f0e73/1279582254980/v575/17/15/EDczAa0GTjuhppapCLFjtaQ\u002ef4v';

my @raw_url = $example =~ m{(http\\u003a.+?f4v)}g;

my @processed_url =   map {
                             s{\\u003a}{:}g,$_; 
                             s{\\u002e}{.}g,$_;
                             s{\\u002d}{@}g,$_;
  } @raw_url;

print join("\n",@processed_url);

And why this map thing doesn't work if I omit those dollar underscores like so?

 my @processed_url =   map {
                             s{\\u003a}{:}g; 
                             s{\\u002e}{.}g;
                             s{\\u002d}{@}g;
  } @raw_url;

When I omit those dollar underscores, I get nothing except for a possibly success flag "1". What am I missing? Any ideas? Thanks like always :)

+10  A: 

What you want is...

my @processed_url =   map {
                       s{\\u003a}{:}g; 
                       s{\\u002e}{.}g;
                       s{\\u002d}{@}g;
                       $_;
} @raw_url;

A map block returns the value composed of the last statement evaluated as its result. Thats why we pass the $_ as the last statement. The substitution operator s{}{} returns the number of substitutions made.

In your prior setup, you had by itself the following statement. Which is pretty much meaningless and that is what Perl is warning about.

s{\\u003a}{:}g, $_;
Danny
@Danny, thanks a lot for the clarification! Thanks :)
Mike
For prettier source, you can use Filter() from Algorithm::Loops and omit the $_
ysth
+6  A: 

You already have the answer you were looking for, but I wanted to point out a subtlety about using the substitution operator inside a map block: your original array is also being modified. If you want to preserve the original array, one way to do it is to make a copy of the array, then modify only the copy:

my @processed_url = @raw_url;
for (@processed_url) {
    s{\\u003a}{:}g;  
    s{\\u002e}{.}g; 
    s{\\u002d}{@}g;
}

Or, if you only need one array, and you want the original to be modified:

for (@raw_url) {
    s{\\u003a}{:}g;  
    s{\\u002e}{.}g; 
    s{\\u002d}{@}g;
}
toolic
@toolic, thanks for the input:) I originally used push and it worked but then I tried map but there was problem and I couldn't figure it out on my own. my original script was like this: for (@raw_url) { s{\\u003a}{:}g; s{\\u002e}{.}g; s{\\u002d}{@}g; push @processed_url,$_;}
Mike