Mark Goland wrote:
John,
    this code didn't work for me. Although I did have to change <DATA> to
<RD>;

Probably because he use the sample data in the DATA filehandle.


His code works correctly near as I can tell; so -
My question is - is your data actually in this format?

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


Using John's code:

#!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__
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


I get:


$VAR1 = {
          '1' => {
                   'baudrate' => '9600',
                   'flowcontrol' => '0',
                   'uarttype' => '0',
                   'databits' => '8',
                   'stopbits' => '1',
                   'port' => '1',
                   'benable' => '1',
                   'parity' => '0',
                   'bmanset' => '0'
                 },
          '0' => {
                   'protocol' => '1',
                   'baudrate' => '9600',
                   'flowcontrol' => '0',
                   'uarttype' => '0',
                   'databits' => '8',
                   'stopbits' => '1',
                   'port' => '0',
                   'benable' => '1',
                   'parity' => '0',
                   'bmanset' => '0'
                 }
        };


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