views:

91

answers:

2

Hi,

I have some files with below data.

sample File 1:

sitename1,2009-07-19,"A1",11975,17.23
sitename1,2009-07-19,"A2",11,0.02
sitename1,2009-07-20,"A1",2000,17.23
sitename1,2009-07-20,"A2",538,0.02

I want to map the values in column 4 with column 2 and 3 as shown below.

Output required.

Site,Type,2009-07-19,2009-07-20
sitename1,"A1",11975,2000
sitename1,"A2",11,538

Here is what I have tried so far:

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

my $column_header=["Site,Type"];
my $position={};
my $last_position=0;
my $current_event=[];

my $events=[];

while (<STDIN>) {
    my ($site,$date,$type,$value,$percent) = split /[,\n]/, $_;
    my $event_key = $date;

    if (not defined $position->{$event_key}) {
        $last_position+=1;
        $position->{$event_key}=$last_position;
        push @$column_header,$event_key;
    }
    my $pos = $position->{$event_key};
    if (defined $current_event->[$pos]) {
        dumpEvent();
    }
    if (not defined $current_event->[0]) {
        $current_event->[0]="$site,$type";
    }
    $current_event->[$pos]=$value;
}

dumpEvent();

 my $order = [];
 for (my $scan=0; $scan<scalar(@$column_header); $scan++) { 
    push @$order,$scan; 
}

printLine($column_header);
map { printLine($_) } @$events;

sub printLine {
    my $record=shift;

    my @result=();
    foreach my $offset (@$order) {
        if (defined $record->[$offset]) {
            push @result,$record->[$offset];
        } else {
            push @result,"";
        }
    }
    print join(",",@result)."\n";
}

sub dumpEvent {
    return unless defined $current_event->[0];
    push @$events,$current_event;
    $current_event=[];
}

The output i am getting is as below.

*Site,Type,2009-07-19,2009-07-20*
sitename1,"A1",11975,
sitename1,"A2",11,
sitename1,"A1",,14620
sitename1,"A2",,538
+1  A: 

If I understand you correctly (and I have to admit I'm only guessing), you have several types of things at different dates and a value for each. Thus you need a data structure like this hash for each site:

$foo = {
    site  => 'sitename1',
    type  => 'A1',
    dates => [
                {
                    date  => '2009-07-19',
                    value => 11975,
                },
                {
                    date  => '2009-07-20',
                    value => 538,
                },
             ],
};

Is that even close?

innaM
A: 

The folowing code produces the expected result and makes "some" sense. I don't know if it makes real sense.

my %dates;
my %SiteType;
while (<DATA>) {
    chomp;
    my ($site,$date,$type,$value,$percent) = split /,/;
    $dates{$date} = '1';
    push @{$SiteType{"$site,$type"}}, $value ;
};
print 'Site,Type,', join(',', sort keys %dates), "\n";
foreach ( sort keys %SiteType) {
    print $_, ',', join(',', @{$SiteType{$_}}), "\n";
};
larelogio
Thanks larelogio, its very helpful for me.
Space