Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: join doesn't seem to insert separators

by davido (Cardinal)
on Sep 14, 2004 at 04:00 UTC ( [id://390733]=note: print w/replies, xml ) Need Help??


in reply to join doesn't seem to insert separators

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:

$_ = ':'; print join /:/, "dog", "day", "afternoon";

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://390733]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-19 15:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found