This channel is intended for people just starting with the Raku Programming Language (raku.org). Logs are available at irclogs.raku.org/raku-beginner/live.html Set by lizmat on 8 June 2022. |
|||
00:53
Kaiepi left
02:09
deoac joined
|
|||
deoac | $foo = '-s'; | 02:11 | |
my @arr = < alice -bob cat -soup dog>; | |||
How to I determine if any elements in @arr match $foo, without looping thru the array? | |||
02:41
deoac left
03:23
deoac joined
|
|||
pelevesque | andrewshitov.com/2019/09/09/check-...in-perl-6/ | 03:30 | |
stackoverflow.com/questions/354230...rl-6-array | |||
03:37
deoac left
05:04
Ghost10 joined
05:05
Ghost10 left
05:12
deoac joined
|
|||
deoac | But I need a partial match. '-s' isn't an element of the array. But an element contains it. | 05:14 | |
05:35
deoac left
|
|||
CIAvash | `@arr.any.&(*.contains: '-s')` | 06:02 | |
06:17
frost joined
06:47
discord-raku-bot left,
discord-raku-bot joined
06:54
Kaiepi joined
|
|||
Nahita | or `@arr.grep(*.contains: $foo).so` | 07:25 | |
08:13
dakkar joined
|
|||
lizmat | m: say < alice -bob cat -soup dog>.contains('-s') | 09:11 | |
camelia | Calling '.contains' on a List, did you mean 'needle (elem) list'? True in block <unit> at <tmp> line 1 |
||
lizmat | m: say quietly < alice -bob cat -soup dog>.contains('-s') | ||
camelia | True | ||
lizmat | though this *looks* like it is following the requirement to not loop through the array | 09:12 | |
it actually does to create the Str representation | |||
and it is actually the worst case always | |||
m: say <alice -bob cat -soup dog>.first: *.contains('-s') | 09:13 | ||
camelia | -soup | ||
lizmat | m: say so <alice -bob cat -soup dog>.first: *.contains('-s') | ||
camelia | True | ||
lizmat | would be much better, as it would short-circuit as soon as it found it, so in this case, only look at 4 of the 5 elements in the list | ||
^^ deoac | 09:14 | ||
oh, and it looks to me that the '-s' should be at the start of an element | 09:25 | ||
if that is true, then this is better: | |||
m: say so <alice -bob cat -soup dog>.first: *.starts-with('-s') | |||
camelia | True | ||
13:15
deoac joined
|
|||
deoac | Thank you lizmat. That's much better than my manual loop. | 13:17 | |
13:44
deoac left
14:08
frost left
14:55
razetime joined
15:37
razetime left
15:49
razetime joined
16:23
camelia left,
m_athias left
16:25
m_athias joined
16:27
dakkar left
16:28
camelia joined
17:05
razetime left
17:08
Kaipei joined
17:11
Kaiepi left
17:32
Kaiepi joined
17:35
Kaipei left
19:12
Kaiepi left
19:57
Kaiepi joined
22:59
Kaiepi left
23:58
Kaiepi joined
|