At 4:13 AM -0600 1/11/12, Chris Stinemetz wrote:
I have a script where I have captured the value on the left side of
the "=" as the header for my table. Now I want to take the value on
the right side of the "=" sign and populate a new row in table format
where the header value I stored in the @header array matches the value
on the left side of the "=" sign. Each dataset begins with a new line.
If there is no match I would like to return null or just "" as the
value.

Your data looks like a good candidate for an array of hashes. Each element of the array will contain a reference to an anonymous hash, which will contain the data for one dataset. The keys for the hash will be the strings to the left of the equal sign, and the values will be the string to the right.

Things to watch out for:

Datasets that do not have all of the data, or have misspelled keys, or duplicate keys, or unexpected keys. You will still be able to store the data, but you may have trouble printing it out. Duplicate keys means you may over-write data unless you test for it and handle it correctly.


Below is the script thus far.

Any advice is greatly appreciated,

Chris

currently my output is just the header. I haven't been able to figure
out how to do the rest.

**OUTPUT**
csno,rfpi,vrp0,vrp1,trl,repl,repl_d1,rrl,row[1],rfpc0,rfpc1,row[4],line,row[15],tti


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

my %seen;
my @header;
my $delim = ",";

Declare an array to hold the data, and a reference to a hash to hold one dataset, and initialize both (all code untested):

my @data;
my $setref = {};
push(\@data,$setref);

Declare a hash to count occurrences of the headers:

my %headers;

while( <DATA> ) {
  chomp;

  if($_ =~ /(.*)\=(.*)?/) {

Since you are using the $_ variable, you can use it implicitly here. You don't need to escape the equal sign. The ? character is superfluous:

  if( /(.*)=(.*)/ ) {

    $seen{$1}++;


Your data value is now in $2. You should save it in the hash for this dataset:
  $setref->{$1} = $2;

Count the header:
  $headers{$1}++;


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

If a blank line, start a new dataset:

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

}

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


There is no need to put $delim in quotes. You can get the headers from the keys of the hash %headers (but not in the same order as read from the file).

print join( $delim, sort( keys %headers ) ), "\n";

You can also print the counts:

print "Field counts:\n\n";
for my $key ( sort keys %headers ) {
  printf("  %6s  %3d\n", $key, $headers{$key} );
}



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


--
Jim Gibson
j...@gibson.org

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