Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing hash of array

Status
Not open for further replies.

JJ1

Programmer
Apr 19, 2001
104
GB
Hi All,

I am trying to pass a hash which was inside an array and modify the hash in the sub-routine. Could anyone let me know whether this is the correct syntax please?

The data structure should look like this:
[red]
Code:
Mails [ARRAY]           {HASH}               {HASH}
0             --->   To (scalar)
              --->   CC (scalar)
              --->   Subject (scalar)
              --->   HTML             --->   file_loc
                                      --->   uri
                                      --->   content
1             --->   To (scalar)
              --->   CC (scalar)
              --->   Subject (scalar)
              --->   HTML             --->   file_loc
                                      --->   uri
                                      --->   content
[/red]
etc...

Here's the code I'm thinking might work:
[red]
Code:
#Call subroutine
#Sub routine should create 'uri' and 'content' keys
get_html ( \$mails[$i]{'html'} );

sub get_html{
   #Access the hash
   open ( HTML, &quot;<$html_hash->{'file_loc'}&quot; )
   ...
   #Push array onto hash, creating the 'uri' key
   $html_hash->{'uri'} = \@img;
   #Push scalar onto hash, creating the 'content' key
   $html_hash->{'content'} = $html_code;
}
[/red]

Any help is much appreciated.
Thanks,

James.
 
This does the trick. Note that: (i) I'm not using using the \ operator to generate a reference as $Mails[0]->{HTML} is already a reference; and (ii) hash keys need not be quoted within {}s or as the left side of a => if they correspond to perl variable naming rules. This can make code cleaner.

Code:
#!/usr/bin/perl

my @Mails = ( # open anonymous array
        {        # open anonymous hash $mail[0]
         To      => 'j@c.com',
         CC      => 'k@d.com',
         Subject => 'test0',
         HTML   => {            # open anonymous hash $mail[0]->HTML
                file_loc => '/path/to/file0',
                },
        },
        {        # open anonymous hash $mail[1]
         To      => 'l@e.com',
         CC      => 'm@f.com',
         Subject => 'test1',
         HTML   => {            # open anonymous hash $mail[1]->HTML
                file_loc => '/path/to/file1',
                },
        },
);

get_html( $Mails[0]->{HTML} );

sub get_html {
        my $mail = $_[0];
        open( FH, $mail->{file_loc} ) or die &quot;$0: $!&quot;;
        #...
        $mail->{URI} = \@img;
        $mail->{content} = $content;
}

Yours,


fish



&quot;As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilkes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top