Perlseeker_1 has asked for the wisdom of the Perl Monks concerning the following question:

subroutine to read the data from csv to array

{my $csv = Text::CSV->new(); open (FILE, "$Filename") or die "Couldn't open location file: $!"; while (<FILE>) { $csv->parse($_); push(@report, [$csv->fields]); } close FILE; return ( join( "\n", @report ) );}

Output after reading

$VAR1 = [ 'Title_1', 'Title_2', 'Title_3', 'Title_4', 'Title_5', 'Title_6', 'Title_7', 'Title_8', 'Title_9', 'Title_10' ]; $VAR2 = [ 'Apr13', 'Gkel_gz.dat.gz', '1456', '1456', 'Check_ABC_TT1_INTL.dat', '489', '', '', '', 'TRUE' ]; $VAR3 = [ '\' \'', '\' \'', '\' \'', '\' \'', 'Check_AB_TT1.dat', '124' ]; $VAR4 = [ 'May13', 'Gkel_gz.dat.gz', '6541', '6541', 'Check_ABC_TT1.dat', '466', 'Check_AB_TT1_TT2.dat', '07364', '07364', 'TRUE' ]; $VAR5 = [ '\' \'', '\' \'', '\' \'', '\' \'', 'Check_AB_TT1.dat', '263' ]; $VAR6 = [ 'Jun13', 'Gkel_gz.dat.gz', '62382', '62382', 'Check_ABC_TT1.dat', '470', 'Check_AB_TT1_TT2.dat', '04220', '04220', 'TRUE' ]; $VAR7 = [ '\' \'', '\' \'', '\' \'', '\' \'', 'Check_AB_TT1.dat', '278' ];

Then i am assigning the array to email body as below

my $body = ConvertData(); createEmail( $body, $Filename );

But when i execute the script i ma getting output

as:

Output

ARRAY(0x6000000000f09e48) ARRAY(0x6000000000f1dcc0) ARRAY(0x6000000000f1dfc0) ARRAY(0x6000000000f0a148) ARRAY(0x6000000000f1dfa8) ARRAY(0x6000000000f0a328) ARRAY(0x6000000000f1df90)

Please help on this

Replies are listed 'Best First'.
Re: How to read data from csv file and place the data on HTML format
by Tux (Canon) on Jun 27, 2013 at 06:55 UTC

    Your parsing is unsafe and you are not returning html and you are printing references instead of fields

    use Text::CSV; use HTML::Entities; sub ConvertData { my $csv = Text::CSV->new ({ binary => 1, auto_diag => 1 }); open my $fh, "<", $Filename or die "Couldn't open location file $F +ilename: $!"; my @report = ("<table>"); while (my $row = $csv->getline ($fh)) { push @report, "<tr>"; push @report, "<td>", encode_entities ($_), "</td>" for @$row; push @report, "</tr>"; } push @report, "</table>"; close $fh; return join "\n" => @report; }

    Enjoy, Have FUN! H.Merijn
Re: How to read data from csv file and place the data on HTML format
by rjt (Curate) on Jun 27, 2013 at 07:02 UTC

    You are getting the ARRAY(0x....) output because you are asking Perl to stringify array references with this code:

    push(@report, [$csv->fields]); # Later... return ( join( "\n", @report ) );

    The [ ] brackets create an anonymous ARRAY ref and stick the contents of $csv->fields inside. Hence @report contains a bunch of references, and when you try to join them, you just get a bunch of ARRAY(0x...) strings.

    Using ARRAY refs in @report was probably the right idea, so now you just need to do a little extra work to dereference each element and join them together. Here's a quick runnable example illustrating the idea:

    my @refs = ([qw/foo bar baz/], [qw/fee fie foe/], [qw/red green/]) +; print join "\n", map { join ',', @$_ } @refs;

    Instead of printing the result of join, you can of course assign it to a scalar.

      Hi Monks,

      I used the above recommended one, i can see the output on

      email body but it was not in order

      i want the data to be displayed in order

      Any help on this please

        Hi Monks,
        Title_1,Title_2,Title_3,Title_4,Title_5,Title_6,Title_7,Title_8,Title_ +9,Title_10 Apr13,Gkel_gz.dat.gz,1456,1456,Check_ABC_TT1_INTL.dat,489,Gkel_gz.dat, +124,422,TRUE ' ',' ',' ',' ',Check_AB_TT1.dat,124 May13,Gkel_gz.dat.gz,6541',6541,Check_ABC_TT1.dat,466,Check_AB_TT1_TT2 +.dat,07364,07364,TRUE ' ',' ',' ',' ',Check_AB_TT1.dat,263 Jun13,Gkel_gz.dat.gz,62382,62382,Check_ABC_TT1.dat,470,Check_AB_TT1_TT +2.dat,04220,04220,TRUE ' ',' ',' ',' ',Check_AB_TT1.dat
        print DETAIL "$pattern,$Orig_gz[0],$Orig_gz[1],$Orig_gz[1],$INTL[0]->[ +0],$INTL[0]->[1],$Orig[0],$Orig[1],$Orig[1],TRUE\n"; print DETAIL "'\t','\t','\t','\t',$INTL[1]->[0],$INTL[1]->[1]\n"; return join "\n", map { join ',', @$_ } @report; my $body = ConvertData();

        How can i put the data in tabular form, any suggestions

        In output i am seeing ',', how can i remove that

        As i am new to this, any inputs please