Skip to content

Instantly share code, notes, and snippets.

@packetchef
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save packetchef/0de8339bc7a8d88b9455 to your computer and use it in GitHub Desktop.
Save packetchef/0de8339bc7a8d88b9455 to your computer and use it in GitHub Desktop.
Assigning variables based on regular expression in perl
# 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