Hi Monks,
I'm stuck on a problem and I hope that one of you may be able to help.
Initially, I thought the problem was that an variable in the main program was not accessible to a subroutine. After much testing, I'm now fairly certain the problem lies with how I'm trying to define the array. I've cut the program down to the smallest I can to demonstrate my issue.
What is the best way to define a 2d array without knowing exactly how big the array will be? Any help appreciated.
!/usr/bin/perl -Tw
#use T taint, and W warnings
use strict;
use Data::Dumper;
require Text::CSV;
#----------------------------------------------------------
sub printRow{
#input the row number
print Dumper(@d_array);
}
#----------------------------------------------------------
#read the csv
my $csv_filename="test.csv";
# Getting input into working file
my $line;
my $line_number;
my $csv = Text::CSV->new;
my $separator = ',';
my $column = '';
my @d_array;
my $cnt;
unless (open(INFILE, $csv_filename)){
print "Cannot open file \"$csv_filename\"\n";
exit;
}
foreach $line (<INFILE>){
$line_number++;
if ($csv->parse($line)) {
my @field = $csv->fields;
my $count = 0;
for $column (@field) {
++$count;
$d_array[$line_number][$count]=$column;
}
} else {
my $err = $csv->error_input;
print "parse() failed on argument: ", $err, "\n";
}
close INFILE
}
print Dumper(@d_array);
&printRow($cnt);
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.