creating array of hash..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dillipkumar
    New Member
    • Mar 2008
    • 41

    creating array of hash..

    Hi,

    I Have 3 arrays

    Code:
    @name=('abcv','abcdv');
    @add=('Mumbai','Delhi');
    @ph= ('838246','237546');
    I want to create a hash like below:

    [CODE=perl]%trg_hash = class => [
    {
    name => 'abcv',
    add => 'Mumbai',
    ph => '838246',
    dept => 'hrd'
    },
    {
    name => 'abcdv',
    add => 'Delhi',
    ph => '237546',
    dept => 'hrd'
    },
    ][/CODE]

    Can sombody helps me.
    Last edited by eWish; Apr 23 '08, 12:18 PM. Reason: Please use code tags
  • rajiv07
    New Member
    • Jun 2007
    • 141

    #2
    [CODE=perl]use Data::Dumper;

    @name=('abcv',' abcdv');
    @add=('Mumbai', 'Delhi');
    @ph= ('838246','2375 46');

    my %det;



    my @array=();

    for $i(0..$#name){
    my %info;
    $info{name}=$na me[$i];
    $info{add}=$add[$i];
    $info{ph}=$ph[$i];

    push(@array,\%i nfo)
    }

    $det{class}=\@a rray;


    print Dumper %det; [/CODE]

    I thing this is what You are expecting.hope it help.
    Regards
    Rajiv

    Comment

    • dillipkumar
      New Member
      • Mar 2008
      • 41

      #3
      Originally posted by rajiv07
      [CODE=perl]use Data::Dumper;

      @name=('abcv',' abcdv');
      @add=('Mumbai', 'Delhi');
      @ph= ('838246','2375 46');

      my %det;



      my @array=();

      for $i(0..$#name){
      my %info;
      $info{name}=$na me[$i];
      $info{add}=$add[$i];
      $info{ph}=$ph[$i];

      push(@array,\%i nfo)
      }

      $det{class}=\@a rray;


      print Dumper %det; [/CODE]

      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....

      Comment

      • dillipkumar
        New Member
        • Mar 2008
        • 41

        #4
        Originally posted by dillipkumar
        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.

        Comment

        • nithinpes
          Recognized Expert Contributor
          • Dec 2007
          • 410

          #5
          Originally posted by dillipkumar
          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.

          Comment

          • dillipkumar
            New Member
            • Mar 2008
            • 41

            #6
            Originally posted by nithinpes
            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:

            [CODE=perl]%hash1 = diff =>
            [
            {
            'filename' => 'src',
            'line_number' => 2,
            'source_line' => '448|303 EAST',
            'target_line' => ''
            },
            {
            'filename' => 'src',
            'line_number' => 4,
            'source_line' => '322|2200 WEST',
            'target_line' => ''
            }
            ];

            %hash2 = diff=>
            [
            {
            'filename' => 'trg',
            'line_number' => 2,
            'source_line' => '448|303 EAST',
            'target_line' => ''
            },
            {
            'filename' => 'trg',
            'line_number' => 4,
            'source_line' => '322|2200 WEST',
            'target_line' => ''
            }
            ];


            after merge it will be in hash 3 like:

            %hash3 = diff =>
            [
            {
            'filename' => 'src',
            'line_number' => 2,
            'source_line' => '448|303 EAST',
            'target_line' => ''
            },
            {
            'filename' => 'trg',
            'line_number' => 2,
            'source_line' => '448|303 EAST',
            'target_line' => ''
            },
            {
            'filename' => 'src',
            'line_number' => 4,
            'source_line' => '322|2200 WEST',
            'target_line' => ''
            }
            {
            'filename' => 'trg',
            'line_number' => 4,
            'source_line' => '322|2200 WEST',
            'target_line' => ''
            }
            ];[/CODE]
            Last edited by eWish; Apr 25 '08, 03:33 AM. Reason: Please use code tags

            Comment

            • nithinpes
              Recognized Expert Contributor
              • Dec 2007
              • 410

              #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.

              Code:
              use Data::Dumper;
              
              #### defining two hashes
              %hash1 =( diff =>
              [
              {
              'filename' => 'src',
              'line_number' => 2,
              'source_line' => '448|303 EAST',
              'target_line' => ''
              },
              {
              'filename' => 'src',
              'line_number' => 4,
              'source_line' => '320|2200 WEST',
              'target_line' => ''
              }
              ]);
              
              %hash2 = (diff=>
              [
              {
              'filename' => 'trg',
              'line_number' => 2,
              'source_line' => '455|303 SOUTH',
              'target_line' => ''
              },
              {
              'filename' => 'trg',
              'line_number' => 4,
              'source_line' => '322|2200 NORTH',
              'target_line' => ''
              }
              ]);
              
              
              ### creating third hash by merging values of first two hashes
              foreach (keys %hash1) {
               @{$hash3{$_}}=();
               for $i (0..$#{@{$hash1{$_}}}) {
                 push @{$hash3{$_}},(${$hash1{$_}}[$i],${$hash2{$_}}[$i]) ;
              }
              
              }
              
              print Dumper %hash3;
              Last edited by nithinpes; Apr 24 '08, 12:03 PM. Reason: Added comments

              Comment

              • dillipkumar
                New Member
                • Mar 2008
                • 41

                #8
                Originally posted by nithinpes
                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.

                Code:
                use Data::Dumper;
                
                #### defining two hashes
                %hash1 =( diff =>
                [
                {
                'filename' => 'src',
                'line_number' => 2,
                'source_line' => '448|303 EAST',
                'target_line' => ''
                },
                {
                'filename' => 'src',
                'line_number' => 4,
                'source_line' => '320|2200 WEST',
                'target_line' => ''
                }
                ]);
                
                %hash2 = (diff=>
                [
                {
                'filename' => 'trg',
                'line_number' => 2,
                'source_line' => '455|303 SOUTH',
                'target_line' => ''
                },
                {
                'filename' => 'trg',
                'line_number' => 4,
                'source_line' => '322|2200 NORTH',
                'target_line' => ''
                }
                ]);
                
                
                ### creating third hash by merging values of first two hashes
                foreach (keys %hash1) {
                 @{$hash3{$_}}=();
                 for $i (0..$#{@{$hash1{$_}}}) {
                   push @{$hash3{$_}},(${$hash1{$_}}[$i],${$hash2{$_}}[$i]) ;
                }
                
                }
                
                print Dumper %hash3;

                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.

                Comment

                • eWish
                  Recognized Expert Contributor
                  • Jul 2007
                  • 973

                  #9
                  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

                  Comment

                  • KevinADC
                    Recognized Expert Specialist
                    • Jan 2007
                    • 4092

                    #10
                    What is this supposed to do?

                    Code:
                    %hash3 = diff =>
                    That does not look like it is correct syntax.

                    Comment

                    • dillipkumar
                      New Member
                      • Mar 2008
                      • 41

                      #11
                      Originally posted by dillipkumar
                      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.

                      Comment

                      • nithinpes
                        Recognized Expert Contributor
                        • Dec 2007
                        • 410

                        #12
                        Originally posted by dillipkumar
                        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.

                        Comment

                        • dillipkumar
                          New Member
                          • Mar 2008
                          • 41

                          #13
                          Originally posted by nithinpes
                          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

                          Comment

                          • nithinpes
                            Recognized Expert Contributor
                            • Dec 2007
                            • 410

                            #14
                            Originally posted by dillipkumar
                            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.

                            Comment

                            Working...