Howdy Monks,

So I've been bejiggering with this code for awhile and it's not working out the way I need it to. I am parsing a text file where the first line consists of headings that I would like to use for keys in a hash. The next few lines contain values. I'd like to store each line of values in a hash that is keyed by the headings in the first line, and contains the values in the second line. However, I'd like to store each of those hashes in a master hash that is keyed by the first value in each row. So the data I am looking at resembles the following:

Heading 1 Heading 2 Heading 3 ... Value A1 Value A2 Value A3 ... Value B1 Value B2 Value B3 ... . . .

I am trying to parse that code with the following subroutine (please note values are delimited by tabs, so the parsing is working as expected)

sub get_fields{ my $local_filein = shift; # Input file needs to be the first fi +le passed to get_fields my %tmp_hash; my %tickets; my @keys; my @values; while(my $line = <$local_filein>){ chomp $line; if ($line =~ m/^\D/){ @keys = split(/\t/, $line); }elsif($line =~ m/d/){ @values = split(/\t/, $line); @tmp_hash{@keys} = @values; # foreach(keys %tmp_hash){ # print "KEY: $_ VALUE: $tmp_hash{$_}\n" # } $tickets { $values[0] } = %tmp_hash; #$tickets{ @values[0]{@keys}} = @values; #$tickets{ $values[0] } = { each %tmp_hash }; # Each is a + piece of shit. Use @ context or something else. } } #@tickets{@keys} = @values; return %tickets; }

The line of code I am having trouble with is the one where I am trying to assign the values of %tmp_hash into the master hash %tickets. As you can see, I've tried a few different things which I've commented out. Basically, %tmp_hash is getting created just fine by assigning it the arrays @keys and @values. But I'd like to do something similar to make %tickets the hash of temporary hashes keyed by the first value in the @values array. I can't seem to get the context to work out.

Any help y'all could provide would be appreciated.

Thanks!
Brady


In reply to Storing a Hash Made of Two Arrays in Another Hash by BJ_Covert_Action

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.