in reply to Extract substring from string with no whitespace using regexp?
You have ...
foreach (@results) { if ($_ =~ m/(http\:\/\/).+/) { print "URL found: ".$_,"\n"; } }
The reason the "default variable", $_, exists is to simplify chunks of code which all operate on the same variable by eliminating explicit reference to the variable. Admittedly, some people question whether this is simplification and clarification, or obfuscation, but that's a separate discussion thread. As far as your code is concerned, I suggest it is pointless to assign to the default variable in the loop constrtuct, and then explicitly state the variable within it.
Either make use of the way the various functions use the default variable:
foreach (@results) { if (/(http\:\/\/).+/) { print "URL found: "; print; print "\n"; } }
Or else use a variable and give it a meaningful name. This is the more robust, as it allows you to use other constructs which could generate default variables, without worrying about the outer value being trashed, and more importantly, conveyes meaning to readers.
foreach $url (@results) { if ($url =~ m/(http\:\/\/).+/) { print "URL found: " . $url, "\n"; } }
P.S.: Back in the 70's, people dropped optional spaces from their BASIC programs, since it made it possible to fit more code into the 8K or 16K available RAM. Now that your CPU has more cache than that, use the spaces you need to make that print statement easily readable.
--
TTTATCGGTCGTTATATAGATGTTTGCA
|
|---|