Re: Adding a number from scalar variable to array name
by chargrill (Parson) on Sep 08, 2006 at 18:43 UTC
|
| [reply] [d/l] |
Re: Adding a number from scalar variable to array name
by hgolden (Pilgrim) on Sep 08, 2006 at 18:47 UTC
|
It might be easier to just use a hash. Then it would be: foreach $num (@numbers) {
$store_get{$num}=$num; }
Also, if you're just assigning one number to each of the arrays, you can just access the array element directly: $numbers[$num-1]
is equal to $num from when you set up the array. I hope this helps. | [reply] [d/l] [select] |
|
Well I want to store more than one variable... Reason I want to do this is for example:
I want my code to create the arrays @array1, @array2, @array3... Based on the number it gets from the original array (1,2,3.... ).
I want @array1 = (1,2,3), @array2 = (4,5,6), etc
| [reply] |
|
Well I want to store more than one variable...
Why? (BTW: you are confusing variable and named variable).
Reason I want to do this is for example: I want my code to create the arrays @array1, @array2, @array3... I want @array1 = (1,2,3), @array2 = (4,5,6), etc
That's not a reason, it's just a restatement of the assertion that you want more than one named variable.
Please look deep within youself and see if you can actually find a reason why you want this.
| [reply] |
Re: Adding a number from scalar variable to array name
by shoesaphone (Novice) on Sep 08, 2006 at 18:55 UTC
|
Use a hash of anonymous arrays instead:
my %store;
foreach my $num ( 1..4 ) {
$store{ "get$num" } = [ $num ];
}
--shoesaphone | [reply] [d/l] |
|
Maybe that will work, but I need those arrays later to create multiple querys from.. So for example.
I created @array1, @array2 and @array3 names for my arrays based on the for loop.
@array1 = (1,2,3);
@array2 = (4,5,6);
so later in my perl code I can run these queries:
in my for loop (1..10 or whatever)
Select * from table where id in (@array1);
Select * from table where id in (@array2);
Select * from table where id in (@array3);
etc
| [reply] |
|
Using the hash of array references will work for you.
Instead of @array1=(1,2,3) you say $hash{1}=[ 1,2,3 ]. Then, to use this as an array you would say "...where id in @{$hash{1}}..."
| [reply] |
Re: Adding a number from scalar variable to array name
by ikegami (Patriarch) on Sep 08, 2006 at 18:49 UTC
|
Do you perhaps means $store_get[$num] (set element $num of @store_get) instead of @store_get$num?
| [reply] [d/l] [select] |
|
I need to create a new array called @array but add to the end of the name whatever value is in the array. So if the 1st element of the is 1. The new array name would be @array1.
| [reply] |
|
| [reply] |
|
I agree that you probably don't want to use a variable as a variable name, but you still have to fix your problem. Have you considered using an array of arrays? You can define elements as $array[1][2]=(whatever) which would be the third element of the second array (0 being the first index).
| [reply] [d/l] |
|
No, you don't NEED to do that. You really don't. You may think that's the best way to solve your problem, but it's definitely not. It's the WORST way to solve your problem. Variable variable names (symbolic references) are a bad solution to just about every problem. And you haven't invented a new good reason. You've just come up with an other example of a bad reason to use them.
Listen to what people are telling you. Use a hash, use real first rate references. Forget that you ever thought about variable variable names. You don't need them, and shouldn't want them. Are they possible? Yes. Wise? No.
| [reply] |
Re: Adding a number from scalar variable to array name
by Skeeve (Parson) on Sep 08, 2006 at 19:04 UTC
|
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
| [reply] [d/l] [select] |
|
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);
}
| [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
|
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);";
}
| [reply] [d/l] |
|
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.
| [reply] [d/l] [select] |
|
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";
}
| [reply] [d/l] |