Help Creating a Hash from a CSV file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    Help Creating a Hash from a CSV file

    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,,,,,,,val ue,,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=perl]
    se strict;
    use warnings;

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

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

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

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

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

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

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

    Regards,

    Jeff
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    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.

    Regards,

    Jeff

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      see DIC

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Originally posted by KevinADC
        see DIC
        Well, I know what you mean, but that could SOOO be misconstrued on here. :-)

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          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! ;)

          Comment

          • eWish
            Recognized Expert Contributor
            • Jul 2007
            • 973

            #6
            Although the opportunity is great, I am going to pass on this one. )

            --Kevin

            Comment

            Working...