in reply to splitting string based on length index

Why does ten[10] have 12 characters after the =? If I change ten[10] to ten[12], the following works.

#$data = "five[5]=12345&ten[10]=12345=678=90&six[6]=123456"; $data = "five[5]=12345&ten[12]=12345=678=90&six[6]=123456"; for (;;) { $data =~ s/(.+?)\[(\d+)\]=// or last; $hash{$1} = substr($data, 0, $2, ''); $data =~ s/^&// or last; } die("Bad input\n") if length $data; use Data::Dumper; print Dumper(\%hash);

This works too, but it's probably slower:

#$data = "five[5]=12345&ten[10]=12345=678=90&six[6]=123456"; $data = "five[5]=12345&ten[12]=12345=678=90&six[6]=123456"; for (;;) { $data =~ s/(.+?)\[(\d+)\]=((??{".{$2}"}))// or last; $hash{$1} = $3; $data =~ s/^&// or last; } die("Bad input\n") if length $data; use Data::Dumper; print Dumper(\%hash);

Replies are listed 'Best First'.
Re^2: splitting string based on length index
by phi95aly (Novice) on Nov 05, 2004 at 22:39 UTC
    may i say you are a genius, ikegami. this is the second time in days you have helped enormously. this works splendidly. and yes, you right, the 10 should be 10 characters.
      $h{$1} = substr $str, 0, $2, '' while $str =~ s/^&?([^\[]+)\[(\d+)\]=/ +/;

      cheers

      tachyon