[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference 7.286::perl

Title:Perl
Notice:PERL, the Pathologically Eclectic Rubbish Lister
Moderator:PLUGH::needle
Created:Thu Jun 02 1994
Last Modified:Thu May 15 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:56
Total number of notes:132

55.0. "join and =~" by DONVAN::KEEFE () Tue Feb 18 1997 17:24

Why doesn't the first join in the program below work?
It's not an error, but nothing happens.

In general, if you just want to append one thing to 
another, I guess it's better to:

$one =~ s/$/$two/;

Still, I am curious about the join. 

Neil

----------------------------------------------
#!/usr/local/bin/perl

$one = "abc";
$two = "def";

$one =~ join('', $one, $two);

print "Join using =~ on \$one:\t$one\n"; 

$three = join('', $one, $two);

print "Join using = \t\t$three\n"; 

-----------------------------------------------

Join using =~ on $one:	abc        <  Why isn't this abcdef
Join using = 		abcdef
T.RTitleUserPersonal
Name
DateLines
55.1=~ is not a string assignNETRIX::&quot;jarkko.hietaniemi@fno.mts.dec.com&quot;Jarkko HietaniemiFri Feb 21 1997 12:0132
=~ 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.2Forgot...NETRIX::&quot;jarkko.hietaniemi@fno.mts.dec.com&quot;Jarkko HietaniemiFri Feb 21 1997 12:335
The strings ops that make sense on the rhs of =~ and !~ are:

m//, s//, *AND* tr//.

[Posted by WWW Notes gateway]
55.3YAT (Yet Another Correction (TM))NETRIX::&quot;jarkko.hietaniemi@fno.mts.dec.com&quot;Jarkko HietaniemiFri Feb 21 1997 12:5621
	$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.4DONVAN::KEEFEFri Feb 21 1997 14:5513
    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::&quot;Jarkko.Hietaniemi@fno.mts.dec.com&quot;Jarkko HietaniemiMon Feb 24 1997 05:0123
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
Jarkko.Hietaniemi@fno.mts.dec.com   +358-(0)9-434 4738


$x =~ s/a/b/g;


[Posted by WWW Notes gateway]
55.6docs could be more clear2664::KEEFEWed Feb 26 1997 12:5821
    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.7more on join with empty first argVAXUUM::KEEFEFri Mar 07 1997 12:0416
    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.8yes, there is a breakeven point12368::&quot;jarkko.hietaniemi@fno.mts.dec.com&quot;Jarkko HietaniemiThu Mar 13 1997 06:3140
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]