>
> Define an array with the headers you expect:
>
> my @headers = ( '>csno', 'rfpi', 'vrp0', ... );
>
> Use that array for your keys instead of the @header array.
>
> To avoid warnings of uninitialized values for missing values, print the
> expression (defined $data{$key} ? $data{$key} : '' ) instead of $data{$key}
> or put "no warnings 'unintialized';" inside the print loop.
>

Thanks again Jim,

I am having a bit of trouble trying to implement your suggestions.

I am getting the following error:

Global symbol "%data" requires explicit package name at
C:\Users\cstinemetz\Documents\Perl\dev\headers.pl line 38.
Global symbol "%data" requires explicit package name at
C:\Users\cstinemetz\Documents\Perl\dev\headers.pl line 38.

I believe it is because (defined $data{$key} ? $data{$key} : '' ) is
for hash notation and not array.

I also kept the @header array because it contains all the elements
that I am expecting to be in the header.

Thanks again for your help.

It is greatly appreciated,

Chris

#!/usr/bin/perl
use warnings;
use strict;

use Data::Dumper;

my %seen;
my @header;
my @data;
my $setref = {};
push(@data,$setref);
my %headers;
my $delim = ",";

while(<DATA>) {
  chomp;

  if(/(.*)=(.*)/) {
    $seen{$1}++;
    $setref->{$1} = $2;
    $headers{$1}++;

   if ($seen{$1} == 1) {
      push(@header,$1);
    }
  }

  elsif(/^\s*$/) {
    $setref = {};
    push(@data,$setref);
  }
}

# header
print join($delim,@header),"\n";

for my $key (@header) {
  (defined $data{$key} ? $data{$key} : '' )
  print join($delim,@data),"\n"; }

__DATA__
>csno=1
rfpi=1
vrp0=3423000
vrp1=3423000
trl=1700000
repl=100
repl_d1=100
rrl=20
row[1]=yes
rfpc0=0
rfpc1=0

>csno=1
rfpi=2
vrp0=3423000
vrp1=3423000
trl=1700000
repl=100
repl_d1=100
row[4]=no
rrl=20
rfpc0=0
rfpc1=0

>csno=1
rfpi=3
vrp0=3423000
vrp1=3423000
trl=1700000
repl=100
repl_d1=100
rrl=20
rfpc0=0
rfpc1=0

>csno=2
rfpi=1
vrp0=3423000
vrp1=3423000
trl=1700000
repl=100
repl_d1=100
rrl=20
line=yes
rfpc0=0
rfpc1=0

>csno=2
rfpi=2
vrp0=3423000
vrp1=3423000
trl=1700000
repl=100
repl_d1=100
rrl=20
rfpc0=0
line=value
row[15]=
tti=1
rfpc1=0

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to