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 | |
by tachyon (Chancellor) on Nov 05, 2004 at 23:35 UTC |