Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Syntactic Confectionery Delight
 
PerlMonks  

Filling An Array

by MiamiGenome (Sexton)
on Apr 21, 2004 at 20:50 UTC ( #347127=perlquestion: print w/ replies, xml ) Need Help??
MiamiGenome has asked for the wisdom of the Perl Monks concerning the following question:

Greetings PMs,

I am searching for a simple method to fill an array as follows :

$array[0]= "A01"; $array[1]="A02";

..... and so on until

$array[95]="H12";

In this array, letters range from A thru H, and numbers range from 01 thru 12.

Is this the 'best' way:

@arrayletterlist = qw(A B C D E F G H); @arraynumberlist = qw(01 02 03 04 05 06 07 08 09 10 11 12); $count = 0; foreach $arrayletter (@arrayletterlist) { foreach $arraynumber (@arraynumberlist) { $masterarray[$count] = $arrayletter . $arraynumber; $count++; } }

Comment on Filling An Array
Select or Download Code
Re: Filling An Array
by exussum0 (Vicar) on Apr 21, 2004 at 20:56 UTC
    The beset way?

    Did you mean the most concise way? I'm sure we could shorten array variables down for you.

    Least memory exhaustive? Replacing foreach and those arrays to for(i=0;i<=7;i++ ) and assigning chr(i+65), and another for loop, with a sprintf could work better -- i haven't tested.

    Fastest? maybe the memory trick above may be marginally faster.

    I think your solution, is "good enough". Unless you are running it 1million times and need to optimize, this should do you well. It's very self documenting, easily maintainable and most straight forward. Unless you are playing golf, leave it alone! :)

Re: Filling An Array
by Fletch (Chancellor) on Apr 21, 2004 at 20:56 UTC

    Fore!

    my @array; for my $l ( 'A'..'H' ) { push @array, map sprintf("$l%02d", $_), 1..12; }
Re: Filling An Array
by kvale (Monsignor) on Apr 21, 2004 at 20:59 UTC
    Your method is nice and clear. If you are looking for a more golfish solution, try automagical increment:
    my $count; for my $lett ('A'..'H') { for my $combo ($lett.'01'..$lett.'12') { $masterarray[$count++] = $combo; } }

    -Mark

      f00f!
      @a = map { my $l = $_; map $l.$_, '01'..'12' } 'A'..'H';
Re: Filling An Array
by Roy Johnson (Monsignor) on Apr 21, 2004 at 21:02 UTC
    You can definitely get rid of $count by using push. Here's how I'd do it:
    for my $letter ('A'..'H') { for my $num (1..12) { push(@masterarray, sprintf("$letter%02d", $num)); } } print scalar(@masterarray)," elements\n";
    If you wanted to use some magic, you could do this instead:
    for my $letter ('A'..'H') { push(@masterarray, $letter.'01'..$letter.'12'); }
    or even
    # The golfiest my @masterarray = map { $_.'01'..$_.'12' } 'A'..'H';

    The PerlMonk tr/// Advocate
      ++Roy

      I think even the golfiest way is very clear to a reasonable perl user. On the other hand, sprintf never catch my eyes.

•Re: Filling An Array
by merlyn (Sage) on Apr 21, 2004 at 21:06 UTC
Re: Filling An Array
by jdporter (Canon) on Apr 21, 2004 at 21:15 UTC
    @a = grep { (/.(..)/)[0] < 13 } 'A01'..'H12';
      Incorrect. Includes B00, for example. (Besides, my map is still golfier. :-))

      The PerlMonk tr/// Advocate
        Ack!
        @a = grep $_++, grep { (/.(..)/)[0] < 12 } 'A00'..'H11';
Re: Filling An Array
by DamnDirtyApe (Curate) on Apr 21, 2004 at 22:39 UTC

    Mytwo cents:

    push @arr, sprintf '%s%02d', chr( $_ / 13 + 65 ), $_ % 13 for 1..103;

    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
Re: Filling An Array
by tye (Cardinal) on Apr 21, 2004 at 23:44 UTC
    push @array, $_.'00' .. $_.'12' for 'A'..'H';

    - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://347127]
Approved by hardburn
Front-paged by Trimbach
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (12)
As of 2014-04-18 14:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (469 votes), past polls