map/reduce local variables

14 views
Skip to first unread message

Albert Shih

unread,
Jun 5, 2019, 11:09:22 AM6/5/19
to Puppet...@googlegroups.com
Hi


I try to write a code to delete some key inside a hash. So I got this


$_dataset = reduce($dataset.map |$key, $value| { {$key => delete($value,['remove_key1','remove_key2','remove_key3'])} }) | $result, $value | { deep_merge($result, $value)}

so if

$dataset = {
name1 => {
attr1 =>
attr2 =>
remove_key1 =>
remove_key2 =>
remove_key3 =>
attr3 =>
}
}

it will end up

$_dataset = {
name1 => {
attr1 =>
attr2 =>
attr3 =>
}
}

So it's working.

But why this *not* working ?

class (
Optional[Hash] $remove_key = ['remove_key1','remove_key2','remove_key3'],
)
{

$_dataset = reduce($dataset.map |$key, $value| { {$key => delete($value,$remove_key)} }) | $result, $value | { deep_merge($result, $value)}

}

The problem are some key will be remove, some other not. I'm unable to
understand the difference or find out why it delete some key (event not in
the remove_key array), and keep some other.

Regards.






--
Albert SHIH
DIO bātiment 15
Observatoire de Paris
xmpp: j...@obspm.fr
Heure local/Local time:
Wed 05 Jun 2019 04:57:36 PM CEST

chengkai liang

unread,
Jun 5, 2019, 8:24:46 PM6/5/19
to puppet...@googlegroups.com
I believed you made thing overly complicated. You only have one hash table, and try to remove the entries that has remove_ of it. Not sure why you need to use reduce and map. You can use `each` to go through first key of the hash table, then use filter to filter out unwanted keys like,

# this class is use to setup owner and group
class reduce_test {
  $hash = {
    attribute1 => 1,
    attribute2 => 2,
    attribute3 => 3,
    remove1    => 4,
    remove2    => 5,
    attribute4 => 6,
    attribute5 => 7,
  }
  notice("hash table => ${hash}")
  $h = $hash.filter | $items | {
    $items[0] !~ /^remove/
  }
  notice("hash table after filtering out 'remove*' key = ${h}")
}

For the each part, I will left for you to figure out.

-CK

--
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/20190605150914.GA7441%40io.chezmoi.fr.
For more options, visit https://groups.google.com/d/optout.

Albert Shih

unread,
Jun 6, 2019, 7:34:27 AM6/6/19
to Puppet...@googlegroups.com
Le 05/06/2019 à 17:09:14+0200, Albert Shih a écrit
> Hi
>
> I try to write a code to delete some key inside a hash. So I got this
>
> $_dataset = reduce($dataset.map |$key, $value| { {$key => delete($value,['remove_key1','remove_key2','remove_key3'])} }) | $result, $value | { deep_merge($result, $value)}
>
> so if
>
> $dataset = {
> name1 => {
> attr1 =>
> attr2 =>
> remove_key1 =>
> remove_key2 =>
> remove_key3 =>
> attr3 =>
> }
> }
>
> it will end up
>
> $_dataset = {
> name1 => {
> attr1 =>
> attr2 =>
> attr3 =>
> }
> }
>
> So it's working.
>
> But why this *not* working ?
>
> class (
> Optional[Hash] $remove_key = ['remove_key1','remove_key2','remove_key3'],
> )
> {
>
> $_dataset = reduce($dataset.map |$key, $value| { {$key => delete($value,$remove_key)} }) | $result, $value | { deep_merge($result, $value)}
>
> }

Why it's not working ? Because I'm stupid. I've be fool by hiera with some
key on other file.

So everything work well.

Regards.

--
Albert SHIH
Observatoire de Paris
xmpp: j...@obspm.fr
Heure local/Local time:
Thu 06 Jun 2019 01:33:12 PM CEST

Albert Shih

unread,
Jun 6, 2019, 7:36:30 AM6/6/19
to puppet...@googlegroups.com
Le 05/06/2019 à 17:24:26-0700, chengkai liang a écrit

Hi,

Thanks for the help.

> I believed you made thing overly complicated. You only have one hash table, and
> try to remove the entries that has remove_ of it. Not sure why you need to use
> reduce and map. You can use `each` to go through first key of the hash table,
> then use filter to filter out unwanted keys like,
>
> # this class is use to setup owner and group
> class reduce_test {
>   $hash = {
>     attribute1 => 1,
>     attribute2 => 2,
>     attribute3 => 3,
>     remove1    => 4,
>     remove2    => 5,
>     attribute4 => 6,
>     attribute5 => 7,
>   }
>   notice("hash table => ${hash}")
>   $h = $hash.filter | $items | {
>     $items[0] !~ /^remove/
>   }
>   notice("hash table after filtering out 'remove*' key = ${h}")
> }

Well....I understand but it's a example, actually I can't do this with
regexp. Of course I can to it with some very long test.

But I will keep your nice suggestion.

Thanks.

--
Albert SHIH
Observatoire de Paris
xmpp: j...@obspm.fr
Heure local/Local time:
Thu 06 Jun 2019 01:34:24 PM CEST
Reply all
Reply to author
Forward
0 new messages