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

join using different delimiter for first element of list

by NateTut (Deacon)
on Dec 07, 2010 at 21:02 UTC ( #875886=perlquestion: print w/replies, xml ) Need Help??
NateTut has asked for the wisdom of the Perl Monks concerning the following question:

I would like to use a different delimiter for the first element of a join. Below is my feeble attempt to do this. Any suggestions?
use strict; use warnings; my @FieldNames = ( 'One' , 'Two' , 'Three' , 'Four' , 'Five' ) ; my $Foo = join(($_ eq 'One' ? '1' : 'n'), @FieldNames); print("\$Foo\[$Foo\]\n");

Replies are listed 'Best First'.
Re: join using different delimiter for first element of list
by kennethk (Abbot) on Dec 07, 2010 at 21:10 UTC
    If I'm understanding you correctly, you could do something like this:

    use strict; use warnings; my @FieldNames = ( 'One' , 'Two' , 'Three' , 'Four' , 'Five' ) ; print join ':', shift(@FieldNames), join ',', @FieldNames;

    or if you want a non-destructive technique, you could use array Slices:

    print join ':', $FieldNames[0], join ',', @FieldNames[1..$#FieldNames];

      I'd go for slices just to avoid the vagaries of evaluation order! Side effects in evaluating parameter lists are a potent source of hard to track bugs.

      True laziness is hard work
Re: join using different delimiter for first element of list
by AR (Friar) on Dec 07, 2010 at 21:14 UTC
    sub newjoin { my( $f, $a, @r ) = @_; my $t = shift @r; $t .= ( $f . join $a, @r ) if @r; return $t; }
Re: join using different delimiter for first element of list
by hbm (Hermit) on Dec 09, 2010 at 18:32 UTC

    I think I'd just join them all, and then change the first instance of the delimiter:

    ($Foo=join',',@FieldNames)=~s/,/:/; # e.g., "One:Two,Three"
Re: join using different delimiter for first element of list
by AnomalousMonk (Canon) on Dec 08, 2010 at 09:55 UTC

    My assumption is that NateTut would like the custom  join to behave analogously to the built-in join: two join-expressions must be present, and either may evaluate to the empty string; the list of strings to join may be empty.

    AR's and kennethk's approaches both produce oddball output for corner cases: undefined or oddly joined output, operations on undefined values.

    As far as I can tell, the following functions, in increasing order of opacity, all operate like the join built-in. I haven't done any Benchmark-ing on them.

    sub join_multi { die "not enough parameters for join_multi()" if @_ < 2; my $delim1 = shift; my $delim2 = shift; return @_ > 1 ? join $delim1, $_[0], join $delim2, @_[1 .. $#_] : @_ ? $_[0] : '' ; } sub join_multi { die "not enough parameters for join_multi()" if @_ < 2; return @_ > 3 ? join $_[0], $_[2], join $_[1], @_[3 .. $#_] : @_ == 3 ? $_[2] : '' ; } sub join_multi { die "not enough parameters for join_multi()" if @_ < 2; return join '', map { ('', $_[0], ($_[1]) x ($#_ - 2))[$_ - 2], $_[$_] } 2 .. $#_ ; }
Re: join using different delimiter for first element of list
by NateTut (Deacon) on Dec 09, 2010 at 17:56 UTC
    Thanks for all the informative & creative solutions. Is there an expression that I could use as the delimiter that would produce a different delimiter for the first element? That was my first thought.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://875886]
Approved by kennethk
help
Chatterbox?
[GotToBTru]: ah yes that infamous poison di-hydrogen oxide

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2016-12-06 12:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (103 votes). Check out past polls.