|
Hi~!
I met a problem on printing out a kind of hash array.
i.e @array=(1,2,3,4,5,6);
$array[$i]{$term}=1;
diffent $i may contain diifernt $term(more than one term for each $i)
how can I print out each $array[$i]{$term} in each $array[$i]?
Although I can use $array[$i]{$term} singly, codes like "foreach key (sort keys %array[$i])" dont work.
I dont want to convert this kind of array to another kind, like $array{$term}[i].
I'd like to know whether there is a way to print this kind of hash array out?
Thanks very much!
| |
Share:
Expert 2GB |
I don't think you can do what you are attempting. First you have an array:
even assuming $i and $term are valid values, this will not work:
You can't automagically convert the array element ($array[$i]) into a hash. You must create the hash and assign it some initial values or just an emtpy value, for example: - @array=(1,2,3,4,5,6);
-
$i = 0;
-
$term = 'foo';
-
$array[$i]={$term=>1};# creates the hash and one key/value pair
-
print $array[$i][$term};
-
Now the value of the first element of @array is no longer equal to "1" but is am anonymous hash with a key/value pair (foo/1);
| | |
I don't think you can do what you are attempting. First you have an array:
even assuming $i and $term are valid values, this will not work:
You can't automagically convert the array element ($array[$i]) into a hash. You must create the hash and assign it some initial values or just an emtpy value, for example: - @array=(1,2,3,4,5,6);
-
$i = 0;
-
$term = 'foo';
-
$array[$i]={$term=>1};# creates the hash and one key/value pair
-
print $array[$i][$term};
-
Now the value of the first element of @array is no longer equal to "1" but is am anonymous hash with a key/value pair (foo/1);
@array=(1,2,3,4,5);
$array[0]{"aa"}=1;
$array[0]{"bb"}=3;
$array[1]{"aa"}=5;
$array[1]{"bb"}=7;
for($i=0;$i<2;$i++)
{ print $array[$i]{"aa"}." ".$array[$i]{"bb"}."\n";} <=it's ok.
My question was, i have a hash $array[$i]{$term}, but I dont know what $term actually is, how to read every hash value.
| | Expert 2GB |
$array[$i]{$term} is not a hash.
@array is an array, and so $array[$i] is an element of that array. An array is a list of scalar values. Scalars are not hashes. Thus, you cannot use the {} syntax on them as if they were hashes.
Surprisingly though, KevinADC, his code works without strict and warnings. It produces the output
Still, because it's so nonsequitor, I have no idea how to answer his problem.
| | Expert 2GB |
@array=(1,2,3,4,5);
$array[0]{"aa"}=1;
$array[0]{"bb"}=3;
$array[1]{"aa"}=5;
$array[1]{"bb"}=7;
for($i=0;$i<2;$i++)
{ print $array[$i]{"aa"}." ".$array[$i]{"bb"}."\n";} <=it's ok.
My question was, i have a hash $array[$i]{$term}, but I dont know what $term actually is, how to read every hash value.
-
@array=(1,2,3,4,5);
-
$array[0] = {aa => 1, bb => 3};
-
$array[1] = {aa => 5, bb => 7};
-
foreach my $i (@array) {
-
foreach my $key (keys %{$i}) {
-
print "$key = $i->{$key}\n";
-
}
-
}
-
| | | -
@array=(1,2,3,4,5);
-
$array[0] = {aa => 1, bb => 3};
-
$array[1] = {aa => 5, bb => 7};
-
foreach my $i (@array) {
-
foreach my $key (keys %{$i}) {
-
print "$key = $i->{$key}\n";
-
}
-
}
-
Thanks for your answer. But in my program, I can't directly write the value of $term(which means I can't use code like {aa=>1,bb=>3), because I have thousands of words). I solved this problem by defining another @termlist, and store all the $term into it, so i can use $array[$i]{$termlist[$j]} to get what I need.
Your answer is new to me, probably helpful for me in the future. Thank you, KevinADC~
| | |
Thanks for your answer. But in my program, I can't directly write the value of $term(which means I can't use code like {aa=>1,bb=>3), because I have thousands of words). I solved this problem by defining another @termlist, and store all the $term into it, so i can use $array[$i]{$termlist[$j]} to get what I need.
Your answer is new to me, probably helpful for me in the future. Thank you, KevinADC~
What's more, use "if exists" to know whether this $array[$i] have the $array[$i]{$termlist[$j]}, for each array[$i] maps to different terms. Hope this could be helpful other newbies:)
| | Expert 2GB |
Thanks for your answer. But in my program, I can't directly write the value of $term(which means I can't use code like {aa=>1,bb=>3), because I have thousands of words). I solved this problem by defining another @termlist, and store all the $term into it, so i can use $array[$i]{$termlist[$j]} to get what I need.
Your answer is new to me, probably helpful for me in the future. Thank you, KevinADC~
These are examples you need to apply to your particular situation. All I know is what little you have said and what little you have posted. What you need is to study up on perl complex data structures in general so you better understand how they work and know what to do and what not to do.
| | Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
5 posts
views
Thread by R. Rajesh Jeba Anbiah |
last post: by
|
22 posts
views
Thread by VK |
last post: by
|
2 posts
views
Thread by Ravi |
last post: by
|
7 posts
views
Thread by dlarock@gmail.com |
last post: by
|
5 posts
views
Thread by andrewcw |
last post: by
|
24 posts
views
Thread by kdotsky@gmail.com |
last post: by
|
9 posts
views
Thread by IamIan |
last post: by
| | | | | | | | | | | | |