T.R | Title | User | Personal Name | Date | Lines |
---|
55.1 | =~ is not a string assign | NETRIX::"[email protected]" | Jarkko Hietaniemi | Fri Feb 21 1997 09:01 | 32 |
| =~ is not a string assign operator. It is a "bind the left
hand side to be the target of the string operator on the right
hand side." Your line:
$one =~ join('', $one, $two);
does not mean anything because the join() is not a string
operator. join() is a string function.
The string operator for concatenating strings is '.'. For example:
$x = $y . $z;
This can also be done by join:
$x = join('', $x, $y);
but using join() with the first argument empty is a bit silly,
join is better when the delimiter is something longer like for
example:
$x = join(':', $x1, $x2, $x3, $x4, $x5);
Yet another way is to use the "double quotes" interpolation:
$x = "$y$z"
The =~ (and its logical negation !~) is used only for the match
and replace operators (m// and s//).
[Posted by WWW Notes gateway]
|
55.2 | Forgot... | NETRIX::"[email protected]" | Jarkko Hietaniemi | Fri Feb 21 1997 09:33 | 5 |
| The strings ops that make sense on the rhs of =~ and !~ are:
m//, s//, *AND* tr//.
[Posted by WWW Notes gateway]
|
55.3 | YAT (Yet Another Correction (TM)) | NETRIX::"[email protected]" | Jarkko Hietaniemi | Fri Feb 21 1997 09:56 | 21 |
| $foo =~ bar(...);
_does_ in fact mean something: it means match (m//) the LHS to
the RHS, the RHS being evaluated in runtime each time. In your
example:
$one = "abc";
$two = "def";
$one =~ join('', $one, $two);
is identical to
$one =~ /abcdef/;
which in itself is a bit funny (normally the match operator m// is
the conditional of if/unless/while/until/for) but it does have
side effects. As the match is unsuccessful (there is no 'abcdef'
in 'abc'), the special variables $1, $2, ..., and $&, $+, $', $`,
become undefined.
[Posted by WWW Notes gateway]
|
55.4 | | DONVAN::KEEFE | | Fri Feb 21 1997 11:55 | 13 |
| So, if I want to append, without making a new variable, then:
$x = $x . $y;
...is what does the trick, and not
$x =~ $x . $y
Interesting. I have not been in the habit of reading the man
pages, instead relying on the camel and llama, but the
explanation of =~ in perlop is much more clear than in either
book. So I will change my habit starting now. :-)
|
55.5 | .= = =~ | NETRIX::"[email protected]" | Jarkko Hietaniemi | Mon Feb 24 1997 02:01 | 23 |
| The append operator is .=:
$x .= $y;
The assign operator is =:
$x = $y;
The bind operator is =~:
May I suggest the Perl Reference Guide:
http://www.perl.com/CPAN/doc/refguide/perlref-5.001.1.tar.gz
--
Jarkko Hietaniemi Digital/Finland MCS/OSS DTN 879-4738
[email protected] +358-(0)9-434 4738
$x =~ s/a/b/g;
[Posted by WWW Notes gateway]
|
55.6 | docs could be more clear | 2664::KEEFE | | Wed Feb 26 1997 09:58 | 21 |
| Thanks I have the Reference Guide and see .= mentioned, obliquely.
The llama book says:
Another common assignment operator is the string concatenate
operator:
$str = $str . " "; # append a space to $str
$str .= " "; # same thing with assignment operator
It is appending of course but it is not called "the append operator"
anywhere I can see. Which doesn't matter, except that's why I didn't
find it by looking in the index.
Operators should be added to the index so you can look them up by
function. Currently you have to already know .= does an append, to
find it in the index to see what it does!
Well, I learned something.
Neil
|
55.7 | more on join with empty first arg | VAXUUM::KEEFE | | Fri Mar 07 1997 09:04 | 16 |
| Re .1
> but using join() with the first argument empty is a bit silly,
I noticed on p 369 of Camel:
Prefer join("", ...) to a series of concatenated strings.
Multiple . concatenations cause strings to be copied back
and forth multiple times. The join operator avoids this.
For concatenating only two strings join may be overkill, but
for more than two apparently it is preferred. Unless something
changed in V5.
|
55.8 | yes, there is a breakeven point | 12368::"[email protected]" | Jarkko Hietaniemi | Thu Mar 13 1997 03:31 | 40 |
| For 'few' arguments "." is faster, yes, that is still true.
What is 'few' depends but something like 5 arguments is still 'few'
and 6 is not.
I used the following Perl 5 code to benchmark this:
---
use Benchmark;
$n = 10000000;
$s = 10;
$a = $s x 'a';
$b = $s x 'b';
$c = $s x 'c';
$d = $s x 'd';
$e = $s x 'e';
$r = $n / $s;
timethis($r, '$a . $b');
timethis($r, 'join("", $a, $b)');
timethis($r, '$a . $b . $c');
timethis($r, 'join("", $a, $b, $c)');
timethis($r, '$a . $b . $c . $d');
timethis($r, 'join("", $a, $b, $c, $d)');
timethis($r, '$a . $b . $c . $d . $e');
timethis($r, 'join("", $a, $b, $c, $d, $e)');
timethis($r, '$a . $b . $c . $d . $e . $a . $b . $c . $d . $e');
timethis($r, 'join("", $a, $b, $c, $d, $e, $a, $b, $c, $d, $e)');
# eof
[Posted by WWW Notes gateway]
|