Home > Archive > PERL Beginners > May 2007 > Simplification of the code
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Simplification of the code
|
|
| Alok Nath 2007-05-22, 6:59 pm |
| Hi,
Can anybody help me to simplify the for loops here ?
It parses the excel file.
=09
foreach my $col (1..10){
push @row_1, $Sheet->Cells(1, $col)->{'Value'} ;=09
}
my @node_names =3D th(\@row_1);
foreach my $col (1..10){
push @row_2, $Sheet->Cells(2, $col)->{'Value'} ;=09
}
my @workstations =3D td(\@row_2);
foreach my $col (1..10){
push @row_3, $Sheet->Cells(3, $col)->{'Value'} ;=09
}
my @cards =3D td(\@row_3);
I am using the above for loops to create a table -finally.
print table({-border=3D>undef,-width=3D>'100%'},
caption(b('My table')),
Tr(\@node_names),
Tr(\@workstations),
Tr(\@cards),
}
Thanks
Alok.
| |
| Yitzle 2007-05-22, 6:59 pm |
| > foreach my $col (1..10){
> push @row_1, $Sheet->Cells(1, $col)->{'Value'} ;
> }
becomes
push @row_1, $Sheet->Cells(1, $_)->{'Value'} for (1..10);
You can likely join all 3 into one loop, but I can't think how off the
top of my head.
| |
| Jenda Krynicky 2007-05-22, 6:59 pm |
| Subject: Simplification of the code
Date sent: Tue, 22 May 2007 19:04:01 +0530
From: "Nath, Alok (STSD)" <alok.nath@hp.com>
To: <beginners@perl.org>
> Hi,
> Can anybody help me to simplify the for loops here ?
> It parses the excel file.
>
>
> foreach my $col (1..10){
> push @row_1, $Sheet->Cells(1, $col)->{'Value'} ;
> }
> my @node_names = th(\@row_1);
Why the @row_1 variable?
my @node_names;
foreach my $col (1..10){
push @node_names, $Sheet->Cells(1, $col)->{'Value'} ;
}
@node_names = th(\node_names);
or even better
my @node_names = th [map { $Sheet->Cells(1, $_)->{'Value'} }
(1..10)];
> foreach my $col (1..10){
> push @row_2, $Sheet->Cells(2, $col)->{'Value'} ;
> }
> my @workstations = td(\@row_2);
>
>
> foreach my $col (1..10){
> push @row_3, $Sheet->Cells(3, $col)->{'Value'} ;
> }
> my @cards = td(\@row_3);
The different functions make it a bit harder, but you could merge
those loops into this:
my @rows = map {
my $row = $_;
[ map { $Sheet->Cells( $row, $_)->{'Value'} } (1..10) ]
} (1..3);
to get the data and then
> I am using the above for loops to create a table -finally.
>
>
> print table({-border=>undef,-width=>'100%'},
> caption(b('My table')),
> Tr(\@node_names),
> Tr(\@workstations),
> Tr(\@cards),
> }
print table({-border=>undef,-width=>'100%'},
caption(b('My table')),
Tr([ th $rows[0]]),
Tr([ td $rows[1]]),
Tr([ td $rows[2]]),
}
HTH, Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
| |
| Rob Dixon 2007-05-22, 6:59 pm |
| Nath, Alok (STSD) wrote:
>
> Can anybody help me to simplify the for loops here ?
> It parses the excel file.
>
>
> foreach my $col (1..10){
> push @row_1, $Sheet->Cells(1, $col)->{'Value'} ;
> }
> my @node_names = th(\@row_1);
>
> foreach my $col (1..10){
> push @row_2, $Sheet->Cells(2, $col)->{'Value'} ;
> }
> my @workstations = td(\@row_2);
>
>
> foreach my $col (1..10){
> push @row_3, $Sheet->Cells(3, $col)->{'Value'} ;
> }
> my @cards = td(\@row_3);
>
>
> I am using the above for loops to create a table -finally.
>
>
> print table({-border=>undef,-width=>'100%'},
> caption(b('My table')),
> Tr(\@node_names),
> Tr(\@workstations),
> Tr(\@cards),
> }
Is everybody trying for an obfuscation award here?
In your code, Alok, the output from th() and td() is a single piece of HTML
text and so doesn't need storing in an array. Instead put the Excel table
values into your arrays and build all the HTML at once. Like this.
HTH,
Rob
my (@node_names, @workstations, @cards);
foreach my $col (1 .. 10) {
push @node_names, $Sheet->Cells(1, $col)->{'Value'};
push @workstations, $Sheet->Cells(2, $col)->{'Value'} ;
push @cards, $Sheet->Cells(3, $col)->{'Value'} ;
}
print table({-border=>undef,-width=>'100%'},
caption(b('My table')),
Tr(th(\@node_names)),
Tr(td(\@workstations)),
Tr(td(\@cards)),
);
|
|
|
|
|