in reply to How to split a non even number of string elements into a hash [RESOLVED]

Hi thanos1983,

In the spirit of TIMTOWTDI: just yesterday I published some code that uses m/\G.../gc to incrementally parse a string into a hash. The goal of that code is to parse a string like "RH= 23.8 %RH T= 20.4 'C Td= -1.0 'C Tdf=-0.9 'C a=  4.2 g/m3   x=   3.5 g/kg  Tw= 10.2 'C ppm=  5635 pw=   5.68 hPa pws=  23.90 hPa h=  29.5 kJ/kg" (a line of data from a sensor I have to parse) into a data structure like { RH => { val=>23.8, unit=>"%RH" }, ... }. Although this is probably overkill for your case, note how it does allow for more powerful parsing of the input - in the aforementioned example, note how "ppm=  5635" doesn't have units, and how a simple split on whitespace won't work for "Tdf=-0.9".

If I apply the same principle to your code (slightly simplified):

use warnings; use strict; my $input = "one 1 two 2 three 3 odd_element"; my $REGEX = qr{ \s* \b (\w+) \s+ (\w+) \b \s* }msx; my %output; pos($input)=undef; while ($input=~/\G$REGEX/gc) { $output{$1} = $2; } my $rest = substr $input, pos $input; if (length $rest) { # leftovers $output{$rest} = 'turkeysandwich'; } use Data::Dumper; print Dumper(\%output); __END__ $VAR1 = { 'one' => '1', 'two' => '2', 'three' => '3', 'odd_element' => 'turkeysandwich' };

(Note: Assigning to pos is not needed in the example above, but it may become necessary if the code is embedded into a larger script that performs other regexes on the input that may modify pos.)

Hope this helps,
-- Hauke D