Hey all, trying to make a comprehensive perl function that can be used in various flows for my work. The stuff that I'm struggling with is:

$outputType – if left blank (‘’) assume array reference (‘aref’), only other possibility is hash reference (‘href’)

$headingAref – if left blank (‘’) assume first line as the header. Otherwise pass in an array reference where each element in the array is the column heading in the order they appear in the file.

$readLineFrom – if left blank (‘’) start at the first line, otherwise if a number defined start reading at that line of the file.

$readLineTo – if left blank (‘’) read till EOF, otherwise if a number defined end reading at that line of the file.

Trying to read the csv from a certain row to another is proving to be difficult. I tried to keep an index and use a nested for loop but this is still printing all my rows, not just the ones I want. Furthermore, I need to be able to add my own personal header according to $headerAref. I tried opening the file for reading and writing using "+<" but that causes my code to only print the header. I also need to duplicate all of this for a hash output type as well but I have not yet looked into this. Right now, my code is able to output the csv file as an array or arrays as requested but reads each row (not the range given) and can't add a header. Any help would be appreciated.

#!/tool/pandora64/bin/perl5.8.8 #MODULES use strict; use warnings; use Pod::Usage; use Data::Dumper; use Getopt::Long; use File::Basename; use Cwd 'abs_path'; use Data::Dump 'dd'; use Data::Dumper qw(Dumper); use Text::CSV; #my $output = readCSV($pathToCSV, $columnSeperatorChar, $cellEncapsual +tingChar, $outputType, $headingAref, $readLineFrom, $readLineTo, $ign +oreFirstLineBool); my @headerArray = "a,b,c,d,e,f,g,h,i,j,k"; my $headerArrayRef = \@headerArray; my $pathToCSV = "input.csv"; my $columnSeperatorChar = ""; my $cellEncapsulatingChar = ""; my $outputType = ""; my $headingAref = "$headerArrayRef"; my $readLineFrom = 8; my $readLineTo = ""; my $ignoreFirstLineBool = 0; $columnSeperatorChar = "," if ($columnSeperatorChar eq ""); $cellEncapsulatingChar = "" if ($cellEncapsulatingChar eq ""); $outputType = 'aref' if ($outputType eq ""); my @output; my $rowCounter = 0; my @index; my $currentIndex = 0; open(my $file, "<", $pathToCSV) or die "File could not be opened $!\n" +; =begin comment if($headingAref ne ""){ my @headerArrayCopy = @$headingAref; print ($file "@headerArrayCopy\n"); #adds header } =end comment =cut while (my $line = <$file>){ chomp $line; $currentIndex++; if ($readLineTo eq ""){ $rowCounter++; $readLineTo = $rowCounter; } for ($currentIndex >= $readLineFrom and $currentIndex <= $readLine +To){ my @row = split ("$columnSeperatorChar", $line); foreach my $cell (@row){ $cell = "$cellEncapsulatingChar$cell$cellEncapsulatingChar +" } push @output, \@row; } } print Dumper(\@output);

In reply to Reading CSV Function by workInProgress12

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.