in reply to Re: Joining an array
in thread Joining an array

Thanks ww. I should have mentioned though that there are a variable number of elements in the array.

Replies are listed 'Best First'.
Re^3: Joining an array
by ww (Archbishop) on Feb 11, 2012 at 02:50 UTC
    Variable number of elements is not a problem; it's just a circumstance that demands a little more code....

    So, let's see... . o O

    We know how to count the elements, $#arr... so that means we'll need to repeat the next-to-last line $#arr/2 times.

    Oops. Not an integer. Well, we knew that already too, but after we've printed the first pair, we should need to repeat either of the next two lines ( ($#arr/2) -1 ) times. Or we could start with ( ($#arr +1)/2 ). Either way, Shazam! The problem of an unknown number of pairs in the array is (almost) solved, except for the trivial act of actually writing the code.

    Do you smell a loop coming? I do.

    Again, this is a case of TIMTOWTDI that's NOT worth pursuing, except for the mental exercise (or maybe, an obfuscation contest). The solutions with a module that's designed to do just what you want are clearly the way to go in the real world.

    It does, however, have a smidgen of value for readers here in the Monastery: it emphasizes the need for precision and completeness in problem statements.

      Point taken about problem statements ww. Whatever anyone else gets out of it I for one have learnt a geansaí load from this one thread. I would never have thought of using sprintf, and as for tieing a scalar... and I can already see other places where these can be used.

      -- geansaí is the Irish word for jumper, as in "I got a geansaí load of apples from that orchard" - as many apples as you can carry tucked into your jumper. Funnily enough, the Jamaican word "gansy" has exactly the same meaning.