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:14 Heptite left 00:53 Manifest0 left 01:37 QhpAptyj9hj0RQwM left 03:29 razetime joined 03:42 Guest47 joined 04:22 Kaipei left 04:32 razetime left 05:22 razetime joined 06:35 razetime left 07:38 Kaipei joined 10:51 QhpAptyj9hj0RQwM joined 10:55 QhpAptyj9hj0RQwM left 10:56 QhpAptyj9hj0RQwM joined 12:10 subben-troska joined
subben-troska hello I have an issue trying to convert a json line into an hash... 12:12
# { id: 152, name: 'Kochstraße', type: 'PT', description: 'station', entries: 3 },
for stations.lines -> $station {
  if "id: 3" ~~ m:g/$<key>=\w+':'\s*$<value>=\d+/ {
    my %h = $/.hash;
  }
}
sub stationer { 12:14
    return return q:to/END/;
  { id: 1, name: 'xxxxöö', type: 'TB1', description: 'surface', entries: 10 },
  { id: 2, name: 'yyyyåå', type: 'TB3', description: 'semi-surface', entries: 10 },
...
I can't make the global matching in conjunction with hash to work 12:15
I would like to access like "%h{'name'}", however I can't make it to work.. 12:18
Nemokosch isn't it a better idea to use a proper library for that?
subben-troska my original regex: /(\w+)\:\s*\'?(<[\w\s\-]>+)\'?/ 12:19
well.. in java it would be very easy :)
just using find() on matcher
I thought it used to be easy in perl5 12:20
which I stopped using in 2015
Nemokosch I'm not terribly fond of Java regex API but anyways, if there is a better choice, why suffer?
subben-troska suffer is living;-)  .. aaahhh it shouldn't be hard 12:21
its just a consecutive matcher of pairs to turn into a hash 12:22
Nemokosch perhaps it's not hard but please... if that's the only argument against using JSON::Fast...
subben-troska ok! I give up! I will go for your suggestion :)
I just saw it some 30 mins ago
lizmat use JSON::Fast; for station.lines { my %h := from-json($_) } 12:23
subben-troska Just to mention the format wasn't json from the beginning but the data is meant to be inserted in a TS mock application 12:24
thanks both of you discord-raku-bot (bot?) and lizmat!! you are my heroes (shouldn't waste too much Christmas-time with Raku-suffering) 12:26
Nemokosch by the way, I did start up a REPL to check what it returns 12:27
lizmat subben-troska: there's benefit in trying to do stuff yourself from scratch
subben-troska great :)
Nemokosch perhaps m:g always returns a List of matches?
lizmat but the JSON "standard" has all sorts of nooks and crannies, you seriously don't want to get into
Nemokosch and turning a one-element List into a Map... won't work out well 12:28
lizmat having spent quite some time on optimising JSON::Fast, I think I'm qualified to say that :-)
Nemokosch so if you wanted to m:g on all the lines, then you were pretty close probably 12:29
subben-troska nah... I used to be a perl5-geek digging deep.. now I am like a blank paper
ok?
close
Nemokosch it's just what you get is a list of matches, not a big match of lists
subben-troska uhu? list of matches 12:30
Nemokosch so you could call .hash on individual elements of that list, e.g with map or if you really feel like some cool APL folk, with the hyper meta-operator 12:31
subben-troska REPL.. is it regex perl or regex Raku.. I am always using rubular (by default)
Nemokosch what is rubular? 🤣 12:32
sounds like some old east-german truck
subben-troska well working at E/// back then in a test team we used that to check regex'es (while it doest work out for raku) 12:33
rubular.com 12:34
it's ruby?
Nemokosch seems like it is. Yet another language that is slightly getting nostalgic. 12:35
subben-troska "hyper-meta-operator"?
Nemokosch hyper is the name, if anything
subben-troska well my only tools at the moment is "comma-ct", is there a test regex-online (..well offline) for Raku? 12:36
Nemokosch I never tried this with a heredoc so wish me luck... 12:37
m: q:to/END/ ~~ m:g/$<key>=\w+':'\s*$<value>=\d+/;
{ id: 1, name: 'xxxxöö', type: 'TB1', description: 'surface', entries: 10 },
{ id: 2, name: 'yyyyåå', type: 'TB3', description: 'semi-surface', entries: 10 },
{ id: 3, name: 'tire', type: 'TB1', description: 'non-surface', entries: 2 },
END
dd $/>>.hash;
well, I did get a huge output 😄 12:38
perhaps dd was a bit overkill
m: q:to/END/ ~~ m:g/$<key>=\w+':'\s*$<value>=\d+/;
{ id: 1, name: 'xxxxöö', type: 'TB1', description: 'surface', entries: 10 },
{ id: 2, name: 'yyyyåå', type: 'TB3', description: 'semi-surface', entries: 10 },
{ id: 3, name: 'tire', type: 'TB1', description: 'non-surface', entries: 2 },
END
say $/>>.hash;
yeah, this is enough
I don't think the bot output gets posted on IRC but you can run the same thing, there is an evalbot on that end as well 12:39
subben-troska oki :)
..yeah.. ?! .. implementing .. (still slow with the MBP) 12:44
ok, so the problem is that the global matcher creates ONE hash for EACH PAIR instead of ONE hash for EACH SET of pairs? 12:50
(Map.new((key => 「id」, value => 「1」)) Map.new((key => 「entires」, value => 「10」)))
(Map.new((key => 「id」, value => 「2」)) Map.new((key => 「entires」, value => 「10」)))
(Map.new((key => 「id」, value => 「3」)) Map.new((key => 「entires」, value => 「6」)))
(Map.new((key => 「id」, value => 「4」)) Map.new((key => 「entires」, value => 「14」)))
Nemokosch what is MBP again? 12:52
I don't know, I don't see any problems 🙂 12:53
subben-troska MacBook I am a pc/linux guy
from the very start
   if $station ~~ m:g/$<key>=\w+':'\s*$<value>=\d+/ { 12:54
  say $/>>.hash;
  my %h = $/>>.hash;
  say %h{'id'}
Nemokosch haha, don't get me started on Apple
subben-troska ???
but the BATTERY life , the BEAUTIFUL screen .. its the perfect laptop, but my greedy company just let me off with a 14" :( 12:55
I haven't dusted off my hash'ing skills.. but the %h yields 'Any' for 'id' 12:56
(Map.new((key => 「id」, value => 「135」)) Map.new((key => 「gateCount」, value => 「5」)))
(Any)
12:56 QhpAptyj9hj0RQwM left
Nemokosch I mean, the field name is "key", you yourself chose that 12:56
and it's still not a Map but a List of Maps 12:57
subben-troska oohhhh?!..oh 'key'.. well. aha .. key/value - right named capture
right got you it's a list of single entry maps
pity.. 12:58
Nemokosch > my %h = $/>>.hash;
this line is still sort of NFSW
NSFW*
hey, you *could* do what you actually want to do 12:59
subben-troska Its not really work its just me turning data to match the mocked data
of the react app
Nemokosch I think this is a good example of habits that drift one towards wrong solutions
subben-troska ok, how? a half-liner :-) 13:00
Nemokosch It seems you don't want multiple matches but one big match
subben-troska yeah I know I am sucker of cryptic solutions.. but it will not show in the real app
... we'll sort of one big match I am getting your point. 13:01
the lists should be merged into a map
Nemokosch it doesn't help with the case that you only want to match some arbitrary parts of the objects 13:02
subben-troska I thought that was possible, because I went from PERL5 to Java back in -15 and I thought of the transition and about the possibility to easily use hashes like in JAVA. but I can't remember PL5
Its all about matching pairs of k/v into map 13:03
ok! I get your point about arbitrary order..
Nemokosch I mean if you matched the whole thing, the named parts of that one match would be lists alright 13:04
but it's like match a little bit, then another little bit etc
subben-troska yes a list of unordered entries (which are in order)
I SHOULD turn into a complete regex matching the order right? 13:05
Nemokosch not just that. some keys aren't properly matched because their value is not integer
subben-troska not a global consecutive one.
Nemokosch and therefore there are gaps inherently
subben-troska yeah that why I had the optional \'? before and after
well it was in another regex.. 13:06
if $station ~~ m:global/(\w+)\:\s*\'?(<[\w\s\-]>+)\'?/ {
...which isn't working... its a conversion from a previous 13:07
non global context like; if $station ~~ m:global/(name)\:\s*\'?(<[\w\s\-]>+)\'?/ {
Nemokosch ' starts a string though, doesn't it 13:08
subben-troska regex's are much harder in PL6 than PL5. We actually had a PL5 course with Mäsak back than (from Edument) 13:09
Nemokosch '?(<[\w\s-]>+)'? is the string `?(<[\w\s-]>+)` optionally
subben-troska It was the ' that are optional around the values 13:10
Nemokosch I get that that was what you wanted but I doubt that's what you managed to express 13:11
13:11 jgaz joined
subben-troska Mmmmm... maybe its been a mess.. 13:11
Nemokosch I'd say the rules are fairly simple. Any character that isn't alphanumeric, must be escaped to be used literally. That's it.
you can escape it with the backslash or by *putting it into a string literal* 13:12
also, regex literals are whitespace-insensitive so you can write them in a way that doesn't look like a long hieroglyph 13:13
subben-troska allright.. sounds great.. I would really like to see a (laugh if you want, its on the house ;-) ) "rubular" for perl6.. besides REPL 13:14
ok? so no more \s'es then?
\s*+.. 13:15
Nemokosch that's not what I meant
By the way: what are your priorities? To solve a specific task or to get to know more about Raku regexes?
I mean both is fine but that decides how I try to govern the topic 😛 13:16
subben-troska well I would like to dust off perl and more over learn RAKU and how things works out
Nemokosch Okay, gotcha 13:17
subben-troska I am doing this undercover, some members of the team knows I am into this RAKU thing ;-)
Nemokosch disclaimer: the topic is covered in much greater detail in the docs - docs.raku.org/language/regexes 13:18
now, the most important things about writing Raku regexes are: 13:20
- they are constructed with this regex language; prior to bleeding-edge works, a string literal and a regex literal had no connection to each other
- a regex literal can contain strings and even code blocks and it ignores whitespace 13:21
- alphanumeric characters don't have to be escaped but everything else (including whitespace) has to be
subben-troska great @Nemokosch my angry/tired gf just called to get picked up (from work) and I need to warm up my old limping French c4gp car so I have to leave for now.. 13:22
but I also got your last point about those two kinds of literals..
I am still here reading.. (there were some problems with the car .. lol)
Nemokosch for what it's worth, I tend to add everything as a string - except for the regex control characters I want to use in their special meaning
subben-troska ok.. in my case do you and for the moment recommend using json::fast?? just to not get further into some misconceptions like you are pointing out to me? 13:24
I want to get it right and according to your teaching :-)
I mean the way it works in RAKU.. correcgiton 13:25
Nemokosch you seem to be kind of a funny person lol
subben-troska I am dynamic
Nemokosch but it's okay, new year's eve is close
subben-troska yeah it is.. the code is always present to the mind 13:26
well I better be going, have a great NY eve, I will try myself ;-)
Nemokosch well, if what you want to process is valid JSON, that's probably the safest choice, and frankly it might even turn out to be faster, not sure about the implementation
subben-troska I'll have a go after the pickup 13:27
Nemokosch okay okay
take care
13:37 subben-troska left 14:29 Guest47 left 15:42 jgaz left 15:44 razetime joined 15:51 razetime left 15:55 razetime joined 17:58 razetime left 23:10 lizmat joined
I can't vouch for the books but there is the documentation itself 23:10
CodeTortoise Right. The website has a lot of examples, and the documentation is well done, so I should not have too much trouble finding answers to any questions I have. Raku seems to be anti-idiomatic, or pro-idiosyncratic, so I don't have to worry about style I guess. 23:13
23:53 lizmat_ joined, jgaz joined
Nemokosch also, feel free to ask if something is not clear. Mistakes are very much possible when you have a large language with a small crew 😛 23:56
23:56 lizmat left
CodeTortoise For sure! I mean, I have been in a kind of beginner's hell sampling languages when I got interested in programming like.. ten years ago. Never pursued it seriously, I mostly just enjoyed reading about different rationales and implementations, but just like learning an instrument, knowing how to make sounds but not how to play songs is terrible. 23:58