Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Adding a number from scalar variable to array name

by Anonymous Monk
on Sep 08, 2006 at 18:41 UTC ( [id://572056]=perlquestion: print w/replies, xml ) Need Help??

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

How would I add a number to a array name? Add 1 or 2, etc to @array. I need each number(s) to be stored in a seperate array. This is a simplified version. Thanks in advance.
@numbers = (1,2,3,4); foreach $num (@numbers) { @store_get$num = $num; #The array name doesnt work }

Edit: g0n - closed code tag

Replies are listed 'Best First'.
Re: Adding a number from scalar variable to array name
by chargrill (Parson) on Sep 08, 2006 at 18:43 UTC

    Don't use a variable as a variable name.



    --chargrill
    $,=42;for(34,0,-3,9,-11,11,-17,7,-5){$*.=pack'c'=>$,+=$_}for(reverse s +plit//=>$* ){$%++?$ %%2?push@C,$_,$":push@c,$_,$":(push@C,$_,$")&&push@c,$"}$C[$# +C]=$/;($#C >$#c)?($ c=\@C)&&($ C=\@c):($ c=\@c)&&($C=\@C);$%=$|;for(@$c){print$_^ +$$C[$%++]}
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.
      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
        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.

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
      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

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

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?
      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.
        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).

        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.


        Dave

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://572056]
Approved by chargrill
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-20 14:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found