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:
Error while printing records to table

 



hem_k24
Novice

Jul 3, 2013, 2:19 AM

Post #1 of 4 (368 views)
Error while printing records to table Can't Post

Hi Masters,

forming records as below<

my %input;
$input{$EMPNO}[0]="DOB,Name,Empid,salary,Gross_salary,Department,Company";
$input{$EMPNO}[1]="Month,NET_Salary";

The code which is used to put the records in table and i am reading each field and placing in table</p>

{

my @data;
my $table;

foreach my $k (keys %input) {
foreach (@{$input{$k}}) {
chomp();
push( @{@data[$.]},( split /[,\n]/, $_));
}
}
$table .= "<table border=1>";

for my $i ( 0 .. $#data ) {
$table .= "<tr>";
for ( @{@data[$i]}){
$table .= "<td>$_</td>\r\n";
}
$table .= "</tr>";
}
$table .= "</table>";

}


The output i want in a tabular format, when i run the above code i have error,Can't use an undefined value as an ARRAY reference

I want to display the data in tabular format, as i am not much familiar with the array refernce, any solution for this, i
already posted this on monk and used the same code but getting error

DOB,Name,Empid,salary,Gross_salary,Department,Company
Month,NET_Salary


BillKSmith
Veteran

Jul 3, 2013, 5:23 AM

Post #2 of 4 (359 views)
Re: [hem_k24] Error while printing records to table [In reply to] Can't Post

The data structure @data is an array of arrays. (i.e. an array of references to anonymous arrays). You must push a reference into @data for each employee and push the employee data into that array.

Your code that references that structure is much better, but needs work.

References and their use in complex data structures are documented in:

Code
perldoc perldsc


Here is one way to build your table.

Code
use strict; 
use warnings;
sub build_emp_table {
#$input{$EMPNO}[0]="DOB,Name,Empid,salary,Gross_salary,Department,Company";
#$input{$EMPNO}[1]="Month,NET_Salary";
my %input = @{$_[0]};
my @data;
foreach my $emp (values %input) {
push @data, [];
foreach (@$emp) {
chomp;
push @{$data[-1]}, split /[,\n]/;
}
}

my $table .= "<table border=1>";
foreach my $row (@data) {
$table .= "<tr>";
foreach (@$row) {
$table .= "<td>$_</td>\r\n";
}
$table .= "</tr>";
}
$table .= "</table>";
return $table;
}

Good Luck,
Bill


hem_k24
Novice

Jul 3, 2013, 6:18 AM

Post #3 of 4 (349 views)
Re: [hem_k24] Error while printing records to table [In reply to] Can't Post

i have the same data in csv file, read the records from the csv file and write it to table as we did earlier.

The input data:

DOB,Name,Empid,salary,Gross_salary,Department,Company
Month,NET_Salary

The code which is used to put the records in table and i am reading each field and placing in table</p>

{

my @data;
my $table;

open( F, "$Filename" )
|| die("Could not open $Filename");

while ( my $line = <F> ) {
chomp($line);
push( @values,split /[,\n]/, $line );
}

close(F);

$table .= "<table border=1>";

foreach my $row (@values) {
$table .= "<tr>";
foreach ($row) {
$table .= "<td>$_</td>\r\n";
}
$table .= "</tr>";
}

$table .= "</table>";}

when i use the same code,i am getting all values in singel column


DOB
Name
Empid
salary
Gross_salary
Department
Company
Month
NET_Salary


BillKSmith
Veteran

Jul 3, 2013, 6:54 AM

Post #4 of 4 (342 views)
Re: [hem_k24] Error while printing records to table [In reply to] Can't Post

Please read the documentation I refered you to. the variable @values must be an array of arrays.
Good Luck,
Bill

 
 


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

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