G'day shabird,

Welcome to the Monastery.

You've posted your input data twice. Working past that, is it really double-spaced as you've shown? The output from your 'print $line,"\n";' would have been useful for us; however, you've commented that out.

There's another bigger problem. You said "its not printing anything". The code you posted would have printed something like:

Global symbol "@keys" requires explicit package name ... Execution ... aborted due to compilation errors.

For future reference, please

If you had made a mistake by trying to split on non-existent colons, you still would have got the keys but the values would have been all undef. Here's a quick example:

$ perl -e ' my $x = "MIR200B miRNA"; my %h; my ($k, $v) = split /:/, $x; $h{$k} = $v; use Data::Dumper; print Dumper(\%h); ' $VAR1 = { 'MIR200B miRNA' => undef };

Given there's so many question marks over the data and the code, it's difficult to advise you. The following may be close to what you were trying achieve; or, at least, provide some insight into how you might proceed.

#!/usr/bin/env perl use strict; use warnings; my %data; while (<DATA>) { next if $. == 1; chomp; next unless length; my ($k, $v) = split; $data{$k} = $v; } # To test result use Data::Dumper; print "*** Hash ***\n"; print Dumper(\%data); print "*** Keys ***\n"; print "$_\n" for keys %data; print "*** Values ***\n"; print "$_\n" for values %data; __DATA__ GeneName GeneType APOL4 protein_coding CYP2C8 protein_coding NAALADL2 protein_coding NANOS3 protein_coding C20orf204 protein_coding MIR429 miRNA MIR200A miRNA MIR200B miRNA

Output:

*** Hash *** $VAR1 = { 'NANOS3' => 'protein_coding', 'C20orf204' => 'protein_coding', 'APOL4' => 'protein_coding', 'MIR429' => 'miRNA', 'NAALADL2' => 'protein_coding', 'MIR200B' => 'miRNA', 'MIR200A' => 'miRNA', 'CYP2C8' => 'protein_coding' }; *** Keys *** NANOS3 C20orf204 APOL4 MIR429 NAALADL2 MIR200B MIR200A CYP2C8 *** Values *** protein_coding protein_coding protein_coding miRNA protein_coding miRNA miRNA protein_coding

— Ken


In reply to Re: Getiing keys or values of a file based hash by kcott
in thread Getiing keys or values of a file based hash by shabird

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.