Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^6: Value into array

by ameezys (Acolyte)
on Apr 08, 2019 at 08:38 UTC ( [id://1232280]=note: print w/replies, xml ) Need Help??


in reply to Re^5: Value into array
in thread Value into array

I managed to write the code like this.
my @input_array = (N1,N2,N3,N6,N7); #not fixed (user input) for(my $i=0; $i < @input_array; $i++) { my %hash = ( $input_array[$i] => { CC0 => 1, CC1 => 1}, ); print "CC0[", $input_array[$i], "] : "; print $hash{$input_array[$i]}{CC0}, "\n"; print "CC1[", $input_array[$i], "] : "; print $hash{$input_array[$i]}{CC1}, "\n\n"; }
Here is the output:
CC0[n1] : 1 CC1[n1] : 1 CC0[n2] : 1 CC1[n2] : 1 CC0[n3] : 1 CC1[n3] : 1 CC0[n6] : 1 CC1[n6] : 1 CC0[n7] : 1 CC1[n7] : 1

The next thing I want to do is to store each CC0 value into one array. And store each CC1 value into one array. Because I need to update those values later by performing a few calculations. What should I do next?

Replies are listed 'Best First'.
Re^7: Value into array
by Marshall (Canon) on Apr 08, 2019 at 09:25 UTC
    #!/usr/bin/perl use strict; use warnings; my @input_array = qw(N1 N2 N3 N6 N7 B3 B4); #not fixed (user input) # I would recommend a "hash of array" # each "name" in the hash table like N1 or B3 points to a simple array # the first member of this array is CC0 and the second member is CC1 my %hash; foreach my $inputKey (@input_array) { @{$hash{$inputKey}} = (1,"xyz"); # each key of the hash has an # array with 2 members } foreach my $key (sort keys %hash) { my ($CC0, $CC1) = @{$hash{$key}}; print "key=$key; CC0=$CC0; CC1=$CC1\n"; } __END__ prints: key=B3; CC0=1; CC1=xyz key=B4; CC0=1; CC1=xyz key=N1; CC0=1; CC1=xyz key=N2; CC0=1; CC1=xyz key=N3; CC0=1; CC1=xyz key=N6; CC0=1; CC1=xyz key=N7; CC0=1; CC1=xyz
    Update: Another code example:
    #!/usr/bin/perl use strict; use warnings; use constant {CC0 => 0, CC1 =>1}; # just some "gravy" # CC0 means 0 and CC1 means 1 use Data::Dump qw(pp dd); my @input_array = qw(N1 N2 N3 N6 N7 B3 B4); #not fixed (user input) my %hash; foreach my $inputKey (@input_array) { @{$hash{$inputKey}} = (1,"xyz"); # each key of the hash has an # array with 2 members } #### set N3, CC0 to "hey" ##### my @array = @{$hash{"N3"}}; # read array for hash key of N3 $array[CC0] = "hey"; # modify array @{$hash{"N3"}} = @array; # write modified array back ## update.. ## yes, @{$hash{"N3"}}[CC0] = "hey"; ## will do the same thing, but I wanted to show simple steps pp \%hash; __END__ { B3 => [1, "xyz"], B4 => [1, "xyz"], N1 => [1, "xyz"], N2 => [1, "xyz"], N3 => ["hey", "xyz"], N6 => [1, "xyz"], N7 => [1, "xyz"], }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found