|
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. |
|||
|
02:27
MasterDuke joined
03:25
MasterDuke left
|
|||
| pelevesque | Can someone point me in the right direction? Maybe the right Raku way to do this. Maybe just not thinking to sharp cause sick. I have a file that has text and numbers. say: My number is 4, and another 4.4 and 4 again, and also 0 2 and 1.3 I have an array with the same amount of numbers. Say [2.2, 0, 4, 4.4, 2, 0] I want to run throught the file replacing the numbers with the one in the array | 22:39 | |
| giving: | |||
| My number is 2.2, and another 0 and 4 again, and also 4.4 2 and 0 | |||
| Not asking for a solution, just on a general way of doing this. Iv'e been kind of hitting my head against the wall with substitutions. | 22:40 | ||
| ab5tract | pleveque: so the first thing that comes to mind is to use a hash. the key would be the number from the file to replace, the value would be the number to replace it with | 22:43 | |
| then you could do `for %replacers.kv -> $to-replace, $replace-with { $text.subst($to-replace, $replace-with) }` | 22:45 | ||
| but you'll want to use `$text.subst($to-replace, $replace-with, :g)` to replace globally | 22:46 | ||
| pelevesque | I'll try it just after sleeping a bit. I think I have this annoying thing called covid. | 23:01 | |
| ab5tract | ooof.. that's never fun | 23:05 | |
| good luck! and don't hesitate to ping with any further questions | |||
| pelevesque | Thanks! | 23:13 | |
| Very appreciated. | |||
| Does your idea replace one by one, without going back in time? Without always starting back at the beginning of the text. | 23:32 | ||
| Because if we always start again from the beginning, then it fails. | 23:33 | ||
| Not sure if you see what I mean. | |||
| I need to replace the first found number, with the first number in my array of replacements, then the second found number with the second one, and so on. | |||
| ab5tract | the snippet I shared replaces all appearances of A with A', then B with B', etc | 23:41 | |
| that's what the :g is for | |||
| (it stands for `global`) | 23:42 | ||
| so it more or less requires that none of the to-replace values are present in the replace-with values | 23:58 | ||
| if that's a requirement, we'll need to figure something else out involving a cursor, but it still shouldn't be that bad | 23:59 | ||