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

Passing array into a hash

by Anonymous Monk
on Mar 28, 2013 at 15:07 UTC ( #1025973=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I am trying to pass some values to a subroutine but stuck on the array part of it, here is a sample code to show the problem if anyone can help:
... errors( all_msgs => \@all_files, msg_subject => 'Error files', mail_msg => 'Listed files for processing:', ); sub errors { my (%args) = @_; my @all_msgs = $args{all_msgs} || ''; my $msg_subject = $args{msg_subject} || ''; my $mail_msg = $args{mail_msg} || ''; ... foreach my $msgs(@all_msgs) { print "$msgs\n"; ... } ... }
Thanks!

Comment on Passing array into a hash
Download Code
Re: Passing array into a hash
by toolic (Chancellor) on Mar 28, 2013 at 15:20 UTC
    Here's one way (use $all_msgs instead of @all_msgs):
    use warnings; use strict; my @all_files = 5..7; errors( all_msgs => \@all_files, msg_subject => 'Error files', mail_msg => 'Listed files for processing:', ); sub errors { my (%args) = @_; my $all_msgs = $args{all_msgs} || ''; my $msg_subject = $args{msg_subject} || ''; my $mail_msg = $args{mail_msg} || ''; foreach my $msgs (@{ $all_msgs }) { print "$msgs\n"; } } __END__ 5 6 7

      Thank you! I spaced out on getting it with "$" and not with "@".

      Thats the way:
      my $all_msgs = $args{all_msgs} || '';
      and processing it:
      foreach my $msgs (@{ $all_msgs }) { ...
      Problem solved, thanks again!
Re: Passing array into a hash
by sundialsvc4 (Abbot) on Mar 28, 2013 at 15:27 UTC

    What I usually do is to have a single parameter that is a hashref.   For example:

    use strict; use warnings; errors( { 'all_msgs' => 'foo', 'msg_subject' => 'bar', } ); sub errors { my $args = shift; my $all = $$args{'all_msgs'} || ''; ... }

    A few things to notice here, and they’re partly just stylistic:

    1. Perl lets you have a trailing-comma, as I do in my call to errors(), for the pure-and-simple reason that it's a bit easier to add new stuff after it as you revise the program.   The hash as-shown contains two keys, not three.
    2. I enclose the hash-keys in quotes, vs. the so-called “barewords.”
    3. To me, the use of use strict; use warnings; is obligatory.
    4. In the snippet of code defining errors() itself, $$args{'foo'} is a shorthand for $args->{'foo'} that I happen to prefer.

    In this example, the first (and only) argument to the subroutine is “a reference to a hash,” otherwise known simply as a “hashref.”   Perl uses the notion of a “ref” quite extensively, allowing you to build up arbitrarily complex data structures.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1025973]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2014-10-23 21:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (129 votes), past polls