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

Monks,

Can anyone offer up a better/shorter/cleaner solution to this:
my @nums1 = map q|1234560| . $_, 1..9; my @nums2 = map q|1321343| . $_, 10..99; my @nums; for (@nums1){ push(@nums, $_); } for (@nums2){ push(@nums, $_); } for (@nums){ print "Number: $_\n";
Thanks,
Dru

Replies are listed 'Best First'.
Re: Better Way to Combine Two Arrays
by kvale (Monsignor) on Mar 10, 2004 at 17:19 UTC
    my @nums = (@nums1, @nums2);

    -Mark

Re: Better Way to Combine Two Arrays
by Limbic~Region (Chancellor) on Mar 10, 2004 at 17:19 UTC
    Dru,
    push @nums , @nums1, @nums2; # or to skip arrays all together print map { ($_ > 9 ? '1234560' : '1321343') . "$_\n" } 1 .. 99;
    Cheers - L~R
Re: Better Way to Combine Two Arrays
by chip (Curate) on Mar 10, 2004 at 17:46 UTC
    Parentheses are your friends.

    @nums = ( (map q|1234560| . $_, 1..9 ), (map q|1321343| . $_, 10..99) );

        -- Chip Salzenberg, Free-Floating Agent of Chaos

      That's how I'd write it too...

      However, I'd like to point out that map actually is a function, so you can use parentheses in the way tha is common for any other function:

      @nums = ( map(q|1234560| . $_, 1..9 ), map(q|1321343| . $_, 10..99) );
        But I'm not fond of the map EXPR syntax. A block of executable code should look like a block of executable code, and be different from the always-evaluated input list. Or so it seems to me.

        Unfortunately I recently found out that map BLOCK is actually a bit slower than map EXPR. I might fix that one of these days.

            -- Chip Salzenberg, Free-Floating Agent of Chaos

Re: Better Way to Combine Two Arrays
by blokhead (Monsignor) on Mar 10, 2004 at 18:00 UTC
    Are @nums1 and @nums2 really going to contain that data? That's a really bizarre way to generate a list of consecutive numbers.

    This reads better in my opinion:

    @nums = (12345601 .. 12345609, 132134310 .. 132134399);
    This'll be fine unless the integers are too big and become floats.

    blokhead