After that lot, there are surprisingly few string operators. Actually, for the moment, we’re only going to look at two.
The first one is the concatenation operator, which glues two strings together into one. Instead of saying
print "Print ", "several ", "strings ", "here", "\n";
we could say
print "Print " . "one ". "string " . "here" . "\n";
As it happens, printing several strings is slightly more efficient, but there will be times you really do need to combine strings together, especially if you’re putting them into variables.
What happens if we try and join a number to a string? The number is evaluated and then converted:
print "Four sevens are ". 4*7 ."\n";
which tells us, reassuringly, that
$ perl string1.pl
The other string operator is the repetition operator, marked with anx. This repeats a string a given number of times:
print "GO! " x 3, "\n";
$ perl string2.pl
We can, of course, use it in conjunction with concatenation. Its precedence is higher than the concatenation operator’s, as we can easily see for ourselves:
print "Ba" . "na" x 4 ,"\n";
On running this, we’ll get
$ perl string3.pl
In this case, the repetition is done first (“nananana”) and then it is concatenated with the “Ba”. The precedence of the repetition operator is the same as the arithmetic operators, so if you’re working out how many times to repeat something, you’re going to need parentheses:
print "Ba" . "na" x 4*3 ,"\n";
$ perl string4.pl
Why was the first oneBa0? The first thing was the repetition, giving us “nananana”. Then the multiplication—What’s “nananana” times three? When Perl converts a string to a number, it takes any spaces, an optional minus sign, and then as many digits as it can from the beginning of the string, and ignores everything else. Since there were no digits here, the number value of “nananana” was 0. Also note that if the string that is converted to a number contains no numeric characters, Perl will warn you about it as shown previously.
That 0 was then multiplied by 3, to give 0. Finally, the 0 was turned back into a string to be concatenated onto the “Ba”.
Here is an example showing how strings automatically convert to numbers by adding 0 to them:
print "12 monkeys" + 0, "\n";
You get a warning for each line saying that the strings aren’t “numeric in addition (+),” but what can be converted is:
$ perl str2num.pl
Notice how for each of these strings, when converted to numeric values, Perl complains that the string is not numeric. This happens because the string is not a simple numeric value. But also note that Perl does convert the strings to numbers (in the case of three of the strings, the value is 0).
Our first string,"12 monkeys", did pretty well. Perl understood the 12, and stopped after that. The next one was not so brilliant—English words don’t get converted to numbers. Our third string was also a nonstarter as Perl only looks for a number at the beginning of the string. If there’s something there that isn’t a number, it’s evaluated as a 0. Similarly, Perl only looks for the first number in the string. Any numbers after that are discarded. Finally, Perl doesn’t convert binary, hex, or octal to decimal when it’s stringifying a number, so you have to use thehex()oroct()functions to do that. On our last effort, Perl stopped at thex, returning 0. If we had an octal number, such as030, that would be treated as the decimal number 30.
Therefore, conversion from strings to numbers can be summed up with these rules:
The last three conversions listed will produce a warning message if the-woption is used.
Please check back next week for the next part of this series.
blog comments powered by Disqus