I have been testing some code on a smaller scale and something is confusing me. Why does the
print "$csv2\n"; command at the end of this script print each full line of the csv. $csv2 has been populated with the key data of csv2hash, so I'm confused as to why it would print each line? Is each line considered a key in this instance?
#!/bin/perl
my @csv2 = (); # Creates new empty array @csv2
open CSV2, "<csv2" or die; # Creates the file handle CSV2, input
+s the data from csv2 into CVS2
@csv2=<CSV2>; # populates @csv2 array with data from file handle
+ CSV2
close CSV2;
my %csv2hash = (); # Creates empty hash csv2hash
for (@csv2) { # for data in @csv2 array
#chomp;
my ($title) = $_ =~ /^.+?,\s*([^,]+?),/; #/define the data whic
+h is the title
#Indicate that title data will input into csv2hash
$csv2hash{$_} = $title;
}
foreach my $csv2 (keys %csv2hash) { # iterates over all the keys in %c
+sv2hash, assigning each one to $csv2
my $value = $csv2hash{$csv2};
print "$csv2\n"; # This prints the data line by line in the csv2
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.