embedding variables in an array 
Author Message
 embedding variables in an array

Can someone provide a solution or point me to the perldoc which might
get me started in the right direction:

I'm building an array, some elements of which can't be calculated until
the entire array has been built. I would like to avoid a 2-pass solution
if possible. I should add that these elements occur throughout the
array, not at the end: they're percentages of the whole, thus need the
total values before they can be calculated.

I would like to push a "place holder" onto the array at the appropriate
points as I read data in and then assign the corresponding calculated
value to it as I shift items off. The various escape/quote sequences
I've tried have been unsuccessful.

I guess what I'm trying to do is shield the variable from evaluation
going into the array but allow it to be interpreted on the way out. Is
this possible?

Thanks for any guidance.



Tue, 02 Mar 2004 15:16:15 GMT  
 embedding variables in an array

Quote:

>Can someone provide a solution or point me to the perldoc which might
>get me started in the right direction:

>I'm building an array, some elements of which can't be calculated until
>the entire array has been built. I would like to avoid a 2-pass solution
>if possible. I should add that these elements occur throughout the
>array, not at the end: they're percentages of the whole, thus need the
>total values before they can be calculated.

The el Cheapo solution would be to insert code references for each
element that needs your described "lazy evaluation". Of course you
would then need a special accessor functions that would execute the
referenced code and update the array with the results.

The clean way would be to write your own class, probably based on the
code in Tie::Array. Check out "perldoc perltie" for more details.

HTH,



Tue, 02 Mar 2004 18:26:23 GMT  
 embedding variables in an array

Quote:
> Can someone provide a solution or point me to the perldoc which might
> get me started in the right direction:

> I'm building an array, some elements of which can't be calculated until
> the entire array has been built.

Even at this point, this looks like an awkward data structure.  If some
elements have a distinctly different status than others, they should
probably not go into the same array.

Quote:
>                                   I would like to avoid a 2-pass solution
> if possible.

Why?  Presumably, you are keeping all the data in memory because you
want random access.  Use it.

Quote:
>               I should add that these elements occur throughout the
> array, not at the end: they're percentages of the whole, thus need the
> total values before they can be calculated.

Again, why would you want to store percentages among the data they
are percentages of?  Even if you want to display (or otherwise use)
them in that sequence, it will be easier to build the corresponding
lists on the fly, but store them in different places.

Quote:
> I would like to push a "place holder" onto the array at the appropriate
> points as I read data in and then assign the corresponding calculated
> value to it as I shift items off. The various escape/quote sequences
> I've tried have been unsuccessful.

If the data structure *must* remain as it is, undef would be a good
placeholder.  Fill it in as needed, even if what's needed is a second
pass.

Quote:
> I guess what I'm trying to do is shield the variable from evaluation
> going into the array but allow it to be interpreted on the way out. Is
> this possible?

Yes, things like delayed execution are possible in Perl.  You'd use
a closure (perldoc perlsub for that), or (shudder) string eval.  But
I don't think you need that relatively sophisticated approach.

Use sum variable(s) to calculate the total(s) as you go along.  When
all the data are in, fill in the percentages, where-ever you decide
to store them.

Anno



Tue, 02 Mar 2004 18:44:03 GMT  
 embedding variables in an array

Quote:


>>I'm building an array, some elements of which can't be calculated until
>>the entire array has been built. I would like to avoid a 2-pass solution
>>if possible. I should add that these elements occur throughout the
>>array, not at the end: they're percentages of the whole, thus need the
>>total values before they can be calculated.
[...]
>The clean way would be to write your own class, probably based on the
>code in Tie::Array. Check out "perldoc perltie" for more details.

Actually, I had something like this in mind :-)

#!/usr/bin/perl -w

use strict;
use Tie::Array;

# create a subclass
package Tie::ComputingArray;


# overload all methods that access data
# code refs will be executed; all other data
# will be passed back verbatim

sub FETCH {

  if( ref( my $frob = $self->[$index] ) eq "CODE" ){
    # also store computed data for later access
    return $self->[$index] = $frob->();
  } else {
    return $frob;
  }

Quote:
}

sub POP {


    return $frob->();
  } else {
    return $frob;
  }

Quote:
}

sub SHIFT {


    return $frob->();
  } else {
    return $frob;
  }

Quote:
}

sub SPLICE {
 my $self = shift;

 return map {
          ref( $_ ) eq "CODE" ? $_->() : $_

Quote:
}

package main;


# bind an array to our class

# load data into array - code refs will be
# evaluated on access

# show data

__END__

HTH,
--

split//,unpack('u*','L#`T&)QD5#0`#!!`#%1D)#08`#P05!!(3``$$"``#"0L&``('.
'"`P<!`````0$`'));$t=~s/(\d)(.)/$2x$1/eg;map{$t.=substr$t,$i,1,''while



Tue, 02 Mar 2004 19:39:55 GMT  
 embedding variables in an array

Quote:


>>Can someone provide a solution or point me to the perldoc which might
>>get me started in the right direction:

>>I'm building an array, some elements of which can't be calculated until
>>the entire array has been built.

> Even at this point, this looks like an awkward data structure.  If some
> elements have a distinctly different status than others, they should
> probably not go into the same array.

I'm tagging formatted data for a composition system. The end result is
financial statements. Multiple records, multiple record types per
account; not necessarily in the correct sequence. I'm not building a
database, just parsing the records, extracting and tagging the fields
that are to be printed. I'm using the arrays as stacks to push the
tagged data on. Multiple arrays per statement, one for each security
type (bonds, equities, cash equivalents...).  Shift the tagged data off
at the account breaks.

Quote:

>>                                  I would like to avoid a 2-pass solution
>>if possible.

> Why?  Presumably, you are keeping all the data in memory because you
> want random access.  Use it.

But I really don't want random access to it. I don't want the overhead.
Just buckets to hold the various information until it's written to
output. Input is sent to the various buckets as appropriate, the buckets
are dumped in sequence. I don't need to pay attention to what any given
array element is once it's been pushed on the array.

Quote:

>>              I should add that these elements occur throughout the
>>array, not at the end: they're percentages of the whole, thus need the
>>total values before they can be calculated.

> Again, why would you want to store percentages among the data they
> are percentages of?  Even if you want to display (or otherwise use)
> them in that sequence, it will be easier to build the corresponding
> lists on the fly, but store them in different places.

The statment required calculation of the dollar values of the security
types and percentages of the total portfolio (e.g. "some_security_class
: 33%", followed by a detailed listing of each security in that class).
  Hense, the percentages had to be available prior to the calculation of
subtotals and totals. I ended up with a second array (stack) that I
pushed the subtotals on and shifted off prior to dumping (err, shifting)
the related "detail" arrays.

Quote:

>>I would like to push a "place holder" onto the array at the appropriate
>>points as I read data in and then assign the corresponding calculated
>>value to it as I shift items off. The various escape/quote sequences
>>I've tried have been unsuccessful.

> If the data structure *must* remain as it is, undef would be a good
> placeholder.  Fill it in as needed, even if what's needed is a second
> pass.

>>I guess what I'm trying to do is shield the variable from evaluation
>>going into the array but allow it to be interpreted on the way out. Is
>>this possible?

> Yes, things like delayed execution are possible in Perl.  You'd use
> a closure (perldoc perlsub for that), or (shudder) string eval.  But
> I don't think you need that relatively sophisticated approach.

> Use sum variable(s) to calculate the total(s) as you go along.  When
> all the data are in, fill in the percentages, where-ever you decide
> to store them.

> Anno

I was really hoping for a quick & dirty (err, easy) way to get the
percentages into the various buckets. It looks like it would be more
trouble that it's worth (cpu cycles-wise). Thanks for the suggestions.

/whatever



Tue, 02 Mar 2004 20:13:55 GMT  
 embedding variables in an array

Quote:



>>>I'm building an array, some elements of which can't be calculated until
>>>the entire array has been built. I would like to avoid a 2-pass solution
>>>if possible. I should add that these elements occur throughout the
>>>array, not at the end: they're percentages of the whole, thus need the
>>>total values before they can be calculated.

> [...]

>>The clean way would be to write your own class, probably based on the
>>code in Tie::Array. Check out "perldoc perltie" for more details.

> Actually, I had something like this in mind :-)

<snip/>

Ouch... Thanks for the code, but overkill for this application. I'm
sorry to have put you through that effort. In the future I'll be more
careful in describing what I'm trying to achieve. My response to Anno
contains some additional details. Basically, I'm using the arrays as
stacks for temporary storage of print items.

Thanks, and sorry again.

/whatever



Tue, 02 Mar 2004 20:42:48 GMT  
 embedding variables in an array

[big snip]

Quote:
> > Yes, things like delayed execution are possible in Perl.  You'd use
> > a closure (perldoc perlsub for that), or (shudder) string eval.  But
> > I don't think you need that relatively sophisticated approach.

> > Use sum variable(s) to calculate the total(s) as you go along.  When
> > all the data are in, fill in the percentages, where-ever you decide
> > to store them.

> > Anno

> I was really hoping for a quick & dirty (err, easy) way to get the
> percentages into the various buckets. It looks like it would be more
> trouble that it's worth (cpu cycles-wise). Thanks for the suggestions.

I don't think you would save cpu cycles that way.  As far as I can see,
you want a spreadsheet-like action on an array, where some fields are
defined in terms of the content of others.  In another subthread
Thomas B?tzler suggested a tied array that may go in that direction.
You could also use an object that duplicates as an arrayref for a similar
purpose.

None of these are quick-and-dirty (only dirty :).  There may be a module
out there that has the functionality you want, but then the dirty part
is finding it.

Anno



Tue, 02 Mar 2004 20:54:46 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Help with Embedding and Arrays of Arrays?

2. Help with Embedding and Arrays of Arrays?

3. Destroying a variable in an embedded perl interpreter

4. Reading text with embedded variables names from a file

5. embedding perl & magic for variables

6. can you embed variables inside $ENV{ } evaluations?

7. embedded variables in a file

8. Novice question: Interpolating embedded variables from a template file

9. Embedded variable names in text files....possible?

10. Shared variables between perl and embedded sh scripts ???

11. Variable length array of arrays into tree structure - help

12. Sorting @Array from variable in middle of array

 

 
Powered by phpBB® Forum Software