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

#!/usr/bin/perl use strict; use warnings; use Data::Table; my $t = Data::Table::fromCSV("ravi.csv"); # Read a csv file into + a table object print $t->csv; # Display a 'portrait' HTML TABLE + on web.

I am trying some programs on csv files. By searching I saw this "Data::Table". I copied the example in the page given. but its showing an error.

Later I came to know that it need column names. Without column names how can I use this. Because the csv file I am using is auto-generated and does not contains any names in the top of the file. I have permissions only to read the files.

So as per module page

table Data::Table::fromCSV ($name, $includeHeader = 1, $header = ["col +1", ... ]) create a table from a CSV file. return a table object. $name: the CSV +file name. $includeHeader: 0 or 1 to ignore/interpret the first line +in the file as column names, If it is set to 0, the array in $header +is used. If $header is not supplied, the default column names are "co +l1", "col2", ...
#!/usr/bin/perl #!/usr/bin/perl # use strict; use warnings; use Data::Table; my $t = Data::Table::fromCSV("/root/prac/Telenor_CDR/SMSCDR_POSTPAID_1 +51013000000_10.84.0.29_AS.log",0); #print $t->csv; #t2 = $t->group(["Department","Sex"],["Name", "Salary"], [sub {scalar +@_}, \&average], ["Nof Employee", "Average Salary"]); my $t2 = $t->group(["col1","col9"]); print $t2->csv;
Still its not working

Invalid column col9Unknown column col9 at /root/perl5/lib/perl5/Data/T +able.pm line 1794. Data::Table::group('Data::Table=HASH(0x15e84c0)', 'ARRAY(0xff0 +300)') called at test_table.pl line 13

Replies are listed 'Best First'.
Re: Data::Table showing error
by BrowserUk (Patriarch) on Oct 27, 2015 at 05:08 UTC

    Pass a second argument of 0 to tell the module not to use headers:

    #!/usr/bin/perl use strict; use warnings; use Data::Table; my $t = Data::Table::fromCSV( "ravi.csv", 0 ); # Read a csv file + into a table object (no headers) print $t->csv; # Display a 'portrait' HTML TABLE + on web.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Ya, I passed it and its showing an error again.

      Invalid column col9Unknown column col9 at /root/perl5/lib/perl5/Data/T +able.pm line 1794. Data::Table::group('Data::Table=HASH(0x15e84c0)', 'ARRAY(0xff0 +300)') called at test_table.pl line 13
Re: Data::Table showing error
by kevbot (Vicar) on Oct 27, 2015 at 05:51 UTC

    I think you recently updated your node with additional information. It is helpful if you clearly annotate any updates. For example, include a header like UPDATE.

    When your file has no header row, Data::Table will create default column names in this format,

    col1, col2, col3, etc.

    The error you are now getting,

    Invalid column col9Unknown column col9 at /root/perl5/lib/perl5/Data/T +able.pm line 1794. Data::Table::group('Data::Table=HASH(0x15e84c0)', 'ARRAY(0xff0 +300)') called at test_table.pl line 13
    suggests that there are fewer than 9 columns in your input file.

      But my input file contains nearly 80 columns.

       Tue Oct 13 00:10:00 2015|33594|959796245242|08211019|Tue Oct 13 00:10:00 2015|Tue Oct 13 00:10:00 2015|CMT|Undelivered|none|Submit|0|SMSC_PR_LC_SMSC_InvalidDestAddress|GSM|INVALID|ASCII|92|92|no||no|no||None|No|NO|no|no|0|0|0|0||959790000028||8|0||0|no|no|default_billing|-1|0|no|no|1|1|1|1|0|1|0|0|||||Tue Oct 13 06:10:00 2015|SR|||IV|011809614446716000046|||0|0||0|0||0||||08211019||||||||||||||||||

      This is the sample of my input

        See my other reply in Re^3: Data::Table showing error. You are not defining the delimiter properly, and therefore the data is being loaded as one (very wide) column. Once you set your delimiter properly (for pipe-separated values), you should get multiple columns.
Re: Data::Table showing error
by kevbot (Vicar) on Oct 27, 2015 at 05:37 UTC
    Hello ravi45722,

    The syntax in your post appears to be incorrect. If you have a csv file that lacks a header, create the Data::Table object like this:

    my $t = Data::Table::fromCSV("ravi.csv", 0);
    Could you post an example showing the format of your csv file? Also, are you using an up-to-date version of Data::Table? The current version is 1.72. You can get the version by running this one-liner:
    perl -MData::Table -e 'print "$Data::Table::VERSION\n"'

      Its showing it as 1.68.

       Tue Oct 13 00:10:00 2015|33594|959796245242|08211019|Tue Oct 13 00:10:00 2015|Tue Oct 13 00:10:00 2015|CMT|Undelivered|none|Submit|0|SMSC_PR_LC_SMSC_InvalidDestAddress|GSM|INVALID|ASCII|92|92|no||no|no||None|No|NO|no|no|0|0|0|0||959790000028||8|0||0|no|no|default_billing|-1|0|no|no|1|1|1|1|0|1|0|0|||||Tue Oct 13 06:10:00 2015|SR|||IV|011809614446716000046|||0|0||0|0||0||||08211019||||||||||||||||||

      This is my log file(one sample). In this I want to group all the events and counts that where 10 (Submit),13 (GSM), 14 (SMPP) occured. Here (in example) the fields are different 13(GSM),14 (INVALID). But I need to count as per my condition

        The default separator for the fromCSV command is a comma, i.e. comma-separated values. Your file appears to contain pipe-separated values, so you need to define the delimiter. The Data::Table documentation for the fromCSV command states,
        if the delimiter or the qualifier is a special symbol in regular expre +ssion, you must escape it by '\'. For example, in order to use pipe s +ymbol as the delimiter, you must specify the delimiter as '\|'.
        Try this with your file,
        my $t = Data::Table::fromCSV("ravi.csv", 0, undef, { delimiter => '\|' + });
Re: Data::Table showing error
by Anonymous Monk on Oct 27, 2015 at 04:56 UTC

    Amazing that your search ended on Data::Table and not had been come in contact with Text::CSV*!

    If you lack header, then don't use Data::Table for headers seem to be integral part of it. Use Text::CSV* instead, which can deal with (absence of) headers separately.

Re: Data::Table showing error
by Anonymous Monk on Oct 27, 2015 at 05:14 UTC

    Post update, re: "So as per module page ... my $t = new Data::Table("$file",$includeHeader = 1);" ...

    I do not see the constructor new in Data::Table pod taking in a file name (as the 1st parameter). First parameter of new is an array reference as shown in pod & forcefully suggested by the error message. You need to use fromCSV class method apparently to be have the module parse the file for you.

    After you have solved that, you may encounter undeclared variable error, something like Global symbol "<variable name>" requires explicit package name .... Try to understand the documentation before coding after reading a line. In the pod, purpose of "$includeHeader = 1" is to impart the reader the knowledge about the 2nd positional parameter; that it can be used to deal with (lack of) headers.

Re: Data::Table showing error
by jeffa (Bishop) on Oct 27, 2015 at 18:05 UTC

    From your code:

    print $t->csv; # Display a 'portrait' HTML TABLE on web.

    Question -- do you want to output HTML or CSV? If you wish to output an HTML table as the comment suggests, you might be able to accomplish this with the latest version (v1.04) of my Spreadsheet::HTML module:

    use strict; use warnings; use Spreadsheet::HTML qw( portrait ); print portrait( file => 'ravi.csv', sep => '|', table => {border=>1} ) +;

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      Sorry, I changed the code but forget to change the comment. My requirement is to group some fields & count how many events occurred.

       Tue Oct 13 00:20:01 2015|189333794|32665|959799899574|Tue Oct 13 00:19:57 2015|Tue Oct 13 00:20:01 2015|CMT|Delivered|none|Submit|0|SMSC_no_error|SMPP|GSM|ASCII|105|92|no||no|no||None|No|DestPrepaid|no|no|0|0|0|0|414061102110510||959790000004|0|0||0|no|no|default_billing|-1|0|no|no|0|0|0|0|1|1|1|1|959790000024|32665|smsgw|78655|Wed Oct 14 00:19:57 2015|SR|||CO|011816714446721970009||Default|0|370870121||0|0||0|||||||||Oct-13-2015  00:19:57.405709|1|1||||||||||0|

      This is example of my input data. In this I want to group 10th (Submit), 13th (GSM), 14th (SMPP) fields & count how many events occurred. In the example the 13th field is SMPP. So don't count this event. Whenever all I fields met the condition then I want to consider it as a event. This is my total requirement.