CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [Tejas] How to write dispatch tables ? How to send arguements to the functions : Edit Log



Zhris
Enthusiast

Dec 17, 2014, 12:46 PM


Views: 30269
Re: [Tejas] How to write dispatch tables ? How to send arguements to the functions

Hi,

With regards to your first question, you are creating and returning subroutine references ( AKA anonymous subroutines, closures ). You can find useful information here. They are not run until you explicitly dereference / call them, at which point the arguments you supply are copied to @_. Try to follow the basic example below and you'll see the outer and inner subroutines work independently:


Code
my $innerref = outer( 'outerarg' ); 
$innerref->( 'innerarg' );

sub outer
{
my $arg = shift;

print "in outer - $arg\n";

return sub
{
my $arg = shift;

print "in inner - $arg\n";

return;
};
}


With regards to your second question, firstly I did suggest something similar over Skype but you haven't quite understood the purpose of my suggestion. The idea was that because you had alot of formulas across your code, many of which were very similiarly notated, I thought it would be better to group them in a hash using distinguishable keys and subroutine values, which you could call directly i.e. $formulas->{total_the_col}->( $col ). I didn't intend the hash to have col numbers as keys nor used quite in the manner you have in certain instances, although its not a problem if it works well for you.

I later suggested that you create an array of arrays that represents sections of your worksheet, which you can populate with information on how to fetch the value / which print method to use ( or even combination of both ). Roughly, here is the implementation I suggested, I'm sure there is probably a better way to do it, I will certainly get back to you if I think of one, I'll try to take another look when I have time:


Code
use strict;  
use warnings;

my $days_in_month = 10;
my @headings = ( 'A' .. 'Z' );

my $fetch =
{
default => sub { '--' },
headings => sub { my $heading = splice @headings, 0, 1; "$heading " }, # or $headings[$_[1]] etc.
custom1 => sub { 'c1' },
custom2 => sub { 'c2' },
};

my $print =
{
default => sub { print $_[0] },
custom1 => sub { print $_[0] },
custom2 => sub { print $_[0] },
};

my $grid = [ ];
prepop( $grid, [ 0 ], [ 0 .. 9 ], [ $fetch->{headings}, $print->{default} ] ); # 1st row, cols 1 to 10.
$grid->[1]->[0] = [ $fetch->{custom1}, $print->{custom1} ]; # 2nd row, 1st col.
$grid->[$days_in_month - 1]->[1] = [ $fetch->{custom2}, $print->{custom2} ]; # last row, 2nd col.
prepop( $grid, [ 1 .. 5 ], [ 4, 8 ], [ $fetch->{custom1}, $print->{custom2} ] ); # rows 2 to 6, cols 5 and 9.

for my $row ( 0 .. $days_in_month - 1 )
{
for my $col ( 0 .. 9 )
{
if ( my $subrefs = $grid->[$row]->[$col] )
{
# use custom fetch and print subrefs.
$subrefs->[1]->( $subrefs->[0]->( $row, $col ) );
}
else
{
# use default fetch and print subrefs.
$print->{default}->( $fetch->{default}->( $row, $col ) );
}

print '|';
}

print "\n";
}

sub prepop
{
my ( $list, $rows, $cols, $val ) = @_;

map { @$_[@$cols] = ( $val ) x @$cols } @$list[@$rows]; # complex one liner instead of two for loops.

return 1;
}


Regards,

Chris


(This post was edited by Zhris on Dec 17, 2014, 3:10 PM)


Edit Log:
Post edited by Zhris (Enthusiast) on Dec 17, 2014, 12:47 PM
Post edited by Zhris (Enthusiast) on Dec 17, 2014, 12:48 PM
Post edited by Zhris (Enthusiast) on Dec 17, 2014, 12:48 PM
Post edited by Zhris (Enthusiast) on Dec 17, 2014, 12:49 PM
Post edited by Zhris (Enthusiast) on Dec 17, 2014, 12:51 PM
Post edited by Zhris (Enthusiast) on Dec 17, 2014, 12:53 PM
Post edited by Zhris (Enthusiast) on Dec 17, 2014, 12:54 PM
Post edited by Zhris (Enthusiast) on Dec 17, 2014, 12:56 PM
Post edited by Zhris (Enthusiast) on Dec 17, 2014, 1:00 PM
Post edited by Zhris (Enthusiast) on Dec 17, 2014, 2:36 PM
Post edited by Zhris (Enthusiast) on Dec 17, 2014, 2:37 PM
Post edited by Zhris (Enthusiast) on Dec 17, 2014, 2:38 PM
Post edited by Zhris (Enthusiast) on Dec 17, 2014, 2:40 PM
Post edited by Zhris (Enthusiast) on Dec 17, 2014, 2:47 PM
Post edited by Zhris (Enthusiast) on Dec 17, 2014, 2:49 PM
Post edited by Zhris (Enthusiast) on Dec 17, 2014, 2:55 PM
Post edited by Zhris (Enthusiast) on Dec 17, 2014, 2:55 PM
Post edited by Zhris (Enthusiast) on Dec 17, 2014, 2:57 PM
Post edited by Zhris (Enthusiast) on Dec 17, 2014, 3:06 PM
Post edited by Zhris (Enthusiast) on Dec 17, 2014, 3:10 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives