SamCG has asked for the wisdom of the Perl Monks concerning the following question:

Okay, I have an ftp download that picks up the most recent file (they have datestamps in the file name), with a particular set of names defined in an array.

It's a "chatty" script, one which indicates the files available and then downloads the proper one. I'd really like the listing to put a newline after every second file, so that instead of:

files available=
file1, file2, file3, file4, file5, file6

I get:

files available=
file1, file2
file3, file4
file5, file6


Please forgive the style of code snippet below -- it does break things out into more explicit steps than it probably needs.
for (@data){ @datafiles = $ftp->ls("*$_\.*1"); @datafiles = reverse @datafiles; print "files available=\n".join ", ", @datafiles; print "\n"; $currentfile = $datafiles[0]; print "getting $currentfile\n";

suggestions?

Replies are listed 'Best First'.
Re: how to alternate join?
by Roy Johnson (Monsignor) on May 05, 2005 at 15:43 UTC
    A while back, I wrote Fun with join. I think it's great that you have a possible use for it. :-)

    Alternatively, you could use List::Util 'reduce' to roll your own version of join that does the same thing.

    use List::Util 'reduce'; my @arr = ('A'..'L'); my $per_line = 2; my $on_this_line = 0; print reduce {$a . (++$on_this_line % $per_line ? ',' : "\n") . $b} @a +rr;
    or
    my @arr = ('A'..'L'); my $sep = [',', "\n"]; print reduce {$a . ($sep->[@$sep] = shift @$sep) . $b} @arr;
    The last works, though it's possibly too clever. What if the left @$sep is evaluated before the shift?

    Caution: Contents may have been coded under pressure.
Re: how to alternate join?
by Roy Johnson (Monsignor) on May 05, 2005 at 18:05 UTC
    You know how some questions just keep tickling your brain for more solutions? This is one of those. Look, Ma! No temp variables!
    sub pairs { map {join ',', splice(@_, 0, 2)} 0..$#_/2 } print join("\n", pairs(@arr)), "\n";
    That reads pretty nice. I'm going to say that's my final answer, Regis.

    Update: replaced an awkward map with a nice splice.


    Caution: Contents may have been coded under pressure.
      beautiful! Thank you all -- some questions seem lead you to things far better than the actual answer itself. I feel like that here.
Re: how to alternate join?
by blazar (Canon) on May 05, 2005 at 15:42 UTC
    TIMTOWTDI. You may roll your own altjoin() sub, e.g.:
    sub altjoin ($$@) { my $d1=shift; my $d2=shift; my $cnt; @out=map { $_ => ++$cnt % 2 ? $d1 : $d2} @_; pop @out; @out; }

    Update: except that this is not a "join", still I hope that as a minimal example it is not that bad...

Re: how to alternate join?
by tlm (Prior) on May 05, 2005 at 15:45 UTC

    There are a bazillion ways to do this using unglamorous loops, but if you insist in using join for this, check out Roy Johnson's Fun with join.

    the lowliest monk

Re: how to alternate join?
by davidrw (Prior) on May 05, 2005 at 15:53 UTC
    As mentioned, the roll-your own with a mod 2 in there somewhere is a way to go. Could also do a more creative (read: maybe not best) join like:
    my @datafiles = map { "file".$_ } 1..7; printf "files available=\n%s\n", join("\n", # 4) glue w/your newlines grep { $_ } # 3) exclude blank elements created by + split() split(/([^,]+, [^,]+, )/, # 2) split on pairs join(", ", @datafiles) # 1) glue big ,-delim string ) ) ;