Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

If you want to go a step farther than eval, you can use some tie magic to "shadow" the original $Sel1Ttl variables with a real array.

You have to be careful about how you index into this array -- $Sel1Ttl counts up from one, but (modern) Perl arrays count up from zero. In this code, I put a dummy element at index zero so that $a1z = $az[1].

#!/usr/bin/perl -w use strict; { package FixedArrayOfScalarRefs; use Carp qw( croak ); sub TIEARRAY { my ( $class, @refs ) = @_; my $self = [@refs]; bless $self, $class; return $self; } sub FETCH { my ( $self, $index ) = @_; if ( not ref $self->[$index] ) { croak "Array index $index out-of-bounds"; } my $value = ${ $self->[$index] }; return $value; } sub STORE { my ( $self, $index, $value ) = @_; if ( not ref $self->[$index] ) { croak "Array index $index out-of-bounds"; } ${ $self->[$index] } = $value; } sub FETCHSIZE { my ($self) = @_; return scalar @$self; } }
my ( $a1z, $a2z, $a3z ) = qw( 42 13 666 ); my @az; tie @az, 'FixedArrayOfScalarRefs', "There is no element zero!", # Dummy element 0 \( $a1z, $a2z, $a3z ); # Elements 1 .. 3 for ( 1 .. 3 ) { print "\$a${_}z = $az[$_]\n"; }

Since the dummy element zero is not a reference, the tied array will croak if you accidentally use it:

print "\$a0z = $az[0]\n"; # Array index 0 out-of-bounds at bar.pl line 63

With the array, you can use the traditional for (1 .. 3) { $sum += $az[$_] } or the IMHO prettier:

use List::Util qw( sum ); my $sum = sum( @az[ 1 .. 3 ] ); print "sum = $sum\n";

If you'd prefer $Sel1Ttl = $SelTtl[0], leave out the dummy element in the tie. If you do, the sum becomes:

my $sum = sum( @az );

In reply to Re: Adding scalars but with a twist.. by TilRMan
in thread Adding scalars but with a twist.. by snappybo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2022-05-29 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (101 votes). Check out past polls.

    Notices?