in reply to How to duplicate every member of an array

You have some good answers from other Monks so now you can do this if you want.

My question is: why do you want to do this? It will double/n-fold the memory footprint of your array for very little benefit. It sounds a little bit like an XY Problem. Would you care to elaborate?


🦛

  • Comment on Re: How to duplicate every member of an array

Replies are listed 'Best First'.
Re^2: How to duplicate every member of an array
by soblanc (Acolyte) on Jul 28, 2022 at 14:02 UTC
    ahah don't worry, this is to handle a very particular case in my script (not the general case, and hard to explain without context), so here it provides a fair solution to my problem, with a good benefit. But I understand your comment, since usually we want to do the opposite (get rid of the duplicates) ;)
Re^2: How to duplicate every member of an array
by Anonymous Monk on Jul 28, 2022 at 10:48 UTC
    sounds like homework.
      ..if homework a oneliner is needed :D

      perl -MData::Dump -M"5;push @ARGV,reverse @ARGV" -e "push @m,shift,pop while @ARGV;dd @m" a b c

      ("a", "a", "b", "b", "c", "c")

      L*

      PS hello soblanc and welcome to the Monastery and to the wonderful world of Perl!

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

        But why not a simple one-liner:

        Win8 Strawberry 5.8.9.5 (32) Thu 07/28/2022 8:22:01 C:\@Work\Perl\monks >perl -MData::Dump -e "my @m = map { ($_) x 2 } @ARGV; dd \@m" a b + c ["a", "a", "b", "b", "c", "c"]


        Give a man a fish:  <%-{-{-{-<

        Thank you Discipulus, and thanks everyone for your (quick!) replies :)