There has to be a better way, but cannot figure it out (see below)
 
The problem lies with the regex:
  m/^\s*(\w+)\s*=\s*(.+)$/
 
which is trying to match lines in a /etc/security/user file in the format:
 
item = data
 
The /^\s*(\w+)\s*=\s* part works fine being that "item" if well defined as being a single word.
 
It is the (.+)$ bit that is bothering me, and because I could not come up with an adequate
solution I had to add the following two lines to get the thing to work.
      $item = $1; $data = "" $data =~ s/\s*$//;
      $user{$stanza}{$item} = $data unless $data eq "";
Instead of just being able to do:
      $user{$stanza}{$1} = $2;
The problem is:  "data" can consist of and needs to match or notmatch any of the following:
 
do not match these
1)   a null  as in nothing whatsoever following the "=" ignore this one don't want it
2)  any amount of white space following the "=" again ignore this one don;t want it
 
match these but trim any trailing space
1)  a quoted string "something like this"
2)  a single word   
3) either of the above but with trailing white space that would need to be trimed before
    being asigned to the hash.      such as /"something like this"   /
 
My current solution works.... but those two additional lines seem ugly, is there a way I can do it all from within the regex?
 
< --- code ---- >
while ( <USER> ) {
   next if (/^\s*(\*|$)/);
   if(  m/^(\S.*):/ ){
      $stanza = $1;
      next;
   }
   if (m/^\s*(\w+)\s*=\s*(.+)$/) {
      $item = $1; $data = "" $data =~ s/\s*$//;
      $user{$stanza}{$item} = $data unless $data eq "";
   }
}
foreach $stanza (sort keys %user) {
   foreach $item (sort keys %{$user{$stanza}}) {
   print "$stanza: $item = [${%{$user{$stanza}}}{$item}]\n";
   }
}
< --- code ---- >
 
--
db


_______________________
Dave Blakemore
[EMAIL PROTECTED]
_______________________________________________
Perl-Unix-Users mailing list
Perl-Unix-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to