Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Motherfuckin' Perl

Name: Anonymous 2012-07-13 15:44


$thingy = "asdf1.jpg";
$num = $thingy =~ s/.*/asdf/;
print $num;


Why the fuck is this outputting a "1" instead of "asdf"?

Name: Anonymous 2012-07-13 16:08

And now you have two problems!

Name: Anonymous 2012-07-13 16:22

You answered your own question. See:
Why the fuck is this outputting a "1" instead of "asdf"?
Motherfuckin' Perl

Name: Anonymous 2012-07-13 16:25

Obviously do:
$thingy = "asdf1.jpg";
$thingy =~ s/.*/asdf/;
$num = $thingy;
print $num;


Keep in mind that =~ is destructive, and changes the value of the left operand. What you assign to $num in your code is a value that indicates the success of the regex-replacement (in this case a true value, 1, because it indeed found something to replace). I recommend the book "Learning Perl" (O'Reilly) if you are interested in learning Perl.

Name: Anonymous 2012-07-13 16:30

it's not good practice to chain shit in any language.

Name: Anonymous 2012-07-13 16:42

$x =~ s/.*/asdf/ doesn't even need to be regex. It is exactly the same as $x = "asdf", only slower.

Name: Anonymous 2012-07-13 16:57

Because in scalar context =~ returns the number of matches. In list context it returns matches themselves. So:
($num) = $thingy =~ s/.*/asdf/;

Name: Anonymous 2012-07-13 16:58

Oh, nevermind, my mistake.

Name: FFP 2012-07-13 16:58

because perl is retarded.

WYPMP

Name: Anonymous 2012-07-13 17:16

>>7
Oh, thank you.

Name: Anonymous 2012-07-13 17:35

>>9
No, FFP, you are the retards!

Name: Anonymous 2012-07-14 3:45

You can also use the /r flag, which performs the substitution on a copy a returns it. This works and leaves $thingy untouched:

$thingy = "asdf1.jpg";
$num = $thingy =~ s/.*/asdf/r;
print $num;

Name: Anonymous 2012-07-14 3:57

note that /r was added in perl 5.14, so it won't work if you're using something old

Name: Anonymous 2013-11-30 8:02

░░░░░░░▄▀▀▀▀▀▀▀▀▀▀▄▄░░░░░░░░░
░░░░▄▀▀░░░░░░░░░░░░░▀▄░░░░░░░
░░▄▀░░░░░░░░░░░░░░░░░░▀▄░░░░░ YOU HAVE BEEN VISITED BY
░░█░░░░░░░░░░░░░░░░░░░░░▀▄░░░ LE 'FEEL OF NO GF
░▐▌░░░░░░░░▄▄▄▄▄▄▄░░░░░░░▐▌░░
░█░░░░░░░░░░░▄▄▄▄░░▀▀▀▀▀░░█░░ A qt 3.14 gf will come to you,
▐▌░░░░░░░▀▀▀▀░░░░░▀▀▀▀▀░░░▐▌░ but ONLY if you post a
█░░░░░░░░░▄▄▀▀▀▀▀░░░░▀▀▀▀▄░█░ `>tfw no GF on this thread
█░░░░░░░░░░░░░░░░▀░░░▐░░░░░▐▌
▐▌░░░░░░░░░▐██▀█▄░░░░░░█▀█░▐▌
░█░░░░░░░░░░░▀▀▀░░░░░░▀▀▀▀░▀▄
░▐▌░░░░▄░░░░░░░░░░░░░▌░░░░░░█
░░▐▌░░▐░░░░░░░░░░░░░░▀▄░░░░░█
░░░█░░░▌░░░░░░░░▐▀░░░░▄▀░░░▐▌
░░░▐▌░░▀▄░░░░░░░░▀░▀░▀▀░░░▄▀░
░░░▐▌░░▐▀▄░░░░░░░░░░░░░░░░█░░
░░░▐▌░░░▌░▀▄░░░░▀▀▀▀▀▀░░░█░░░
░░░█░░░▀░░░░▀▄░░░░░░░░░░▄▀░░░
░░▐▌░░░░░░░░░░▀▄░░░░░░▄▀░░░░░
░▄▀░░░▄▀░░░░░░░░▀▀▀▀█▀░░░░░░░

Don't change these.
Name: Email:
Entire Thread Thread List