in reply to converting an arbitrary string into a hash based on a pattern

Thanks holli and gopalr. Both of you gave me a fantastic (if not a perfect) start ;-). Turns out, there are many variations in my strings, and neither solution found all the key-value pairs correctly, however, gopalr's solution worked better on the data I threw at it. One particular miss was when there were a series of square brackets adjacent to each other, as in
FOO[this][that][other]
which had to resolve into
FOO => 'thisthatother'
and the other was, empty square brackets were not caught. In other words,
BAR []
should have become
BAR => ''
In any case, many thanks to the both of you for clearing my haze. I can take your suggestions and create one that will work for me.
--
when small people start casting long shadows, it is time to go to bed

Replies are listed 'Best First'.
Re^2: converting an arbitrary string into a hash based on a pattern
by holli (Abbot) on Mar 04, 2005 at 06:03 UTC
    use strict; use Data::Dumper; my %hash; $_="ABCD----[this] fgab [that] BFTE-- [other] AB CD EF---- [foo] +FOO[this][that][other] BAR []"; while ( /\G\s*([\w ]+)[\s-]*\[([\w\[\]]*)\]/g ) { $hash{$1}=$2; $hash{$1} =~ s/\]\[//g; } print Dumper (\%hash); #$VAR1 = { # 'BAR ' => '', # 'fgab ' => 'that', # 'ABCD' => 'this', # 'BFTE' => 'other', # 'FOO' => 'thisthatother', # 'AB CD EF' => 'foo' # };
    I just love regexes. Try to implement that in VB!


    holli, /regexed monk/