Rewrite the code a bit:
my $string = '11 15 '; while ( $string =~ /(\d+)/g ) {
I changed the regex of the while statement. You can use \d+ to represent one or more digits, and this is easier to understand than [0-9]{1,} . You also (since the space does not match \d ), you do not need the last space at the end of the line.
Take a look at the rest of the code:
my $string = '11 15'; while ( $string =~ /(\d+)/g ) { my $match = $1; print "$match....."; if ($match >= 12 and $match <= 14) {
You cannot use regular expression the way you should test a range.
Use a regular range test instead.
if ($match >= 12 and $match <= 14)
or newer group test:
if ($match ~~ [12..14])
This last one works only in newer versions of Perl, such as 5.12 on my Mac and 5.14 on my Linux box, but not on Perl 5.8 in my Solaris window).
A few tips:
- Use indents and spaces. This makes your code more readable.
- Use descriptive names for variables. Instead of
$m I used $match . - Do not use the supplied
if . The attached if harder to detect, so you may miss something important, and this makes it difficult to update your code. It can be used if the report itself is clear and simple, and it improves readability. The latter is a bit subjective, but you usually see attached if in things like return if not -f $file; . - Save variables for one purpose. In this case, instead of changing the value of
$match I used the if/else . Imagine your code was a bit more complicated and someone had to add a new function. They see the $match variable and believe that this is what they need. Sorry, you changed what $match . Now this is the value to be printed, not the string match. It may take someone who modified your program for a while to find out what happened to the value of $match and why it was mysteriously set to t219>. - In the print statement, you can include variables inside double quotes. This is very different from almost all other languages. This is because the Perl variable uses sigils to denote variable names. This usually makes reading easier if your combined variables and other lines are on the same line.
For instance:
print "The range of possible values are $low to $high\n";
vs.
print "The range of possible values are " . $low . " to " . $high . "\n";
Note that in the second example, I had to be careful with the spaces inside the quotes, while in the first example the required spaces were pretty natural. Imagine that you need to change this statement in a later version of the program. What will be easier to maintain?
source share