Home arrow Perl Programming arrow Page 4 - Scalars and Boolean and String Operators

String Operators - Perl

In this third part of a five-part series on scalars in Perl, we continue our study of operators, moving on to Boolean and string operators. This article is excerpted from chapter two of the book Beginning Perl, written by James Lee (Apress; ISBN: 159059391X).

TABLE OF CONTENTS:
  1. Scalars and Boolean and String Operators
  2. Comparing Numbers for Equality
  3. Boolean Operators
  4. String Operators
By: Apress Publishing
Rating: starstarstarstarstar / 2
April 08, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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:

#!/usr/bin/perl -w
# string1.pl

print "Four sevens are ". 4*7 ."\n";

which tells us, reassuringly, that

$ perl string1.pl
Four sevens are 28
$

The other string operator is the repetition operator, marked with anx. This repeats a string a given number of times:

#!/usr/bin/perl -w
# string2.pl

print "GO! " x 3, "\n";

will print

$ perl string2.pl
GO! GO! GO!
$

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:

#!/usr/bin/perl -w
# string3.pl

print "Ba" . "na" x 4 ,"\n";

On running this, we’ll get

$ perl string3.pl
Banananana
$

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:

#!/usr/bin/perl -w
# string4.pl

print "Ba" . "na" x 4*3 ,"\n";
print "Ba" . "na" x (4*3) ,"\n";

Compare:

$ perl string4.pl
Argument "nananana" isn't numeric in multiplication (*) at string4.pl line 4.
Ba0
Banananananananananananana
$

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:

#!/usr/bin/perl -w
# str2num.pl

print "12 monkeys"    + 0,  "\n";
print "Eleven to fly" + 0,  "\n";
print "UB40"          + 0,  "\n";
print "-20 10"        + 0,  "\n";
print "0x30"          + 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
Argument "12 monkeys" isn't numeric in addition (+) at str2num.pl line 4.
Argument "Eleven to fly" isn't numeric in addition (+) at str2num.pl line 5.
Argument "UB40" isn't numeric in addition (+) at str2num.pl line 6.
Argument "-20 10" isn't numeric in addition (+) at str2num.pl line 7.
Argument "0x30" isn't numeric in addition (+) at str2num.pl line 8.
12
0
0
-20
0
$

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:

  1. A string that is purely a number is automatically converted to the number (“21.42” is converted to 21.42).
  2. Leading whitespace is ignored (“ 12” is converted to 12). 
     
  3. Trailing nonnumerics are discarded (“12perl” is converted to 12). 
     
  4. Strings that do not start with numeric values are treated as 0 (“perl12” is converted to 0).

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.



 
 
>>> More Perl Programming Articles          >>> More By Apress Publishing
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: