Welcome to Dream.In.Code
Getting Help is Easy!

Join 86,260 Programmers. There are 1,941 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

Perl: Help Creating a Hash from a CSV File

 
Reply to this topicStart new topic

Perl: Help Creating a Hash from a CSV File

numberwhun
post 25 Apr, 2008 - 10:57 AM
Post #1


New D.I.C Head

*
Joined: 28 Mar, 2008
Posts: 6



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.

ie:
CODE

Name1,Name2,Name3,........
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.

CODE

se strict;
use warnings;

###### Array and Variable Declarations #####
my @keysarray;
my @valuesarray;

##### Open Files For Reading #####
open (K, "<keys.txt");
open (V, "<values.txt");

my $totalelementsk = $#keysarray + 1;
my $totalelementsv = $#valuesarray + 1;
my %hash;

print $totalelementsk, "\n";
print $totalelementsv, "\n";

if ($totalelementsk == $totalelementsv)
{
for(my $i = 0; $i <= $totalelementsk; $i++)
{
    $hash{$keysarray[$i]} = $valuesarray[$i];
}
}

for my $key ( keys %hash ) {
    my $value = $hash{$key};
    print "$key => $value\n";
}


Thanks in advance! I look forward to any assistance I can get.

Regards,

Jeff
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


mocker
post 25 Apr, 2008 - 11:24 AM
Post #2


D.I.C Head

**
Joined: 14 Oct, 2007
Posts: 73

Try using Text::CSV
I wrote a parser for a bunch of csv files recently and it worked perfect.


my $csv = Text::CSV->new();

for each line
$csv->parse($line);
@columns = $csv->fields ;
and refer to it as
@columns[0] .. etc . It should fill in an array element even when there is no value, so you can just compare the first and second lines using the array index
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

numberwhun
post 25 Apr, 2008 - 11:30 AM
Post #3


New D.I.C Head

*
Joined: 28 Mar, 2008
Posts: 6

QUOTE(mocker @ 25 Apr, 2008 - 11:24 AM) *

Try using Text::CSV
I wrote a parser for a bunch of csv files recently and it worked perfect.


my $csv = Text::CSV->new();

for each line
$csv->parse($line);
@columns = $csv->fields ;
and refer to it as
@columns[0] .. etc . It should fill in an array element even when there is no value, so you can just compare the first and second lines using the array index


I will try it and let you know how it goes. Thanks for the advice!!

Regards,

Jeff

User is offlineProfile CardPM
Go to the top of the page
+Quote Post

KevinADC
post 25 Apr, 2008 - 12:17 PM
Post #4


D.I.C Head

Group Icon
Joined: 23 Jan, 2007
Posts: 168

the hash is not necessary at all if this is just sort of for debugging. You will also lose the order of the columns if that is a concern. Use split with -1 as the limit, I think that preserves blank fields :

CODE
@array = split (/,/$foo,-1);

then:

CODE
for (0..$#array1) {
   print "$array1[$_] = $array[$_]\n"
}


where @array1 is the headers and @array is the other line. This has the advantage also of keeping the original order of the columns.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

numberwhun
post 25 Apr, 2008 - 12:21 PM
Post #5


New D.I.C Head

*
Joined: 28 Mar, 2008
Posts: 6

QUOTE(KevinADC @ 25 Apr, 2008 - 12:17 PM) *

the hash is not necessary at all if this is just sort of for debugging. You will also lose the order of the columns if that is a concern. Use split with -1 as the limit, I think that preserves blank fields :

CODE
@array = split (/,/$foo,-1);

then:

CODE
for (0..$#array1) {
   print "$array1[$_] = $array[$_]\n"
}


where @array1 is the headers and @array is the other line. This has the advantage also of keeping the original order of the columns.


I will have to give this a try as the Text::CVS is failing to install due to Compress::Raw::Zlib being one version behind and IT won't update either.

You are correct though, I would like to keep the order as it makes life easier. I will try this and get it working. Thanks Kevin!!!

Regards,

Jeff

User is offlineProfile CardPM
Go to the top of the page
+Quote Post

KevinADC
post 25 Apr, 2008 - 02:54 PM
Post #6


D.I.C Head

Group Icon
Joined: 23 Jan, 2007
Posts: 168

Jeff,

read the split function manpage too if you have not done so recently. The way you call split affects its behavior, the documentati0n explains it pretty well.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

numberwhun
post 25 Apr, 2008 - 05:12 PM
Post #7


New D.I.C Head

*
Joined: 28 Mar, 2008
Posts: 6

QUOTE(KevinADC @ 25 Apr, 2008 - 02:54 PM) *

Jeff,

read the split function manpage too if you have not done so recently. The way you call split affects its behavior, the documentati0n explains it pretty well.


I had skimmed through it, obviously a bit fast to catch the finer point that you mentioned. I have already been going through it since, thanks!!!

I think I have to go through and count the number of fields in the values file as it is 5 short. It looks like it goes off a bit at one point. It may not be the data but instead something I did when producing it.

Thanks again for your help Kevin!! Mucho appreciated!

Regards,

Jeff

User is offlineProfile CardPM
Go to the top of the page
+Quote Post

KevinADC
post 25 Apr, 2008 - 06:12 PM
Post #8


D.I.C Head

Group Icon
Joined: 23 Jan, 2007
Posts: 168

You're welcome Jeff, hope you get it all figured out soon. If not, let me know where to apply for you job. wink2.gif
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/16/08 10:09AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month