In Perl, there is usually more than one way to do this, and since your problem involves parsing, one solution, of course, has a regex:
my $text = "width=13\nheight=15\nname=Mirek"; $text ~~ / [(\w+) \= (\N+)]+ %% \n+ /; my %params = $0>>.Str Z=> $1>>.Str;
Another useful tool for extracting data is comb() , which produces the following single-line file:
my %params = $text.comb(/\w+\=\N+/)>>.split("=").flat;
You can also write your original approach this way:
my %params = $text.split("\n")>>.split("=").flat;
or even simpler:
my %params = $text.lines>>.split("=").flat;
In fact, I would probably go with this until your data format becomes more complex.
source share