Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

print out keys of hash with carriage returns

by bengmau (Beadle)
on Nov 03, 2004 at 18:43 UTC ( #404965=perlquestion: print w/ replies, xml ) Need Help??
bengmau has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm trying to print out the keys of my hash into a file with (1 key per line) I'm sure there is a way to do this with one line but my brain is frozen.. Someone want to refresh my brain? print RPTOUT (keys %tapeid),"\n"; ??

Comment on print out keys of hash with carriage returns
Re: print out keys of hash with carriage returns
by sth (Priest) on Nov 03, 2004 at 18:46 UTC

    print RPTOUT join( "\n", keys %tapeid)

      that's it.. the light bulb has turned back on
Re: print out keys of hash with carriage returns
by Zaxo (Archbishop) on Nov 03, 2004 at 18:50 UTC

    One way is to set the output record field seperator,

    { local $, = "\n"; print RPTOUT keys(%tapeid), "\n"; }
    You could instead call join "\n", . . . on the keys.

    Update: sth++, corrected.

    After Compline,
    Zaxo

      ...one minor correction, $, is output field serperator, $\ is output record serpperator.

Re: print out keys of hash with carriage returns
by pg (Canon) on Nov 03, 2004 at 18:50 UTC

    Or

    print FILE "$_\n" for (keys %hash);
Re: print out keys of hash with carriage returns
by fletcher_the_dog (Friar) on Nov 03, 2004 at 19:19 UTC
    Another variation
    map{print RPTOUT "$_\n"} keys %tapeid;

      That is indeed another way to do the same thing, but many argue against using map in void context. In older versions of perl, it was inefficient, because it would build a list of return values. In newer versions of perl, it's just bad style.

Re: print out keys of hash with carriage returns
by superfrink (Curate) on Nov 03, 2004 at 19:39 UTC
    I always do this when I just want to see what's in a hash:
    use Data::Dumper; print Dumper(\%hash); print Dumper($hashref);
    Don't forget the backslash so that a reference gets passed to the function.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://404965]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2014-12-28 08:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (179 votes), past polls