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

creating array of hash..

P: 41
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
Share this Question
Share on Google+
13 Replies


rajiv07
100+
P: 141
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

P: 41
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

P: 41
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
Expert 100+
P: 410
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

P: 41
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
Expert 100+
P: 410
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

P: 41
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
Expert 100+
P: 971
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
Expert 2.5K+
P: 4,059
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

P: 41
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
Expert 100+
P: 410
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

P: 41
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
Expert 100+
P: 410
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

Post your reply

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