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: [Laurent_R] How to write dispatch tables ? How to send arguements to the functions : Edit Log



Tejas
User

Dec 17, 2014, 10:32 AM


Views: 29354
Re: [Laurent_R] How to write dispatch tables ? How to send arguements to the functions

Thanks Laurent

This snippet would really be helpful for me in my future assignments

Code
my %dispatch_table;  
for (a..z) {
$dispatch_table{$_} = create_func($_);
}
sub create_func {
my $letter = shift;
my $file = "letter_$letter.txt";
open my $FH, ">", $file or die "can't open $file $file $!";
return sub { my $output = shift; print $FH $output;}
}


I see that we are sending a single arguement and that is the letter
But where is thsi getting input from

Code
return sub { my $output = shift; print $FH $output;}

shift actaully gets the first variable from input..which we already have saved in $letter,
what does $output contain


Quote
I have one more issue or rather doubt regadring the implementation, this actually is chris's idea that i implemented
I have a hash of formulaes, that are related to excel sheet and i print these in cells.


Code
my $ptech_formulas = 
{

0 => sub { my $row = shift; $row = $row+1 ;qq/=IF(B${row}="", "", TEXT(WEEKDAY(B${row}), "ddd"))/ },
1 => sub { my $row = shift; qq/=IF(B${row}="", "", IF(B${row}+1>EOMONTH(B${row}, 0), "", B${row}+1))/},
2=> sub { my $row = shift;$row = $row+1 ;qq/=IFERROR(SUMIFS(SLA!\$J:\$J,SLA!\$N:\$N,\$C\$4,SLA!\$E:\$E,65100,SLA!\$M:\$M,Summary!B${row}),0)+IFERROR(SUMIFS(SLA!\$J:\$J,SLA!\$O:\$O,\$C\$4,SLA!\$E:\$E,65100,SLA!\$M:\$M,Summary!B${row}),0)/},
3 => sub { my $row = shift; $row = $row+1 ;qq/=IFERROR(SUMIFS(SLA!\$J:\$J,SLA!\$N:\$N,\$C\$4,SLA!\$E:\$E,65910,SLA!\$M:\$M,Summary!B${row}),0)+IFERROR(SUMIFS(SLA!\$J:\$J,SLA!\$O:\$O,\$C\$4,SLA!\$E:\$E,65910,SLA!\$M:\$M,Summary!B${row}),0)/} ,
4 => sub { my $row = shift; $row = $row ;qq/=B${row}/},
5 => sub { my $row = shift; $row = $row+1 ;qq/= -SUMIFS('PTI FIN25'!L:L, 'PTI FIN25'!F:F,E${row}, 'PTI FIN25'!E:E, \$C\$5)-SUMIFS('PTI FIN25'!M:M, 'PTI FIN25'!F:F,E${row}, 'PTI FIN25'!E:E, \$C\$5)/},
6 => sub { my $row = shift; $row = $row+1 ;qq/=-SUMIFS('PTI FIN25'!K:K, 'PTI FIN25'!F:F,E${row},'PTI FIN25'!E:E, \$C\$5)/ },
7 => sub { my $row = shift; $row = $row+1 ;qq/=IFERROR((D${row}+C${row})-SUM(F${row}:G${row}),0)/},
8 => sub { my $row = shift; $row = $row+1; qq/=IF(C${row}=0,"Current Month in transit","")/ } ,
default => { sub{ qq/=EOMONTH(B2,-1)+1/ },
default1 => sub{my $row = shift; $row = $row+2 ; qq/=E${row}-1/ },
};



Quote
And i call them like


Code
my $days_in_month = 30; 
my $summary_row = 1;
my $offset = 2 ;
$days_in_month = $days_in_month +$offfset ;
my $transit_day = $days_in_month-$offfset;
for my $day_rows (1..$days_in_month){
for my $cols(0..$#{$headings}){
if($cols == 1 && $day_rows == 1){
printf $formulas->{default}->($summary_row);
}
elsif($cols == 4 && $day_rows == 1){
printf $formulas->{default1}->($summary_row) ;
}
else {
if ( exists $formulas->{$cols} ){
if ($cols == 1 || $cols == 4){
printf $formulas->{$cols}->($summary_row);
}
else {
if($cols != 8 ) {
printf $formulas->{$cols}->($summary_row);
}
elsif ($cols == 8 && $day_rows >= $transit_day ){
printf $formulas->{$cols}->($summary_row) ;
}
}
}
else {
}
}

}
$summary_row++;
}



Firstly , im calling the default columns for row 1
and then im continuing with the normal columns which is sime.
I owuld want this hard coding stuff to be simplified by using
a hash of rownum and col num which has its value as the functions

Ex
in my formula hash
i have
default => (we have formula here)

i would want to have

default=>rownum=>colnum=>{sub ...}

so that i can call all the defualt with the corresponding row num and coil num an dthen go ahead printing the normal column formulas


Quote
Explaination of if conditions

if($cols == 1 && $day_rows == 1) # for row one and column 1 we have default formula
elsif($cols == 4 && $day_rows == 1) # for row one and column 4 we have default1 formula

if ( exists $formulas->{$cols} ) #After printing default , print all the columns with formulas correspoding to col number

At the end iam printing a summary column 8 for some offset dates (i.e from last day -2 in this code )

Code
if($cols != 8 ) { 
printf $formulas->{$cols}->($summary_row);
}
elsif ($cols == 8 && $day_rows >= $transit_day ){
printf $formulas->{$cols}->($summary_row) ;
}


Thanks
Teja

(This post was edited by Tejas on Dec 17, 2014, 10:38 AM)


Edit Log:
Post edited by Tejas (User) on Dec 17, 2014, 10:38 AM


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

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