P is for Practical | |
PerlMonks |
Re: join doesn't seem to insert separatorsby davido (Cardinal) |
on Sep 14, 2004 at 04:00 UTC ( [id://390733]=note: print w/replies, xml ) | Need Help?? |
Both of the previous responses you received were spot on, but I wanted to mention one other thing. As the other responses mentioned, the first argument of join is an argument that should evaluate to the string you wish to use in joining the list that follows. Normally that expression will be a quoted string. In the case of your need, that could be join ":", "dog", "day", "afternoon";, or you could use single quotes instead of double quotes. Both of those will evaluate to a string that pretty much looks like what you've put literally between the quote operators. But remember, any expression will do. The problem you're seeing is that in using a regexp type operator, when that operator is evaluated, it returns a boolean value: truth or falsehood, based on Perl's notion of true and false. Quotes are operators too. They just return a string equal to the literal text they enclose (interpolated in the case of double-quotes). If your expression were join 1, "dog", "day", "afternoon", the output would be "dog1day1afternoon". Now for the surprise. Try the following code and see what happens:
Now why on earth would the output here be "dog1day1afternoon"? Because m/:/ is implicitly matched against the contents of $_, which we've set to ":". So the result is ...a match: success, truth, and in fact, "1". Isn't that cool? ;) Dave
In Section
Seekers of Perl Wisdom
|
|