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.
01:52 archenoth left 01:55 Kaipei joined 02:08 archenoth joined 04:09 Heptite left 06:22 archenoth left 06:23 archenoth joined 08:19 PolarBearXL joined 08:47 Kaipii joined 08:51 Kaipei left 09:05 discord-raku-bot left, discord-raku-bot joined
rcmlz Hello, I am trying to do a short script that scambles text, preserving word order and keeping the first and the last letter of each word at its place. 09:11
# keep first and last letter, permutate letters in the middle at random
my $INPUT = "A longer sentence\nOFF words. And some more!";
my $EXAMPLE_OUTPUT = "A lgoner sntceene OFF wdors. And smoe mroe!";
My current solution is:
say $INPUT.lines.words.map(.subst(/(\w)(\w)(\w?)(<[. , ! ?]>?)/, {$0 ~ $1.flip ~ $2 ~ $3})).join(" ");
I have two issues
a.) the regex consumes the entire middle part, so the last character of each word is not kept at the last position. I tried to use <ww> (within words) to make the regex a bit more appealing, but failed.
b.) using flip works, but I fail using roll or permutations.
Can anyone give me a hint on the two issues?
Thank you
lizmat m: say .substr(0,1) ~ .substr(1,*-1).comb.pick(*).join ~ .substr(*-1) given "frobnicate" 09:15
camelia fonbtcriae
lizmat m: say .substr(0,1) ~ .substr(1,*-1).comb.pick(*).join ~ .substr(*-1) given "frobnicate"
camelia foirantbce
lizmat I wouldn't use a regex for this 09:16
.comb separates the letters, .pick(*) randomly orders them, .join puts them together again 09:17
09:18 discord-raku-bot left 09:19 discord-raku-bot joined 09:27 Kaipii is now known as Kaiepi 09:35 frost24 joined
rcmlz Thank you. 10:04
m:
my $INPUT = "AbcdefG";
say .substr(1,*-1) given $INPUT;
say .substr(1,*-1).comb given $INPUT;
say .substr(1,*-1).comb.pick given $INPUT;
say .substr(1,*-1).comb.pick.join given $INPUT;
I get the same wrong output at my machine running 10:05
Welcome to Rakudo™️ v2022.07.
Strange that sting.comb.pick.join it worked when you executed it. 10:06
Strange that sting.comb.pick.join worked when you executed it.
(the asterixes get lost when using Discord, but I put them in) 10:08
10:08 frost24 left
(the asterixes before -1 get lost when using Discord, but I put them in) 10:10
lizmat that would explain :-) 10:11
also: .pick(*) with an asterisk! 10:12
otherwise you'd only get one letter
Nemokosch would .pick(Inf) work the same way?
rcmlz ahhh, that is it. I was wondering why you used pick() and not pick ... 10:15
m:
my $INPUT = "AbcdefG";
say .substr(1,*-1).comb.pick(*).join given $INPUT;
say .substr(1,*-1).comb.pick(Inf).join given $INPUT;
(with * in pick() and before -1)
lizmat m: say .substr(0,1) ~ .substr(1,*-1).comb.pick(Inf).join ~ .substr(*-1) given "frobnicate"
camelia fnibotcrae
lizmat Inf works
Nemokosch discord.com/channels/5384078799804...3503830016 toldya 😝 10:16
rcmlz yes, I was not aware of that issue.
Nemokosch can a substitution used with a callable? 10:17
lizmat m: say "foo bar".subst(/ \w+ /, { .uc }) # like that you mean? 10:18
camelia FOO bar
Nemokosch > The replacement can be a Callable in which the current Match object will be placed in the $/ variable, as well as the $_ topic variable.
cool 10:19
yep
something is weird 10:27
still investigating in case I did something wrong
aha 10:29
10:29 PolarBearXL left
the S/// syntax can't be used with callables? 10:29
or perhaps s/can't/shouldn't/ 10:30
lizmat fwiw, I'm no fan of S/// 10:32
m: say S/ \w+ / { .uc } / given "foo bar"
camelia FOO BAR bar
lizmat looks like $_inside the { } is the whole string ?
m: say S/ \w+ /{ $/.uc }/ given "foo bar" 10:33
camelia FOO bar
Nemokosch yes, it does seem like that... 10:37
it seems like S/// does not set $_ at all
rcmlz Simplifying my code using Liz hint I derived at 10:47
my $INPUT = "A longer sentence\nOFF words. And some more!";
say $INPUT.subst(:g, /\w+\w+/, {.substr(0,1) ~ .substr(1,*-1).comb.pick(*).join ~ .substr(*-1)});
The only thing I am not happy with is the \w+\w+ to exclude single letters like the "A" - as .subst() is not working otherwise.
Thank you for your help.
Simplifying my code using Liz hint I derived at 10:48
my $INPUT = "A longer sentence\nOFF words. And some more!";
say $INPUT.subst(:g, /\w+\w+/, {.substr(0,1) ~ .substr(1,\*-1).comb.pick(\*).join ~ .substr(\*-1)});
The only thing I am not happy with is the \w+\w+ to exclude single letters like the "A" - as .subst() is not working otherwise.
Thank you for your help.
lizmat m: say .substr(0,1) ~ .substr(1,*-1).comb.pick(Inf).join ~ .substr(*-1) given "f" 10:50
camelia Number of characters argument to substr out of range. Is: -1, should be in 0..^Inf; use *-1 if you want to index relative to the end
in block <unit> at <tmp> line 1
lizmat hmmm
I guess you could put in a check for number of chars 10:51
rcmlz there is no second character subst(1,*-1) crashes 10:52
lizmat add a .chars > 3 as any smaller word doesn't make sense anyway
\w ** 4..* 10:53
or use that ^^
rcmlz I tried that 10:54
my $INPUT = "A longer sentence\nOFF words. And some more!";
say $INPUT.subst(:g, /\w+/, {.substr(0,1) ~ .substr(1,\*-1).comb.pick(\*).join ~ .substr(\*-1) if .chars > 1});
but then single letters are omitted.
Nemokosch yeah liz is right 10:56
your condition returns Empty when the condition fails
that's why they get omitted
it's better to not match them at all
rcmlz Yes, /\w ** 4..*/ did the trick 10:58
No I have a nice, short solution! Thank you.
Now I have a nice, short solution! Thank you.
11:13 Kaiepi left 11:15 Kaiepi joined 11:30 frost53 joined
Nemokosch 🥳 11:42
11:56 frost53 left 11:57 frost70 joined 12:27 frost70 left 12:50 jgaz joined 14:25 Heptite joined 16:21 Kaiepi left 17:06 deoac left 17:15 Kaiepi joined 19:02 n1to joined 19:03 n1to left 19:11 jgaz left 19:32 jgaz joined 20:22 jgaz left 21:31 Nemokosch joined 22:51 jaguart joined 23:04 deoac joined 23:20 deoac left