$all="{this, {is, some, {deeply, nested}, text}, for, you}";
while ($all=~s/{([^{}]*?)}/f($1)/seg) {}
sub f {return \{split(",",$_[0])};}
print @{$all};
I expect $all to be a listref whose list contains:
{this, [reference to array], for, you}
Instead, @{$all} is empty.
I'm sure this is something basic, but what am I doing wrong?
Note: I intentionally "golfed" the code to post here (ie, minimal code that shows the problem). A more extensive version is at: https://github.com/barrycarter/bcapps/blob/master/playground.pl
EDIT: Thanks to everyone who answered! Notes:
The real f() has side effects, updates dbs, etc, so I really do have to call it. It doesn't just change a list into something else. My bad for not mentioning this.
I was exporting from Mathematica so "{a,b,c}" is a list, not a hash. Again, mea culpa for not mentioning this.
I know the "normal" way to do this is recursive: process each element, and if an element is a list, call f() on the list itself. I was trying to do "unfold" the recursion to avoid splitting nested "{". If you work inside out, you never have to count "{" when parsing.
An interesting other application would be a one-line XML parser (almost).
Giving geekosaur the checkmark for pointing out what was wrong and why my approach is probably wrong.
I think I'll try the Parser approach or even jrey's s/{/[ approach.