I need to arrange output of SQL query into a comma separated format and I'm struggling with processing the output...
The output is something like this:
<Attribute1 name><x amount of white spaces><Atribute value>
<Attribute2 name><x amount of white spaces><Atribute value>
<Attribute3 name><x amount of white spaces><Atribute value>
<Attribute4 name><x amount of white spaces><Atribute value>
<Attribute5 name><x amount of white spaces><Atribute value>
- There are always 5 Attribute/Value pairs that makes one record.
- The Attribute names are not always in same order.
So it could be(example where I would have only two records 5 lines(A,B,C,D,E) each):
A 1
B 2
C 3
D 4
E 5
B 4
C 3
A 5
E 1
D 2
I would need to parse this output into the following format:
A,B,C,D,E
A,B,C,D,E
....
but using only the value, so the above 2 records would be printed as:
1,2,3,4,5
5,4,3,2,1
What I've tried so far is as below:
sub query {
open (OUTPUT, "su - oracle -cf 'source .profile; sqlplus $user/$passwd \@$sql_file'|") || die "Can open SQL\n";
while (<OUTPUT>) {
chop;
($rubbish,$value) = split(/\s+/);
if (/^A/){
$list[0] = $value;
} elsif (/B/){
$list[1] = $value;
} elsif ( /C/ ){
$list[2] = "$value";
} elsif ( /D/ ){
$list[3] = "$value";
} elsif ( /E/ ){
$list[4] = "$value";
} else { next;}
push ( @array, \@list );
}
close(OUTPUT);
}
sub printSubs {
$" = ",";
for $a (@array){
print "@$a\n";
}
}
But that does not work.. it just prints out the last record many times.. :wall:
So would someone have any ideas how to fill the array?