in reply to Re: nested reg ex over multiple lines
in thread nested reg ex over multiple lines

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) }

Replies are listed 'Best First'.
Re^3: nested reg ex over multiple lines
by holli (Abbot) on Jun 20, 2005 at 13:52 UTC
    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/
Re^3: nested reg ex over multiple lines
by BUU (Prior) on Jun 20, 2005 at 17:33 UTC
    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.