473,626 Members | 3,191 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to print out this kind of hash array?

30 New Member
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
7 2648
KevinADC
4,059 Recognized Expert Specialist
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
anklos
30 New Member
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
3,652 Recognized Expert Specialist
$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
4,059 Recognized Expert Specialist
@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
anklos
30 New Member
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
anklos
30 New Member
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
4,059 Recognized Expert Specialist
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

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

Similar topics

5
2014
by: R. Rajesh Jeba Anbiah | last post by:
I could see that it is possible to have hash array using objects like var hash = {"a" : "1", "b" : "2"}; Couldn't still findout how to declare hash array in Array. var arr = new Array("a" : "1", "b" : "2"); doesn't work. Any hints? TIA -- <?php echo 'Just another PHP saint'; ?> Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/
22
4599
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last month I had enough of extra proof that the cat doesn't hount mice anymore in more and more situations. And the surrent sicretisme among array and hash is the base for it. I summarized all points in this article:...
2
2520
by: Ravi | last post by:
Hi, I am working on a winform app. I need to use an object which can store some information(key/value pairs) and also can be acessed by multiple threads(read/write). From what I heard Hash table is best suited for it. My question is Why hash table. why can't we use an array. I thought hash table uses more memory than array. Correct me if am wrong.
7
17038
by: dlarock | last post by:
I wrote the following to do an MD5 hash. However, I have a problem (I think) with the conversion from the Byte MD5 hash back to string. Watching this through the debugger it appears as if the MD5 is computing the right Byte for the hash when compared to other MD5 hash generators online. However, when I attempt to convert it back to tring using the line String outputData = textConverter.GetString( result ) ; I essentially get...
5
3491
by: andrewcw | last post by:
I have a VB 5 module that duplicates the FCIV.exe from Microsoft. I need to move an application forward to C#, but the samples for MD5 hash using the framework I tried gave different hashes. What do I feed the framework and how do I get the same values ?? The following code DOES NOT GIVE THE FCIV value: public string getFileHash(string filePath) { string retVal = "";
24
4283
by: kdotsky | last post by:
Hello, I am using some very large dictionaries with keys that are long strings (urls). For a large dictionary these keys start to take up a significant amount of memory. I do not need access to these keys -- I only need to be able to retrieve the value associated with a certain key, so I do not want to have the keys stored in memory. Could I just hash() the url strings first and use the resulting integer as the key? I think what I'm...
9
1838
by: IamIan | last post by:
I'm using an array to store map features (name, lat, lon, caption, etc), from which the user can then select an individual feature. The problem is that when thousands of features are stored in the array, looping over the entire array looking for a match is SLOW. So I'm running a hash in parallel, where every time a feature is pushed onto the array it's name is also added to the hash as an identical key value pair. I then check if the key...
3
2356
by: cdfd1234 | last post by:
suppose u have file File A A -> G C->D A -> R P->A File B
11
4201
by: JWest46088 | last post by:
I'm having difficulty trying to figure out how to print a text file from a hash table one line at a time. I have the text file read into the hash table and can print the text file all at once, but I can't seem to figure out how to do it one line at a time. Here is what I'm trying to do: I want the user to be able to print the text file one line at a time by clicking a button to see the next line. Example: If text_file1 first line is...
0
8268
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8641
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8366
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5575
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4202
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1812
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.