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:
How to write dispatch tables ? How to send arguements to the functions

 

First page Previous page 1 2 Next page Last page  View All


Zhris
Enthusiast

Dec 20, 2014, 7:17 PM

Post #26 of 34 (9431 views)
Re: [Tejas] How to write dispatch tables ? How to send arguements to the functions [In reply to] Can't Post

I've mulled this over. Lets not over complicate things and try to future proof the code. I've created an overly simplified version below and retired all use of coderefs. We simply tell each cell of the grid at a particular row / col the method to call, the value to print and the format object to use. If we want to print to stdout for debugging we can always create our own worksheet object that handles the same methods and arguments appropriately. If you can populate the grid with your test data, we can reduce repetition later with coderefs and a implement a populate list function like the one I provided a few posts back.


Code
use strict; 
use warnings;

# my $worksheet = ...
# my $format = ...

my $grid = [ ];
$grid->[1]->[0] = [ 'write_formula', '=blah:2:1', $format ];
$grid->[1]->[1] = [ 'write_string' , 'blah' , $format ];

my $max_row_i = 30;
my $max_col_i = 10;

for my $row_i ( 0 .. $max_row_i )
{
for my $col_i ( 0 .. $max_col_i )
{
my $cell = $grid->[$row_i]->[$col_i];

if ( defined $cell )
{
my ( $method, $val, $format ) = @$cell;

$worksheet->$method( $row_i, $col_i, $val, $format );
}
}
}


For example, to implement the following from your code:


Code
if ($cols == 1 && $day_rows == 1) 
{
$summary_worksheet-> write_formula($summary_row, $cols,$formulas->{default}->($summary_row),$date_format);
}


Use (assuming the formula is 'blah' and you really mean row 2, col 2):


Code
$grid->[1]->[1] = [ 'write_formula', 'blah', $date_format ];


Since you are hardcoding into the grid, you don't need to read the row and col from variables when constructing your formulas, just hardcode the numbers. We can revisit this later.

Regards,

Chris


(This post was edited by Zhris on Dec 20, 2014, 7:27 PM)


Tejas
User

Dec 21, 2014, 12:00 AM

Post #27 of 34 (9415 views)
Re: [Zhris] How to write dispatch tables ? How to send arguements to the functions [In reply to] Can't Post

I have modified it per requirement and below is the code

Code
use strict; 
use warnings;


my %custom_formats ;

$custom_formats{4} = "DATE_FORMAT"; # everything in col 1 and 4 are dates , the key of this hash is column number
$custom_formats{1} = "DATE_FORMAT"; # everything in col 1 and 4 are dates , the key of this hash is column number
$custom_formats{1}{1} = "DATE_FORMAT"; #everything in row 1 and col 1,4,7,9 ARE dates , KEYS ARE ROW AND COL
$custom_formats{1}{4} = "DATE_FORMAT"; #everything in row 1 and col 1,4,7,9 ARE dates , KEYS ARE ROW AND COL
$custom_formats{1}{7} = "FORMAT"; #everything in row 1 and col 1,4,7,9 ARE not dates , KEYS ARE ROW AND COL
$custom_formats{1}{9} = "FORMAT"; #everything in row 1 and col 1,4,7,9 ARE not dates , KEYS ARE ROW AND COL



my %grid ;

$grid{1}{1} = sub { qq/=EOMONTH(B2,-1)+1/ };
$grid{1}{4} = sub { my ($row,$offset) = @_; $row = $row+1 ;qq/=B${row}-$offset/ };
$grid{1}{7} = sub { my ($row,$offset) = @_; $row = $row+1 ;qq/=B${row}-$offset/ };
$grid{1}{9} = sub { my ($row,$offset) = @_; $row = $row+1 ;qq/=B${row}-$offset/ };

#my $days_in_month = Days_in_Month("2014","09")

my $days_in_month = 30;

my_func("formula_hash",$days_in_month,3);


sub my_func {

my ($fetch,$no_of_days,$offset) = @_ ;
$no_of_days += $offset;
my $no_of_cols = 10;
for my $row ( 1 .. $no_of_days )
{

for my $col ( 0 .. $no_of_cols)
{
#my $val = $grid->[$row]->[$col];
#my $format = $custom_formats->[$row]->[$col];
my $val = $grid{$row}{$col};
my $row_col_format = $custom_formats{$row}{$col};
my $col_format = $custom_formats{$col};
if ( defined $val && defined $row_col_format )
{
print "1 . $row,$col \n";
print "Defined => $custom_formats{$row}{$col}\n";
}
elsif(defined $col_format)
{
print "2. $row,$col \n";
print "Defined Format=> $custom_formats{$col}\n";
}
else
{
}
}
print "\n";
}

}


This code has some issues.

Ist when i run wiuth strict refs
Error is

Quote


So when i removed it , the output doesnt match my imagination
Probabaly due to auto vivification

Quote

12,1 => 2
Defined Format=> DATE_FORMAT
12,2 => 2
Defined Format=> HASH(0x840fddc)
12,3 => 2
Defined Format=> HASH(0x840fe0c)
12,4 => 2
Defined Format=> DATE_FORMAT
12,5 => 2
Defined Format=> HASH(0x840fe54)
12,6 => 2
Defined Format=> HASH(0x840fe84)
12,7 => 2
Defined Format=> HASH(0x840feb4)
12,8 => 2
Defined Format=> HASH(0x840fee4)
12,9 => 2
Defined Format=> HASH(0x840ff14)
12,10 => 2
Defined Format=> HASH(0x840ff44)


Though i dint define the format for other columns except 1 and 4, all the columns are getting satisfied

Thanks
Tejas


(This post was edited by Tejas on Dec 21, 2014, 12:10 AM)


Zhris
Enthusiast

Dec 21, 2014, 3:44 AM

Post #28 of 34 (9406 views)
Re: [Tejas] How to write dispatch tables ? How to send arguements to the functions [In reply to] Can't Post

Hi,

Your code is not nothing like the two I provided yesterday. Please see my previous post that showed a simplified version.

Chris


Tejas
User

Dec 21, 2014, 4:05 AM

Post #29 of 34 (9403 views)
Re: [Zhris] How to write dispatch tables ? How to send arguements to the functions [In reply to] Can't Post

Yeash, Thsi is like the array version , but i have chnaged it to hash to cater for the arguments.

The FORMULAS takes 1 ,2 or 3 arguements in some cases.

But Dont u think this works.
I Tried to implement array approach, but wasnt really sure how to send arguements.

Thanks
Tejas


Tejas
User

Dec 21, 2014, 4:10 AM

Post #30 of 34 (9401 views)
Re: [Zhris] How to write dispatch tables ? How to send arguements to the functions [In reply to] Can't Post

Or

Code
my $grid = [ ];  
$grid->[1]->[0] = [ 'write_formula', '=blah:2:1', $format ];
$grid->[1]->[1] = [ 'write_string' , 'blah' , $format ];


Iam just conevrting ur code to original formulas..


Code
$grid->[1]->[0] = [ 'write_formula', '=B${row}', $format ];  
$grid->[1]->[1] = [ 'write_string' , 'qq/=B${row}-$offset/' , $format ];


Now , i have 2arguements to be sent to first grid and 2 to second
As it seemed a bit toough , i had just changed the array approach to hash approach.

Here $row and $offset values have to be repplaced in grid 's second declaration
and Just $row in first.

Thanks
Tejas


Zhris
Enthusiast

Dec 21, 2014, 4:15 AM

Post #31 of 34 (9399 views)
Re: [Tejas] How to write dispatch tables ? How to send arguements to the functions [In reply to] Can't Post

Hi,

Your code looked like a totally fresh version, with more differences than you describe. The array is the most logical structure to represent rows and cols.

You don't need to use row and col variables for now when constructing formulas, re-introducing coderefs at this stage may cause confusion again.


Quote
Now , i have 2arguements to be sent to first grid and 2 to second


Its the same grid, just 2 different cells.

Chris


(This post was edited by Zhris on Dec 21, 2014, 4:18 AM)


Tejas
User

Dec 21, 2014, 4:27 AM

Post #32 of 34 (9394 views)
Re: [Zhris] How to write dispatch tables ? How to send arguements to the functions [In reply to] Can't Post

Hi Chris


Code
$grid{1}{1} = sub {  qq/=EOMONTH(B2,-1)+1/ };  
$grid{1}{4} = sub { my ($row,$offset) = @_; $row = $row+1 ;qq/=B${row}-$offset/ };
$grid{1}{7} = sub { my ($row,$offset) = @_; $row = $row+1 ;qq/=B${row}-$offset/ };
$grid{1}{9} = sub { my ($row,$offset) = @_; $row = $row+1 ;qq/=B${row}-$offset/ };


This is hash version, with functions,paraments here are to be replaced in the formulas ..
If u see the four formulas,the sub routine takes arguemets and its adding up the offset variable .

But how can that be ahceivable using array
Where should i ppush the $row and $offset paraments here in array..


Code
$grid->[1]->[0] = [ 'write_formula', '=B${row}', $format ];   
$grid->[1]->[1] = [ 'write_string' , 'qq/=B${row}-$offset/' , $format ];


Thanks
Tejas


(This post was edited by Tejas on Dec 21, 2014, 4:29 AM)


Zhris
Enthusiast

Dec 21, 2014, 5:35 AM

Post #33 of 34 (9386 views)
Re: [Tejas] How to write dispatch tables ? How to send arguements to the functions [In reply to] Can't Post

The aim was that you don't for now, you just hardcode the numbers. I was hoping to see your grid built in this manner, then reintroduce coderefs to improve. But handle in whatever manner you wish.

Chris


Tejas
User

Jan 23, 2015, 12:05 AM

Post #34 of 34 (9111 views)
Re: [Laurent_R] How to write dispatch tables ? How to send arguements to the functions [In reply to] Can't Post

Hi Laurant

I had doubts on dipatch tables again and i have re-read this post once again
And i am really grateful to you tht i have understood it better than i had last time when u ve posted this.

My doubt today was, "How many time we are opening the files "
And after rereading this post i have realizede tht we are just opeing it once while creating the hash and then when ever we come across the word, it is passed to specific file as it is already opening during creatiion
So , at run time the nameless subroutine is called to return but not the create_func , as it is already available as coderef.

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



Thanks for this beautiful explaination . Infact i have saved this expaination and every time i read it iam understanding it better.it all depends on the doubts i have to be precise :)
Please correct me if i ve mis-understood.


Quote
I passed to this script a file containing the full text of the Bible
(both Old and New Testament, in a, old French edition available on the Internet),
which I had previously (for another project) modified to remove diacritic signs (accents, cedillas, etc.)
and punctuation signs, and to lower case the full text,
so that my file was really a list of Bible's words (lower case) in the said edition (a file of about 4.2 MB).

The program created the 26 letter_a.txt, letter_b.txt, etc. files.
Five of them remained empty (no word starting with letters k, y, x, w and z was found in that edition of the Bible).
The others have between 155 bytes (letter H) and 96064 bytes (letter E).
This is not a surprise, as the letter E is by far the most common letter in the French language.

And regading bible , did u seperate words from each sentence or u meant no sentence in the book started with k, y, x, w and z



Thanks


(This post was edited by Tejas on Jan 23, 2015, 1:53 AM)

First page Previous page 1 2 Next page Last page  View All
 
 


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

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