-2

Can someone please let me know how to achieve the below through Perl.

I have a CVS file with values like below.

i/p:

Symbol,A,B,C,D,E,F

66,.8500,.8500,1.1600,1.1600

O/P:

[['Symbol','A','B','C','D','E','F' ], ['66','.8500','.8500','1.1600','1.1600']];

and assign the same to a variable.

How can i able to achieve this through Perl script.

$sts = open( CSV, "< File.csv" );

while (<CSV>) {

   $csv = $_;

   chomp($csv);

   @csv_rcd = split( ',', $csv );
   foreach $cell (@csv_rcd) {
      push @rowdata, $cell;
   }

   push @$somedata, @rowdata;

}

But the above code didn't produce the desired o/p.

Sobrique
  • 52,974
  • 7
  • 60
  • 101
HariHaran
  • 33
  • 5
  • Added the code, Could you please help me to achieve the desired result. – HariHaran Apr 27 '17 at 07:54
  • What's the problem you're having though? What do you not understand about the result you're getting? – Sobrique Apr 27 '17 at 08:01
  • Hi i want to produce the o/p in this fomat : [['Symbol','A','B','C','D','E','F' ], ['66','.8500','.8500','1.1600','1.1600']]; (from file to variable) through perl. I wrote a code but it;s not working, Can you please help on this. – HariHaran Apr 27 '17 at 08:07
  • "it's not working" is not explaining what problem you're having. – Sobrique Apr 27 '17 at 08:18

1 Answers1

1
use strict;
use Data::Dumper;

my $somedata;
while (my $csv = <DATA>) {
    chomp $csv;
    push @$somedata, [ split (',' , $csv)];
}
print Dumper($somedata);
__DATA__
Symbol,A,B,C,D,E,F
66,.8500,.8500,1.1600,1.1600

Here is a full example to use PDF::Table

use PDF::API2;
use PDF::Table;

my $somedata;
while (my $csv = <DATA>) {
    chomp $csv;
    push @$somedata, [ split (',' , $csv)];
}

my $pdftable = new PDF::Table;
 my $pdf = new PDF::API2(-file => "table_of_lorem.pdf");
 my $page = $pdf->page;

$pdftable->table($pdf, $page, $somedata, x => 50, w => 495, start_y => 500, start_h => 300,
    padding => 5, background_color_odd => 'gray', background_color_even => 'lightblue');

$pdf->saveas();

__DATA__
Symbol,A,B,C,D,E,F
66,.8500,.8500,1.1600,1.1600
BOC
  • 1,109
  • 10
  • 20
  • Does this code produce this o/p: [['Symbol','A','B','C','D','E','F' ], ['66','.8500','.8500','1.1600','1.1600']]; – HariHaran Apr 27 '17 at 07:56
  • No. The formating of output can be done with Data::Dumper:my $d = Data::Dumper->new([$somedata]); $d->Terse(1)->Indent(0); print $d->Dump, "\n"; This gives exactly the expected format. – BOC Apr 27 '17 at 08:02
  • It means that we need to install this Data::Dumper package right? Can't this be done through regular perl scripting? – HariHaran Apr 27 '17 at 08:03
  • Data::Dumper is a core package of Perl that is always present in perl distributions (many packages depend of it). You can do the same with regular perl. It is often given as exercise for learning perl. It requires to learn how to write recursive routine and how to use the ref function (https://perldoc.perl.org/functions/ref.html). – BOC Apr 27 '17 at 08:09
  • Someone has already asked to rewrite Dumper as homework: http://stackoverflow.com/questions/1038271/recursively-printing-data-structures-in-perl – BOC Apr 27 '17 at 08:14
  • But it's also installed by default, as part of the base perl install – Sobrique Apr 27 '17 at 08:18
  • Hi BOC, Can i pass this "$d->Dump" to Pdf::table method for creating the table in PDF? – HariHaran Apr 27 '17 at 10:19
  • $d->Dump is used to convert from perl data structure to a string. Pdf::table expects a perl data structure. You should pass directly $somedata to Pdf::Table. – BOC Apr 27 '17 at 11:16
  • Means you are saying the no need to go for "$d = Data::Dumper->new([$somedata]);" am i right. – HariHaran Apr 27 '17 at 12:44