in reply to Re: Adding a number from scalar variable to array name
in thread Adding a number from scalar variable to array name

Main task is to break it up into multiple arrays to be used later in some SQL queries in some sort of for loop, below is some code similar:
#Original variable @numbers = (1,2,3,4); #so lets say the code actually created array variables @array1 @array2 @aaray3 @array4 #Each of these variables will be assign certain amount of elements. Th +e arrays will be used in a SQL statment like this for ($i = 1;$i < 5;$++) { print FINAL "create table bce.week_g${last_4_chars}v00_temp$i as\n". "select distinct(a.FILEDATE), a.RPTSTORE, b.LOC_N\n". "From bce.week\n". "where ID in (@{'array'}$i);"; }

Replies are listed 'Best First'.
Re^3: Adding a number from scalar variable to array name
by ikegami (Patriarch) on Sep 08, 2006 at 19:35 UTC
    use strict; use warnings; my @numbers = (1,2,3,4); my @data; # Example Data. for my $i (0..$#numbers) { @{$data[$i]} = ( $numbers[$i] ); } for my $i (0..$#data) { print FINAL " CREATE TABLE bce.week_g${last_4_chars}v00_temp" . ($i+1) . " AS SELECT DISTINCT(a.FILEDATE), a.RPTSTORE, b.LOC_N FROM bce.week WHERE ID in (" . join(', ', @{$data[$i]}) . "); "; }

    Note the use of strict and warnings.
    Note the use of join to seperate the IDs with commas.
    Note the lack of hardcoding of the number of elements.
    Note how capitalization and alignement makes the SQL stmt much more readable.

    Updated to use code from OP.

Re^3: Adding a number from scalar variable to array name
by shoesaphone (Novice) on Sep 08, 2006 at 19:50 UTC
    How about this then?
    my @store = ( # use a hash if your keys aren't all small scalars undef, # given that you want to start at 1 [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], # etc. ); foreach my $i ( 1..5 ) { my $idString = join( q{, }, @{$store[ $i ]} ); print FINAL "create table bce.week_g${last_4_chars}v00_temp$i as\n". "select distinct(a.FILEDATE), a.RPTSTORE, b.LOC_N\n". "From bce.week\n". "where ID in ($idString);\n"; }