tags:

views:

100

answers:

1
Field 1 :  data 1
Field 2 :  data 2
Field 3 :  data 3
Field 4 : 
<start>
...

..

<end>
field 5  : data 5 

In this format file content is there. I was trying to extract information and store it into hash .

It works for single line but its not working for multi-line

Please help on this

+1  A: 

Here my shot at the following:

  1. Simple data is field name : field value
  2. Complex data or long-form data appears between <start> and <end> tags.

    use 5.010;
    use strict;
    use warnings;
    
    
    my $state = 0;
    my $name;
    my $data;
    my %hash;
    
    
    while ( my $line = <> ) {
        given ( $state ) { 
            when ( 0 ) { 
                ( $name, $data ) = split /\s*:\s*/, $line, 2;
                if ( $data =~ /\S/ ) { 
                    $data =~ s/\s*$//;
                    $hash{ $name } = $data;
                }
                else { 
                    $state++;
                }
            }
            when ( 1 ) {
                $state++ if $line =~ m/^<start>/;
            }
            when ( 2 ) { 
                if ( $line =~ m/^<end>/ ) { 
                    $hash{ $name } = $data;
                    $state = 0;
                }
                else { 
                    $data .= $line;
                }
            }
        }
    }
    
Axeman
This is a closed question so I couldn't respond with my own answer, but you don't need any of the state handling. When you find the multiline marker, just get the rest of the data: http://gist.github.com/621167
brian d foy