in reply to Adding a number from scalar variable to array name

If you really think you must do it, eval is one way you could go:
@numbers = (1,2,3,4); foreach $num (@numbers) { eval("\@store_get$num = ($num)"); }
But I tell you, like the others do: Don't do it. There are far better ways to achieve what you want to achieve without creating arrays. Just tell us what the main task is.

s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

Replies are listed 'Best First'.
Re^2: Adding a number from scalar variable to array name
by ikegami (Patriarch) on Sep 08, 2006 at 19:20 UTC
    ug! Why compound the problem by needlessly using eval EXPR.
    @numbers = (1,2,3,4); foreach $num (@numbers) { no strict 'refs'; @{"store_get$num"} = ($num); }
      Why? Cause TMTOWTDI!
      Why? Cause neither way is good.
      Why? Cause I'm a caveman.
      Why? Cause I've got eyes in the back of my head.
      Why? It's the heat. Standby.
      excerpt shamelessly cited from Laurie Anderson's "From the Air" ;-)

      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Re^2: Adding a number from scalar variable to array name
by Anonymous Monk on Sep 08, 2006 at 19:22 UTC
    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);"; }
      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.

      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"; }