in reply to Re^6: Value into array
in thread Value into array
Update: Another code example:#!/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
#!/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"], }
|
---|