Local variables lost outside of .each loop

1,198 views
Skip to first unread message

Justin Diana

unread,
Jan 30, 2017, 4:04:53 PM1/30/17
to Puppet Users
Hey guys,

I'm trying to loop through a hash to set a variable and then use it outside of the loop.  For some reason, although my debug shows the variable is getting set to the correct value, it's NULL as soon as I move up a layer in the nested loop:

$lb_info.each |$k, $v| {
        $myaddress = ''
        $myid = ''
        $v.each |$key, $value| {
                if $key == 'address' {
                        $myaddress = $value
                        notify {"this is a debug message: found ${key}: ${value}: ${myaddress}" :
                                loglevel => debug,
                        }
                }
                if $key == 'id' {
                        $myid = $value
                        notify {"this is a debug message: found ${key}: ${value}: ${myid}" :
                                loglevel => debug,
                        }
                }
        }

        notify {"this is a debug message: found ${myaddress}: ${myid} for ${k}" :
               loglevel => debug,
        }
}

Everything shows the correct values except for the last notify... it shows blank values for ${myaddress} and ${myid}.

Why is that?

Martin Alfke

unread,
Jan 30, 2017, 4:34:40 PM1/30/17
to puppet...@googlegroups.com
AFAIK all variables inside a lambda are “private”:
https://docs.puppet.com/puppet/latest/lang_lambdas.html#lambda-scope
> --
> You received this message because you are subscribed to the Google Groups "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/6b011cee-e099-4e43-a0b8-e0810bec81eb%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

jcbollinger

unread,
Jan 31, 2017, 9:03:57 AM1/31/17
to Puppet Users


On Monday, January 30, 2017 at 3:34:40 PM UTC-6, Martin Alfke wrote:
AFAIK all variables inside a lambda are “private”:
https://docs.puppet.com/puppet/latest/lang_lambdas.html#lambda-scope


... and this makes sense, because Puppet variables do not change value once set.  For variables to be assigned values at all in the context of lambdas used with iteration functions, therefore, such assignments must create variables scoped to one execution of the lambda.  With that being the case, it is natural to make that a general property of lambdas, rather than one sensitive to the lambda's usage.


John

Henrik Lindberg

unread,
Feb 1, 2017, 3:25:01 AM2/1/17
to puppet...@googlegroups.com
On 30/01/17 22:34, Martin Alfke wrote:
> AFAIK all variables inside a lambda are “private”:
> https://docs.puppet.com/puppet/latest/lang_lambdas.html#lambda-scope
>

Correct - they are local and only visible to inner scopes in the lambda
(a lambda in a lambda).

If you need an iterative function to produce a value use one of:
* filter, if the value is simply one of the values in the collection
* map, if you want a different value for each of the values in the
collection
* reduce, if you want a different number of values in the result than in
the collection

In your case, you have nested hashes and it is unclear what you expect
to do with the resulting *series* of $myaddress and $myid if you were to
change the each to something else. Also, there is no need to iterate
over a hash to find keys and values - just look up those keys.

Hope that helps,
- henrik
--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

Reply all
Reply to author
Forward
0 new messages