[32901] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 4179 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Mar 23 09:09:59 2014

Date: Sun, 23 Mar 2014 06:09:04 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sun, 23 Mar 2014     Volume: 11 Number: 4179

Today's topics:
    Re: dynamically creating a hash from an array <derykus@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sat, 22 Mar 2014 22:42:38 -0700
From: Charles DeRykus <derykus@gmail.com>
Subject: Re: dynamically creating a hash from an array
Message-Id: <lgls8j$kr6$1@speranza.aioe.org>

On 3/21/2014 9:34 AM, ccc31807 wrote:
> To be honest, this may be a FAQ, but I don't seem to have the vocabulary to find it.
>
> Suppose I have a set of variables, like this:
> my $username = 'Joe';
> my $password = 'somepassword';
> my $role = 'user';
> my $session = 'q1w2e3r4t5y6';
> my $date = '2014-03-21';
> my $time = '12345678';
>
> Now, suppose I have a sub that prints the name and value of each variable, like this:
> sub debug
> {
>    my $href = shift;
>    print "-------------begin debugging data------------\n";
>    foreach my $key (sort keys %{$href})
>    {
>      print "$key = $href->{$key}\n";
>    }
>    print "-------------end debugging data------------\n";
> }
>
> Further, suppose I have an array, like this:
> my @debugarray = qw( username password role session date time );
>
> Question: how can I turn the array into a hash so I can pass a reference to the has to the sub? Like this, perhaps:
> debug($href);
>
> I suppose that my hash would look like this:
> my $href = {
>    username => $username,
>    password => password,
>    role => $role,
>    session => $session,
>    date => $date,
>    time => $time,
> };
>
> As I say, this is probably frequently asked, but I may not know how to ask the question. Thanks CC.
>

See PadWalker for caveats:

use PadWalker qw/peek_my/;
use feature 'say';

my $username = 'Joe';
 ...
my @debugarray = qw/$username $password $role $session $date $time/;
debug( peek_my(0), \@debugarray );

sub debug {
   my ($href, $aref ) = @_;
   foreach my $key (@$aref) {
       say "$key = ", ${$href->{$key}};
   }
}

-- 
Charles DeRykus


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 4179
***************************************


home help back first fref pref prev next nref lref last post