By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,838 Members | 1,434 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,838 IT Pros & Developers. It's quick & easy.

Help Creating a Hash from a CSV file

Expert Mod 2.5K+
P: 3,503
Ok, I have a ".csv" file that contains two lines (VERY LONG lines). Basically, the first line is the headers defining what each field in the second line is.

Expand|Select|Wrap|Line Numbers
  1. Name1,Name2,Name3,........
  2. Value1,Value2,Value3,.......
Unfortunately, I am unable to provide any sample data as it is proprietary and customer confidential, but that is essentially the format of this file.

Now, the first line of comma separated values, being the headers, contains 408 values. The second line contains the same number of comma separated fields, but some are empty. (FYI, empty fields would look like: value,,,,,,,value,,value).

Hopefully this is making sense. My question is this, I want to create a hash and print out these values (or lack there of) side by side so I can correlate what is missing with what is there. The reason for wanting this is because looking at it in this format is giving me a really bad headache.

So, I had used split to split the first line into an array and then did the same with the second line. After that, I tried putting them into a hash, but because of the missing values in the second line, some necessary elements did not get created because they are null. Thus, the comparison to see if the two element counts was off and the hash didn't get created.

Does anyone have any idea how I can get this to work and enter a null value into the array where there is a null value, thus enabling this printout to work properly? My code is below.

Expand|Select|Wrap|Line Numbers
  1. se strict;
  2. use warnings;
  4. ###### Array and Variable Declarations #####
  5. my @keysarray;
  6. my @valuesarray;
  8. ##### Open Files For Reading #####
  9. open (K, "<keys.txt");
  10. open (V, "<values.txt");
  12. my $totalelementsk = $#keysarray + 1;
  13. my $totalelementsv = $#valuesarray + 1;
  14. my %hash;
  16. print $totalelementsk, "\n";
  17. print $totalelementsv, "\n";
  19. if ($totalelementsk == $totalelementsv)
  20. {
  21.  for(my $i = 0; $i <= $totalelementsk ; $i++)
  22.  {
  23.     $hash{$keysarray[$i]} = $valuesarray[$i];
  24.  }
  25. }
  27.  for my $key ( keys %hash ) {
  28.     my $value = $hash{$key};
  29.     print "$key => $value\n";
  30.  }
Thanks in advance! I look forward to any assistance I can get.


Apr 25 '08 #1
Share this Question
Share on Google+
5 Replies

Expert Mod 2.5K+
P: 3,503
Just an FYI, as I am trying to get an answer to this today, I am cross posting this with another board as well. If I get an answer that works, I will post it here.


Apr 25 '08 #2

Expert 2.5K+
P: 4,059
see DIC
Apr 25 '08 #3

Expert Mod 2.5K+
P: 3,503
see DIC
Well, I know what you mean, but that could SOOO be misconstrued on here. :-)
Apr 26 '08 #4

Expert 2.5K+
P: 4,059
Hey, you're just a "New" DIC head, but I am a DIC head so please show me the proper DIC respect. And if other people have dirty minds, well, I wish they would share their thoughts with me! ;)
Apr 26 '08 #5

Expert 100+
P: 971
Although the opportunity is great, I am going to pass on this one. )

Apr 26 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.