Hi!
Thanks a lot gus!!!!!!!!!
I've rewritten the whole thing yesterday to use a hash instead of an array.
Know I use this statement to get my hashref:
$ref_interfacelist = $dbh->selectall_hashref($sth, 'ID_INTERFACE');
Later I call the following:
[- interface_list ($ref_interfacelist) -]
And finally the new 'nacadminlib.epl':
------------------------------------------------------------------------------------------------------------------------
[! use nactools; !]
[$ sub interface_list $]
[- $ref_interfacelist = $_[0]; -]
[$ if (scalar(keys %{$ref_interfacelist}) > 0) $]
<tr class=heading>
<th><a>Mon</a></th>
<th><a>Device</a></th>
<th><a>Interface</a></th>
<th><a>Speed</a></th>
<th><a>Line</a></th>
</tr>
[$ foreach $key ( keys %{$ref_interfacelist} ) $]
[- $bgcolor = ( $bgcolor eq 'light' ) ? 'dark' : 'light'; -]
<tr class=[+ $bgcolor +]>
<td class=[+ $ref_interfacelist->{$key}{MONITOR_TYPE_SHORT} +]><b>[+
$ref_interfacelist->{$key}{MONITOR_TYPE_SHORT} +]</b></td>
<td><a href="view-device.epl?id_device=[+
$ref_interfacelist->{$key}{ID_DEVICE} +]" >[+
$ref_interfacelist->{$key}{DEVICENAME} +]</a></td>
<td><a href="view-interface.epl?id_interface=[+
$ref_interfacelist->{$key}{ID_INTERFACE} +]">[+
$ref_interfacelist->{$key}{INTERFACENAME} +]</a></td>
<td align=right><a>[+ prettyspeed ($ref_interfacelist->{$key}{SPEED})
+]</a></td>
<td><a href="view-line.epl?id_line=[+
$ref_interfacelist->{$key}{ID_LINE} +]">[+
$ref_interfacelist->{$key}{LINEDESCRIPTION} +]</a></td>
</tr>
[$ endforeach $]
[$ else $]
<tr><td colspan=5><a>no interfaces</a></td></tr>
[$ endif $]
[$ endsub $]
------------------------------------------------------------------------------------------------------------------------
Do you have any more suggestions gus??? Or maybe someone else???
THX a lot again, Alex!!!
[EMAIL PROTECTED] (Angus Lees) am 21.02.2003 03:07:09
An: [EMAIL PROTECTED] (Alexander Hartmaier)
Kopie: [EMAIL PROTECTED] (Blindkopie: Alexander
Hartmaier/DEBIS/EDVG/AT)
Thema: Re: UPD: output table with variable data
just some comments on perl/embperl style, none of which are
particularly critical:
At Thu, 20 Feb 2003 10:36:57 +0100, Alexander Hartmaier wrote:
> [- use nactools; -]
this would be better done as [! use nactools !], since that will only
be executed once. when you use a [- -] block, perl will realise and
not actually reload nactools every time, but you can avoid even the
check by using [! !]
in general, put "use" and perl function declarations inside [! !]
> [$ sub interface_list $]
> [- @interfacelist = @_; -]
this *copies* the entire array. since you don't modify @interfacelist
during this function (and so don't need a local copy), it is faster to
pass a reference to it instead (or perhaps just use @_ directly in the
following code. array ref is faster still, since perl won't have to
copy the array onto the argument stack in the first place).
ie: call function with interface_list(\@interface_list), and use
[- $ifref = $_[0] -] here. replace all $interfacelist[$n] with
$ifref->[$n] in the following code.
> [$ if ($#interfacelist >= 0) $]
in perl, an array in scalar context returns a count of its elements. a
boolean expression (in an "if" condition, for example) is a scalar
context.
the "perlish" way to write "if (this array has something in it)" is
simply: [$ if (@interfacelist) $]
(or [$ if (@$ifref) $] if using a reference)
> <tr class=heading>
> <th>Mon</th>
> <th>Device</th>
> <th>Interface</th>
> <th>Speed</th>
> <th>Line</th>
> </tr>
> [$ foreach $indx ( 0 .. $#interfacelist ) $]
in the following code, you don't need the actual *index*. what you
want is a foreach loop that will iterate through each *element* of
@interfacelist. in perl, this is usually written:
[$ foreach $interface (@interfacelist) $]
this avoids having to repeatedly lookup the array index on each use,
so is a little faster too.
(or [$ foreach $interface (@$ifref) $] if using a reference)
> <tr class=light>
> <td class=[+ $interfacelist[$indx][0] +]><a>[+
$interfacelist[$indx][1]
> +]</a></td>
with the above foreach loop change, these expressions then become:
[+ $interface->[0] +], [+ $interface->[1] +], etc
remembering that each element of an "array of arrays" is actually a
*reference* to the deeper array.
[...]
> </tr>
> [$ endforeach $]
> [$ else $]
> <tr><td colspan=5><a>no interfaces</a></td></tr>
> [$ endif $]
> [$ endsub $]
> I'm coding perl and embperl for a year now but I still don't know wheter I
> should use an array/hash or a reference.
> Can somebody explain the pros/cons of each?
passing a reference avoids copying the entire array/hash. in general,
its faster and more memory efficient to use a reference, except that
it also gets more fiddly later on. in particular, *returning* a
reference rather than the array/hash itself leads to code like this:
# get_interfaces() returns an array or () on error
foreach (get_interfaces()) { ... }
# vs. get_interfaces() returns an array ref or undef on error
foreach (@{get_interfaces() || []}) { ... }
in general, always use an array ref if passing large amounts of data
since the copy will hurt.
for small amounts of data, its often easier on the programmer to use a
simple array/hash directly - you'll have to balance that against the
run-time cost.
--
- Gus
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]