Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Getiing keys or values of a file based hash

by kcott (Archbishop)
on Feb 28, 2020 at 07:50 UTC ( [id://11113534]=note: print w/replies, xml ) Need Help??


in reply to Getiing keys or values of a file based hash

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

  • take more care when posting input data,
  • post the actual code you are running, and
  • provide us with the exact output you get (even if that's only error or warning messages).

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11113534]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2024-04-25 15:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found