By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,903 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,903 IT Pros & Developers. It's quick & easy.

How to print out this kind of hash array?

P: 30
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!
Sep 13 '08 #1
Share this Question
Share on Google+
7 Replies


KevinADC
Expert 2.5K+
P: 4,059
I don't think you can do what you are attempting. First you have an array:

Expand|Select|Wrap|Line Numbers
  1. @array=(1,2,3,4,5,6);
even assuming $i and $term are valid values, this will not work:

Expand|Select|Wrap|Line Numbers
  1. $array[$i]{$term}=1;
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:

Expand|Select|Wrap|Line Numbers
  1. @array=(1,2,3,4,5,6);
  2. $i = 0;
  3. $term = 'foo';
  4. $array[$i]={$term=>1};# creates the hash and one key/value pair
  5. print $array[$i][$term}; 
  6.  
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);
Sep 13 '08 #2

P: 30
I don't think you can do what you are attempting. First you have an array:

Expand|Select|Wrap|Line Numbers
  1. @array=(1,2,3,4,5,6);
even assuming $i and $term are valid values, this will not work:

Expand|Select|Wrap|Line Numbers
  1. $array[$i]{$term}=1;
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:

Expand|Select|Wrap|Line Numbers
  1. @array=(1,2,3,4,5,6);
  2. $i = 0;
  3. $term = 'foo';
  4. $array[$i]={$term=>1};# creates the hash and one key/value pair
  5. print $array[$i][$term}; 
  6.  
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.
Sep 14 '08 #3

Ganon11
Expert 2.5K+
P: 3,652
$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

Expand|Select|Wrap|Line Numbers
  1. 1 3
  2. 5 7
Still, because it's so nonsequitor, I have no idea how to answer his problem.
Sep 14 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
@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.
Expand|Select|Wrap|Line Numbers
  1. @array=(1,2,3,4,5);
  2. $array[0] = {aa => 1, bb => 3};
  3. $array[1] = {aa => 5, bb => 7};
  4. foreach my $i (@array) {
  5.    foreach my $key (keys %{$i}) {
  6.       print "$key = $i->{$key}\n";
  7.    }
  8. }
  9.  
Sep 14 '08 #5

P: 30
Expand|Select|Wrap|Line Numbers
  1. @array=(1,2,3,4,5);
  2. $array[0] = {aa => 1, bb => 3};
  3. $array[1] = {aa => 5, bb => 7};
  4. foreach my $i (@array) {
  5.    foreach my $key (keys %{$i}) {
  6.       print "$key = $i->{$key}\n";
  7.    }
  8. }
  9.  

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~
Sep 14 '08 #6

P: 30
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:)
Sep 14 '08 #7

KevinADC
Expert 2.5K+
P: 4,059
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.
Sep 14 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.