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


In reply to Re: join doesn't seem to insert separators by davido
in thread join doesn't seem to insert separators by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.