in reply to nested reg ex over multiple lines

Split your regex in two:
use strict; use warnings; use Data::Dumper; my $key; my %data; while (<DATA>) { $key = $1, next if /^CALCON\((\w+)\)/; $data{$key}->{$1} = $2 if /^\s+(\w+)\(([\w\d\s]+)\)/; } print Dumper (\%data); __DATA__ CALCON(test1) { TYPE(U8) FEATURE(DCOM) NAM(stmin) LABEL(Min seperation time between CFs) MIN(0) MAX(127) UNITS(ms) } CALCON(test2) { TYPE(U16) FEATURE(DCOM) NAM(dcomc_sestmr_timeout) LABEL(DCOM Session Timer Timeout) MIN(0) MAX(65535) UNITS(ms) }
Output:
$VAR1 = { 'test1' => { 'MIN' => '0', 'UNITS' => 'ms', 'FEATURE' => 'DCOM', 'NAM' => 'stmin', 'MAX' => '127', 'TYPE' => 'U8' }, 'test2' => { 'MIN' => '0', 'UNITS' => 'ms', 'FEATURE' => 'DCOM', 'NAM' => 'dcomc_sestmr_timeout', 'MAX' => '65535', 'TYPE' => 'U16' } };


holli, /regexed monk/

Replies are listed 'Best First'.
Re^2: nested reg ex over multiple lines
by tphyahoo (Vicar) on Jun 20, 2005 at 13:40 UTC
    I liked this solution, but since it felt a little "idiomatic" to me, I thought it might also be idiomatic to someone even newer to perl. So, I rewrote it in a way that was a bit easier for me to understand. Mainly I just put conditionals in parens, bracked off the results, and filled in the default variables where they were being assumed.
    use strict; use warnings; use Data::Dumper; my $content = ""; my ($key, %data); while (<DATA>) { if ( $_ =~ /^CALCON\((\w+)\)/ ) { $key = $1; } else { if ( $_ =~ /^\s+(\w+)\(([\w\d\s]+)\)/ ) { $data{$key}->{$1} = $2 ; } } } print Dumper(\%data); __DATA__ CALCON(test1) { TYPE(U8) FEATURE(DCOM) NAM(stmin) LABEL(Min seperation time between CFs) MIN(0) MAX(127) UNITS(ms) } CALCON(test2) { TYPE(U16) FEATURE(DCOM) NAM(dcomc_sestmr_timeout) LABEL(DCOM Session Timer Timeout) MIN(0) MAX(65535) UNITS(ms) }
      idiomatic? The only difference with your code is the use of if/else instead of the next statement. And next is really not very idiomatic. Other languages have a similar construct.


      holli, /regexed monk/
      Maybe it's just me and my love for default variables, but to me, all of your extraneous punctuation and explicit default variable usage makes it harder to read, at least to me. There's more code I have to read, and ignore, before I can understand exactly what is happening. But maybe it's just me.