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

Hi Holy Ones,
I hope you can help, I'm stumped yet again.

I have an array like this:  @array1=(1,33,45,66) and I simply want to double up every element, so it looks like this:
@newarray=(1,1,33,33,45,45,66,66)

I tried a loop using push... but no success. The numbers within the array are actually being pulled out from a text file, and the array is much larger than the example given here (if it helps).
Any ideas?
Many thanks, help much appreciated.

Replies are listed 'Best First'.
Re: How do I double up elements within an array?
by ikegami (Patriarch) on Aug 05, 2008 at 18:57 UTC
    my @newarray; for (@array1) { push @newarray, $_, $_; }
    or
    my @newarray = map { $_, $_ } @array1;
      man, I wish I could come up with solutions as fast as you guys.
      Thanks Archbishop, that does the trick.
      I guess I'd better start going to church more often. I must've strayed from the straight and narrow somewhere.
Re: How do I double up elements within an array?
by shmem (Chancellor) on Aug 05, 2008 at 19:08 UTC

    To do that in-place:

    @array1 = (1,33,45,66); for (reverse 0..$#array1) { splice @array1, $_, 0, $array1[$_]; } print "@array1\n"; __END__ 1 1 33 33 45 45 66 66

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: How do I double up elements within an array?
by TGI (Parson) on Aug 05, 2008 at 20:29 UTC

    ikegami's solution is simple and elegant, as well as being what I would probably use. But, for the sake of TIMTOWTDI, and to introduce a very handy module, let me suggest:

    use List::MoreUtils qw(zip); my @orig = qw(1 33 45 66); my @doubled = zip @orig, @orig; print "@doubled\n";


    TGI says moo

Re: How do I double up elements within an array?
by pjotrik (Friar) on Aug 05, 2008 at 19:03 UTC
    Map is your friend:
    @doubled = map({$_, $_} @orig);
Re: How do I double up elements within an array?
by Lawliet (Curate) on Aug 05, 2008 at 19:01 UTC
    my @array1=(1,33,45,66); my @array2; foreach my $element (@array1) { push @array2, $element, $element; } print @array2;

    Seems to work fine. When you tried that method, did you not use a second array?

    <(^.^-<) <(-^.^<) <(-^.^-)> (>^.^-)> (>-^.^)>
Re: How do I double up elements within an array?
by GrandFather (Saint) on Aug 05, 2008 at 23:27 UTC

    If order doesn't matter you can simply:

    @array1 = (@array1, @array1);

    Perl reduces RSI - it saves typing
Re: How do I double up elements within an array?
by FunkyMonk (Bishop) on Aug 05, 2008 at 22:39 UTC
    And yet another way...
    my @array1=(1,33,45,66); my @newarray = sort { $a <=> $b } ( @array1 ) x 2;

    Or, if you don't care about the order, as long as similar elements are adjacent (although it doesn't make any difference for your sample data):

    my @array1=(1,33,45,66); my @newarray = sort +( @array1 ) x 2;


    Unless I state otherwise, all my code runs with strict and warnings
Re: How do I double up elements within an array?
by spivey49 (Monk) on Aug 05, 2008 at 19:13 UTC

    I'm not sure what type of loop you tried, but this produces an array in the order you described:

    use strict; use warnings; my @array1=(1,33,45,66); my @array2; for my $items(@array1){ push @array2,($items,$items); } for my $item(@array2){ print "$item\n"; }