Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
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!

Replies are listed 'Best First'.
Re: Passing array into a hash
by toolic (Bishop) 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?
[moritz]: choroba in python I use jsonschema
[moritz]: I'm sure there's a Perl implementation of it too
[choroba]: it seems Kelp and similar help you a lot with dispatching, but how to do something similar with contents?
[moritz]: like JSON::Schema
[Corion]: ambrus++ # more bugs than lines
[Corion]: That's like "works on my machine" ;-)
[choroba]: I mean, I'd like to say "this path should run this sub, but first it should check the user is authenticated, has the following permissions, and the contents is valid against a given schema
[ambrus]: no, that means I haven't even tried to run it, might not even parse
[ambrus]: so it doesn't work on my machine
[choroba]: i.e. I know JSON::Schema, I'm just curious whether there's a ready solution to plug it into a web service

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (11)
As of 2016-12-08 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (141 votes). Check out past polls.