Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Add values to subroutine inside of a loop.

by Anonymous Monk
on Jul 02, 2018 at 15:29 UTC ( [id://1217756]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi there Monks!

I need to add the keys and values from the "$added" array of hashes to the sub routine "sent".
It gives a syntax error near "for ". How can it be done if anyone think it can?
use strict; use warnings; my $added = [ { link => "/var/www/docs", name => "mydocs", }, { link => "/var/www/read", name => "letters", } ]; my @keys = qw( link name ); my $success; foreach my $send_to(@send) { $success = sent( { to => 'send_to', account => 'to_account', name => 'name', subject => 'subject', title => 'title', for my $hash (@$added) { for my $key (@keys) { $key => $hash->{$key}, } } type => 'docs', }); }
Thanks for the Help!

Replies are listed 'Best First'.
Re: Add values to subroutine inside of a loop.
by AnomalousMonk (Archbishop) on Jul 02, 2018 at 16:11 UTC

    You seem to want to expand all of the hash refs. in the  $added array ref. into an anonymous hash, but the keys are the same in all the  $added hash referents. So, e.g., each  'link' key's value will simply overwrite the preceding value until the last such key, and likewise for all the  'name' keys.

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $added = [ { link => '/var/www/docs', name => 'mydocs', }, { link => '/var/www/read', name => 'letters', } ]; ;; my @keys = qw(link name); ;; my $hashref = { map { my $k = $_; map { $k => $_->{$k} } @$added } @keys, }; ;; dd $hashref; " { "link" => "/var/www/read", name => "letters" }
    What do you really want to do here?


    Give a man a fish:  <%-{-{-{-<

      Yes, it overwrites the last key,value, but this is what I am trying to accomplish:
      $success = sent( { to => 'send_to', account => 'to_account', name => 'name', subject => 'subject', title => 'title', link => "/var/www/docs", name => "mydocs", link => "/var/www/read", name => "letters", type => 'docs', });
        link => "/var/www/docs", name => "mydocs", link => "/var/www/read", name => "letters",

        That is not possible, because hash keys must be unique, so there can only be one key link and one key name in each hash. However, a hash value can be a reference to an array or another hash, which can hold multiple values - for example, the data structure $added shown in the OP could be a hash value. But which data structure is best here depends very much on the sent function, which you haven't shown us. Is this a function you've written yourself, or is it from an existing module? What data format does that function expect?

Re: Add values to subroutine inside of a loop.
by hippo (Bishop) on Jul 02, 2018 at 16:06 UTC

    Construct the hash with the static values first, then add the dynamic ones, then call the sub.

    use strict; use warnings; my $added = [ { link => "/var/www/docs", name => "mydocs", }, { link => "/var/www/read", name => "letters", } ]; my @keys = qw( link name ); my $success; foreach my $send_to (@send) { my %send_vals = ( to => 'send_to', account => 'to_account', name => 'name', subject => 'subject', title => 'title', type => 'docs', ); for my $hash (@$added) { for my $key (@keys) { $send_vals{$key} = $hash->{$key}; } } $success = sent (\%send_vals); }

    Untested because you haven't defined @send or sent() anywhere.

Re: Add values to subroutine inside of a loop.
by Anonymous Monk on Jul 04, 2018 at 02:55 UTC
    Obviously in the submitted code there is no definition for @send so how are any of us to be expected to meaningfully proceed?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1217756]
Approved by Corion
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-25 17:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found