Skip to content

Instantly share code, notes, and snippets.

@C-F-K
Last active August 29, 2015 14:09
Show Gist options
  • Save C-F-K/72e6e6a30ae8414540a0 to your computer and use it in GitHub Desktop.
Save C-F-K/72e6e6a30ae8414540a0 to your computer and use it in GitHub Desktop.
# CODE PARSES .CSV FILES:
# @csvrows = (@$row1, @$row2, @$row3...)
# @row1 = ("field","other field","bla bla bla"...)
# ie. an array of references to other arrays containing the values of the fields in each row
# CODE NEEDS TO RETURN:
# @$arrayref = (%$hash_of_row1, %$hash_of_row2...)
# %hash_of_row1 = {FIELDS => @$row1}
# %hash_of_row2 = {FIELDS => @$row2}...
# @row1 = (%$hash_of_field1, %$hash_of_field2...)
# %hash_of_field1 = {VALUE => "field"}
# %hash_of_field2 = {vALUE => "other field"}...
# ie. a reference to an array containing references to hashes with single keys with values of array references containing references to hashes with single keys with values of the contents of each field
# (try not to vomit)
# (hashes are the Perl equivalent of JS objects)
use HTML::Template;
my @loop_array = (); # initialize
while (@csvrows) {
# what i did wrong at first: structuring the loop to reflect 3 levels of output whereas there are only 2 levels of input
# comments are numbered by the order in which i eventually built the loop
# which i did by mentally tracking the nature of each bit of data versus its intended place in the template loop structure
# and declaring variables as necessary rather than trying to build top-down
my $row_array_ref = shift @csvrows; # 1. this is now an array ref containing field values
# 2. each of these needs to become a hash ref with the value assigned to a single key VALUE
my @fields_array; # 7. more backwards causality
my %row_hash; # 9. yet more backwards causality
while (@$row_array_ref) {
my %field_value_hash; # 5. backwards causality, inside loop because need a new one every time
my $field_value_string = shift @$row_array_ref; # 3. this is now a string
$field_value_hash{VALUE} = $field_value_string; # 4. now it's inside a hash
push @fields_array, \%field_value_hash; # 6. value hash ref pushed to array
}
$row_hash{FIELDS} = \@fields_array; # 8. that array is now a ref in a hash
push @loop_array, \%row_hash # 10. becoming repetitive
}
my $template = HTML::Template->new(
filename => __DATA__, # this wouldn't work but just so you can also see the template
loop_context_vars => 1,
);
$template->param(FILENAME => __DATA__);
$template->param(ROWS => \@loop_array); # here's the place where the array ref is actually needed
print $template->output;
__DATA__
<!DOCTYPE html>
<html>
<head>
<title>
<TMPL_VAR NAME=FILENAME>
</title>
</head>
<body>
<table>
<TMPL_LOOP NAME=ROWS>
<tr>
<TMPL_IF NAME="__FIRST__">
<TMPL_LOOP NAME=FIELDS>
<th><TMPL_VAR NAME=VALUE></th>
</TMPL_LOOP>
<TMPL_ELSE>
<TMPL_LOOP NAME=FIELDS>
<td><TMPL_VAR NAME=VALUE></td>
</TMPL_LOOP>
</TMPL_IF>
</tr>
</TMPL_LOOP>
</table>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment