Last active
August 29, 2015 14:27
-
-
Save packetchef/0de8339bc7a8d88b9455 to your computer and use it in GitHub Desktop.
Assigning variables based on regular expression in perl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use match with an explicit variable | |
$mystr = "hello to the world"; | |
($one, $two) = $mystr =~ /(\w+)[^\w](\w+)/; | |
print("$one\n"); | |
print("$two\n"); | |
# Use with implicit variable $_ | |
$_ = "hello to the world"; | |
($one, $two) = /(\w+)[^\w](\w+)/; | |
print("$one\n"); | |
print("$two\n"); | |
# Again using implicit $_, but as the result of other actions | |
# Equivalent to: while($_ = <STDIN>) { | |
while(<STDIN>) { | |
chomp($_); | |
($one, $two) = /(\w+)[^\w](\w+)/; | |
print("$one\n"); | |
print("$two\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment