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);
In reply to Re: splitting string based on length index
by ikegami
in thread splitting string based on length index
by phi95aly
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |