New to perl here and have been trying to figure this one out for a couple days now. I think I need nested forall loops or something along those lines. The task that needs to be done is to assign the columns to a multi level hash. So if my csv file was something like something below. Important to note that there are repeated values, which is the case for the provided sample output, (ie info 01 = info11, info02 = info12, etc.), but this is not always the case.
header1,header2,header3....
info01,info02,info03...
info11,info12,info13..
: : :
The hash would look like (with the appropriate brackets)
$VAR1 = {
info01 => {
info02 => {
info03 => {
info11 => {
info12 => {
info13 => {
What I have right now:
# MODULES
use strict;
use warnings;
use Pod::Usage;
use Data::Dumper;
use Getopt::Long;
use File::Basename;
use Cwd 'abs_path';
use Data::Dumper qw(Dumper);
use Text::CSV;
my $csv = Text::CSV->new({
binary => 1,
auto_diag => 1,
sep_char => ','
});
my @columns;
open(my $input, '<:utf8',"input.csv") or die;
while (<$input>){
$csv->parse($_) or die "parse() failed: ";
my @data = $csv->getline($input);
for my $i (0..$#data) {
# push @{$columns[$i]}, $data[$i];
push @{$columns[$i]}, $data[$i];
}
}
close $input;
my %hash = map {shift @$_ => $_} @columns;
use Data::Dumper;
print Dumper(\%hash);
This is giving me my values all in one line and not how is required
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.