in reply to More Regular Expressions (text data handling)
For brevity and clarity in the question: Using the __DATA__ label will enable folks to run this easily.
Update: Agh! the gotcha here is there are some lines with 2 key value pairs on em :(
#!/usr/bin/perl -w use strict; use Data::Dumper; for(grep {/\w/}<DATA>){ # g repeats the regex, e executes the Perl in the substitution, # returns the number of matches into the condition. # $1 and $2 are the bracketed matches in the regex in order. + unless( s/^(^[a-z ]+) *: *([\w\d]+)/$_{$1}=$2/ige ){ chomp; if (/:/){ # key part $_{$_}=join(',',@_); # make new key item @_=() } else { # list part push @_,$_ } } } print Dumper(\%_); __DATA__ Graq: Agnostic: Number: 634321 age: 27 hair colour: black height: 73 weight: 123 legs: 2 arms: 2 jameson bells guinness favourite: detests: likes:
--
Brother Frankus.
¤
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re(2): More Regular Expressions (text data handling)
by graq (Curate) on Dec 04, 2001 at 18:12 UTC |