#!/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"], }

In reply to Re^7: Value into array by Marshall
in thread Value into array by ameezys

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.