Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using any or none on the hash keys and values in Raku

Tags:

hash

raku

I'm trying to use the any or none on the keys or values of a hash like that:

my %w=(a => 1, b => 2);
say %w.keys; # works
say so 'a' == %w.keys.any; # doesn't work

I've checked the Raku documentation's hash and map section but couldn't fix this issue. How to fix it? Thanks.

like image 774
Lars Malmsteen Avatar asked Dec 11 '22 01:12

Lars Malmsteen


2 Answers

The code dies like this:

Cannot convert string to number: base-10 number must begin with
valid digits or '.' in '⏏a' (indicated by ⏏)

This happens because == is the numeric comparison operator, so it first tries to coerce the arguments into a number before doing the comparison.

Hash keys - at least by default - are strings, thus the eq operator for string comparison is needed here:

my %w=(a => 1, b => 2);
say so 'a' eq %w.keys.any; # True
like image 74
Jonathan Worthington Avatar answered Jan 20 '23 12:01

Jonathan Worthington


use cmp operator when compares with string:

say so 'a' cmp %w.keys.any;
like image 35
chenyf Avatar answered Jan 20 '23 14:01

chenyf