Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: hash from sub directly into foreach loop

by davidj (Priest)
on Jun 30, 2006 at 08:17 UTC ( #558529=note: print w/replies, xml ) Need Help??

in reply to hash from sub directly into foreach loop

First of all, the syntax in the sub is incorrect:
%s{'a'} = 10 ; %s{'b'} = 20 ; %s{'c'} = 30 ;
should be
$s{'a'} = 10 ; $s{'b'} = 20 ; $s{'c'} = 30 ;
anyway, the following change will work for you:
#!/usr/bin/perl use strict ; use warnings ; foreach ( keys %{&testa} ) { print "key is $_\n" ; } sub testa { my $s = () ; $s->{'a'} = 10 ; $s->{'b'} = 20 ; $s->{'c'} = 30 ; return $s ; }
hope this helps,

Replies are listed 'Best First'.
Re^2: hash from sub directly into foreach loop
by GrandFather (Sage) on Jun 30, 2006 at 11:30 UTC

    Note that:

    my $s = ();

    is equivelent to

    my $s = (undef);

    which is equivelent to:

    my $s;

    The first use of a hash ref autovivifies the referred hash making the previous assignment redundant (and misleading). Consider:

    use strict; use warnings; use Data::Dump::Streamer; my $s = (); Dump ($s); $s->{'a'} = 10 ; Dump ($s);

    which prints:

    $VAR1 = undef; $HASH1 = { a => 10 };

    and note that the contents of $s changes from an undefined value to a hash ref.

    If you really want to indicate intent with an assignment use:

    my $s = {};

    DWIM is Perl's answer to Gödel
Re^2: hash from sub directly into foreach loop
by jeanluca (Deacon) on Jun 30, 2006 at 08:23 UTC
    nope, the sub should return a hash and not a hash reference. It should be the same as with
    foreach( keys CGI::Vars() )
    because that is what I'm trying to do!


      You can't "return a hash" from a subroutine, you can only return a list, the effect of return %hash is to flatten the hash into a list of key => value pairs. Thus the effect of the the three following subroutines is similar (the order of the hash keys in the first aside:)

      sub foo { my %foo = ( Foo => '1', Bar => '2'); return %foo; } + sub bar { my @bar = qw(Foo 1 Bar 2); return @bar; } + sub baz { return 'Foo','1','Bar','2'; } + print foo(),"\n"; print bar(),"\n"; print baz(),"\n";

      This is why people are telling you to return a hash reference as this is the only way to retain the, er, hashiness after the appropriate dereferencing.


      Please show working code which does what you're trying to do. When I try the following, it doesn't work:

      Q:\>perl -MCGI -le "foreach( keys CGI::Vars() ) { print }" Type of arg 1 to keys must be hash (not subroutine entry) at -e line 1 +, near ") ) " Execution of -e aborted due to compilation errors.

      When I try the following, which the documentation and davidj suggest, it works (in the sense of not being a syntax error). But that works by returning a hash reference:

      Q:\>perl -MCGI -le "foreach( keys %{ CGI::Vars() }) { print }" Q:\>

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://558529]
[LanX]: id isn't approved AFAIK
[LanX]: name works
erix feels silly for having actually tested that contiguous values make no difference in CLUSTERing a table (At least, in postges)
[LanX]: Perl Monks Approved HTML tags
[LanX]: erix and his postguess ...
[erix]: (Grrres...)
[dabella12]: I have a problem install tK on CeontOs using perl 5.24

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2017-06-24 13:32 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (557 votes). Check out past polls.