[EMAIL PROTECTED] wrote:
> 
> Hi all,

Hello,

>      I am trying to setup a hash who's values are referance to hash's. Data
> structure should look like this
> 
> hash
>      1:
>            setting 1
>            setting 2
>      2:
>            setting 1
>            setting 2
> ....
> 
>   I would think it can be accomplished with following code, but when I try
> to print it out it only prints the last setting which it read. Any idea's ??
> 
> CODE:
> #!perl -w
> use strict;
> open RD, "input.txt";
> 
> my ( $field,$portsetting, $value, $port, $portnum, %digi, $debug );
> $debug=0;
> 
>  while ( <RD> ) {
>      if(m/^(port)\s\=\s(.*)$/i){$portnum=$2;$portnum++}
> 
>      $field = {};
>      $digi{$portnum} = $field;
> 
>                 m/^(.*)\s\=\s(.*)$/;
>                 $portsetting=$1;
>                 $value=$2;
>                 $field->{$portsetting}=$value;
> 
> print "setting $portsetting -> $digi{$portnum}{$portsetting}\n" if $debug;
> 
>                 if($portsetting eq 'porttitle'){
>                         $value =~ tr /-/ /;
>                         $value =~ m/^(\w+)\s+(\w+)/i;
>                         $field->{$portsetting}=$1;
>                         $field->{'type'}=$2;
> print "1:$digi{$portnum}{$portsetting}\t2:$digi{$portnum}{'type'}\n" if $debug;
>                 }
> 
>  }
> 
> # print the whole thing
>  foreach $port ( keys %digi ) {
>      print "$port: { ";
>      for $field ( keys %{ $digi{$port} } ) {
>          print "$field=$digi{$port}{$field} ";
>      }
>      print "}\n";
>  }
> 
> Sample DATA
> 
> port = 0
> bmanset = 0
> benable = 1
> uarttype = 0
> baudrate = 9600
> stopbits = 1
> databits = 8
> parity = 0
> flowcontrol = 0
> protocol = 1
> port = 1
> bmanset = 0
> benable = 1
> uarttype = 0
> baudrate = 9600
> stopbits = 1
> databits = 8
> parity = 0
> flowcontrol = 0
> protocol = 1


Perhaps this will do what you want:

#!perl -w
use strict;
use Data::Dumper;

open RD, 'input.txt' or die "Cannot open 'input.txt' $!";

my ( %data, $temp );
while ( <DATA> ) {
    chomp;
    my ( $name, $value ) = split /\s*=\s*/ or next;
    if ( exists $temp->{ $name } or eof DATA ) {
        $data{ $temp->{ port } } = $temp;
        $temp = { $name, $value };
        next;
        }
    $temp->{ $name } = $value;
    }

print Dumper \%data;

__END__



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to