473,386 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

creating array of hash..

Hi,

I Have 3 arrays

Expand|Select|Wrap|Line Numbers
  1. @name=('abcv','abcdv');
  2. @add=('Mumbai','Delhi');
  3. @ph= ('838246','237546');        
  4.  
I want to create a hash like below:

Expand|Select|Wrap|Line Numbers
  1. %trg_hash = class => [
  2.             {
  3.               name => 'abcv',
  4.                    add => 'Mumbai',
  5.               ph => '838246',
  6.               dept => 'hrd'    
  7.             },
  8.             {
  9.               name => 'abcdv',
  10.                    add => 'Delhi',
  11.               ph => '237546',
  12.               dept => 'hrd'        
  13.             },
  14.               ]
Can sombody helps me.
Apr 23 '08 #1
13 3382
rajiv07
141 100+
Expand|Select|Wrap|Line Numbers
  1. use Data::Dumper;
  2.  
  3. @name=('abcv','abcdv');
  4. @add=('Mumbai','Delhi');
  5. @ph= ('838246','237546'); 
  6.  
  7. my %det;  
  8.  
  9.  
  10.  
  11. my @array=();
  12.  
  13. for $i(0..$#name){
  14. my %info;
  15. $info{name}=$name[$i];
  16. $info{add}=$add[$i];
  17. $info{ph}=$ph[$i];
  18.  
  19. push(@array,\%info)
  20. }
  21.  
  22. $det{class}=\@array;
  23.  
  24.  
  25. print Dumper %det; 
I thing this is what You are expecting.hope it help.
Regards
Rajiv
Apr 23 '08 #2
Expand|Select|Wrap|Line Numbers
  1. use Data::Dumper;
  2.  
  3. @name=('abcv','abcdv');
  4. @add=('Mumbai','Delhi');
  5. @ph= ('838246','237546'); 
  6.  
  7. my %det;  
  8.  
  9.  
  10.  
  11. my @array=();
  12.  
  13. for $i(0..$#name){
  14. my %info;
  15. $info{name}=$name[$i];
  16. $info{add}=$add[$i];
  17. $info{ph}=$ph[$i];
  18.  
  19. push(@array,\%info)
  20. }
  21.  
  22. $det{class}=\@array;
  23.  
  24.  
  25. print Dumper %det; 
I thing this is what You are expecting.hope it help.
Regards
Rajiv
Thanks rajiv
Now it is working fine...
If any issue is coming then i will contact you....
Apr 23 '08 #3
Thanks rajiv
Now it is working fine...
If any issue is coming then i will contact you....
Hi Rajiv,

I have same type of two hashes i want to merge two hases into one hash.

Can u help me.
Apr 24 '08 #4
nithinpes
410 Expert 256MB
Hi Rajiv,

I have same type of two hashes i want to merge two hases into one hash.

Can u help me.
Dilip,
Could you be more clear on your question. What do you mean by 'same type' here? In merging, do you want to just put key-value pairs of both hashes into one?
Sample data would be good.
Apr 24 '08 #5
Dilip,
Could you be more clear on your question. What do you mean by 'same type' here? In merging, do you want to just put key-value pairs of both hashes into one?
Sample data would be good.
I have 2 hash given below:

Expand|Select|Wrap|Line Numbers
  1. %hash1 = diff =>
  2.         [
  3.           {
  4.             'filename' => 'src',
  5.             'line_number' => 2,
  6.             'source_line' => '448|303 EAST',
  7.             'target_line' => ''
  8.           },
  9.           {
  10.             'filename' => 'src',
  11.             'line_number' => 4,
  12.             'source_line' => '322|2200 WEST',
  13.             'target_line' => ''
  14.           }
  15.         ];
  16.  
  17. %hash2 = diff=>
  18.         [
  19.           {
  20.             'filename' => 'trg',
  21.             'line_number' => 2,
  22.             'source_line' => '448|303 EAST',
  23.             'target_line' => ''
  24.           },
  25.           {
  26.             'filename' => 'trg',
  27.             'line_number' => 4,
  28.             'source_line' => '322|2200 WEST',
  29.             'target_line' => ''
  30.           }
  31.         ];
  32.  
  33.  
  34. after merge it will be in hash 3 like:
  35.  
  36. %hash3 = diff =>
  37.         [
  38.           {
  39.             'filename' => 'src',
  40.             'line_number' => 2,
  41.             'source_line' => '448|303 EAST',
  42.             'target_line' => ''
  43.           },
  44.           {
  45.             'filename' => 'trg',
  46.             'line_number' => 2,
  47.             'source_line' => '448|303 EAST',
  48.             'target_line' => ''
  49.           },
  50.           {
  51.             'filename' => 'src',
  52.             'line_number' => 4,
  53.             'source_line' => '322|2200 WEST',
  54.             'target_line' => ''
  55.           }
  56.           {
  57.             'filename' => 'trg',
  58.             'line_number' => 4,
  59.             'source_line' => '322|2200 WEST',
  60.             'target_line' => ''
  61.           }
  62.         ];
Apr 24 '08 #6
nithinpes
410 Expert 256MB
You need to parse through each elements of the anonymous array (which is the value for your hash key) of both hashes and push it to the anonymous array (values) ofthird hash's keys.

Though in your example, you have only one key -"diff" for both %hash1 and %hash2, the following code will also work for hashes with multiple keys.

Expand|Select|Wrap|Line Numbers
  1. use Data::Dumper;
  2.  
  3. #### defining two hashes
  4. %hash1 =( diff =>
  5. [
  6. {
  7. 'filename' => 'src',
  8. 'line_number' => 2,
  9. 'source_line' => '448|303 EAST',
  10. 'target_line' => ''
  11. },
  12. {
  13. 'filename' => 'src',
  14. 'line_number' => 4,
  15. 'source_line' => '320|2200 WEST',
  16. 'target_line' => ''
  17. }
  18. ]);
  19.  
  20. %hash2 = (diff=>
  21. [
  22. {
  23. 'filename' => 'trg',
  24. 'line_number' => 2,
  25. 'source_line' => '455|303 SOUTH',
  26. 'target_line' => ''
  27. },
  28. {
  29. 'filename' => 'trg',
  30. 'line_number' => 4,
  31. 'source_line' => '322|2200 NORTH',
  32. 'target_line' => ''
  33. }
  34. ]);
  35.  
  36.  
  37. ### creating third hash by merging values of first two hashes
  38. foreach (keys %hash1) {
  39.  @{$hash3{$_}}=();
  40.  for $i (0..$#{@{$hash1{$_}}}) {
  41.    push @{$hash3{$_}},(${$hash1{$_}}[$i],${$hash2{$_}}[$i]) ;
  42. }
  43.  
  44. }
  45.  
  46. print Dumper %hash3;
  47.  
Apr 24 '08 #7
You need to parse through each elements of the anonymous array (which is the value for your hash key) of both hashes and push it to the anonymous array (values) ofthird hash's keys.

Though in your example, you have only one key -"diff" for both %hash1 and %hash2, the following code will also work for hashes with multiple keys.

Expand|Select|Wrap|Line Numbers
  1. use Data::Dumper;
  2.  
  3. #### defining two hashes
  4. %hash1 =( diff =>
  5. [
  6. {
  7. 'filename' => 'src',
  8. 'line_number' => 2,
  9. 'source_line' => '448|303 EAST',
  10. 'target_line' => ''
  11. },
  12. {
  13. 'filename' => 'src',
  14. 'line_number' => 4,
  15. 'source_line' => '320|2200 WEST',
  16. 'target_line' => ''
  17. }
  18. ]);
  19.  
  20. %hash2 = (diff=>
  21. [
  22. {
  23. 'filename' => 'trg',
  24. 'line_number' => 2,
  25. 'source_line' => '455|303 SOUTH',
  26. 'target_line' => ''
  27. },
  28. {
  29. 'filename' => 'trg',
  30. 'line_number' => 4,
  31. 'source_line' => '322|2200 NORTH',
  32. 'target_line' => ''
  33. }
  34. ]);
  35.  
  36.  
  37. ### creating third hash by merging values of first two hashes
  38. foreach (keys %hash1) {
  39.  @{$hash3{$_}}=();
  40.  for $i (0..$#{@{$hash1{$_}}}) {
  41.    push @{$hash3{$_}},(${$hash1{$_}}[$i],${$hash2{$_}}[$i]) ;
  42. }
  43.  
  44. }
  45.  
  46. print Dumper %hash3;
  47.  

Now it is working fine
Actually I am comparing two flat files..
Now i got those lines which are different from src to trgt

if any further issue is comming i will contact you.
Apr 24 '08 #8
eWish
971 Expert 512MB
dillipkumar,

When posting code fragments and snippets please use the [CODE][/CODE] tags. It will make the code much more readable. It is expected.

Thank You,

Kevin
Apr 25 '08 #9
KevinADC
4,059 Expert 2GB
What is this supposed to do?

Expand|Select|Wrap|Line Numbers
  1. %hash3 = diff =>
That does not look like it is correct syntax.
Apr 25 '08 #10
Now it is working fine
Actually I am comparing two flat files..
Now i got those lines which are different from src to trgt

if any further issue is comming i will contact you.
Hi,

Can u tell me how to read a pdf file...

Dillip.
Apr 30 '08 #11
nithinpes
410 Expert 256MB
Hi,

Can u tell me how to read a pdf file...

Dillip.
You can make use of PDF::Core or PDF::Extract or PDF::Parse modules.
Apr 30 '08 #12
You can make use of PDF::Core or PDF::Extract or PDF::Parse modules.
Hi nithin,

Can u provide me some sample code how to read a file....

Actually there is no such module available in my server, which u have providede.

Thanks
Dillip
May 2 '08 #13
nithinpes
410 Expert 256MB
Hi nithin,

Can u provide me some sample code how to read a file....

Actually there is no such module available in my server, which u have providede.

Thanks
Dillip
If the modules are not available, you need to install them. If you are using Activestate Perl, you can install this through PPM. Else, you can also download them(from the links provided) and install.
May 5 '08 #14

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

Similar topics

4
by: Christian Hackl | last post by:
I honestly wasn't able to find an answer for this design question using Google and Google Groups, so I apologize if it is asked too frequently :) Anyway: Let's say I have a multidimensional array...
5
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",...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
22
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...
5
by: Stijn van Dongen | last post by:
A question about void*. I have a hash library where the hash create function accepts functions unsigned (*hash)(const void *a) int (*cmp) (const void *a, const void *b) The insert function...
4
by: David Bargna | last post by:
Hi I have a problem, I have a string which needs to be converted to a byte array, then have the string representation of this array stored in an AD attribute. This string attribute then has to...
38
by: djhulme | last post by:
Hi, I'm using GCC. Please could you tell me, what is the maximum number of array elements that I can create in C, i.e. char* anArray = (char*) calloc( ??MAX?? , sizeof(char) ) ; I've...
2
by: computerboy | last post by:
I am writing this program that takes a number gives it a hash humber and then put the orginal number in the array spot. ex. Enter a number: 32 Hash value is 5 so 32 is placed in the 5th spot of...
5
numberwhun
by: numberwhun | last post by:
Ok, I have a ".csv" file that contains two lines (VERY LONG lines). Basically, the first line is the headers defining what each field in the second line is. ie: Name1,Name2,Name3,...........
1
by: sixtyfootersdude | last post by:
Good Morning! I am a perl newbie and I think that I am struggling with references. I have an array of references to hashes which I am trying to print. This is what I have: for(my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.