Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re^2: Array loops and assignments

by madbee (Acolyte)
on Jul 07, 2013 at 19:46 UTC ( #1043008=note: print w/replies, xml ) Need Help??

in reply to Re: Array loops and assignments
in thread Array loops and assignments

@Preceptor: Right. I want to store each element of the array in a variable of its own. So instead of assigning it to individual variables, I was hoping to build the variable and assign it on the fly. Makes sense? Perhaps,I'm overcomplicating this... Thanks!

Replies are listed 'Best First'.
Re^3: Array loops and assignments
by davido (Archbishop) on Jul 08, 2013 at 01:26 UTC

    Definitely overcomplicating it. Try this idiom: Think of a hash as a namespace, and its elements as variables within that namespace.

    my @array = qw( Larry Curly Moe ); my %stooges; @stooges{ @array } = ( 0 .. $#array ); print "$_ = $stooges{$_}\n" for keys %stooges;

    Or if you want numbers in the name:

    %stooges = map { 'var' . $_ , $array[$_] } 0 .. $#array;


Re^3: Array loops and assignments
by roboticus (Chancellor) on Jul 07, 2013 at 20:24 UTC


    OK, I'll bite: Why? What are you going to be able to do with the variables that you can't already do with the array?


    When your only tool is a hammer, all problems look like your thumb.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1043008]
[stevieb]: yeah, so the wiringPi library appears to be missing/injecting incorrect defined variables into I2C calls, and it's all over the map. I'm going to have to revisit and use something else, write something else, or scrutinize the code and fix

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2017-06-23 00:06 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (532 votes). Check out past polls.