Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Loading Hash From FileHandle - Cannot Chomp Input Keys

by roho (Chancellor)
on Dec 22, 2004 at 01:47 UTC ( #416674=perlquestion: print w/replies, xml ) Need Help??

roho has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to load a set of hash keys directly from an open filehandle. The only problem is I cannot remove the newline characters from the data lines that create the hash keys. I can do it in two steps by first reading the filehandle into an array and chomping it, but I would like to avoid the overhead of making an array just to get the hash keys loaded. The following code shows both attempts (with one or the other commented out). Note: I am using DATA for this example. Actual code will read from a file. Any ideas on how I can get the first attempt to remove newlines?

Thanks in advance,
roho

#!/usr/bin/perl -w use strict; ###################################################################### +###### # First attempt - BAD - Load hash from file handle - keys contain newl +ines. ###################################################################### +###### #my %person = map { $_ => 1 } <DATA>; ###################################################################### +###### # Second attempt - GOOD - Load hash from chomped array - no newlines. ###################################################################### +###### chomp(my @array = <DATA>); my %person = map { $_ => 1 } @array; for (keys %person) { print "$_ = $person{$_}\n"; } __DATA__ Ruth Samuel Paul John Silas

"Its not how hard you work, its how much you get done."

Replies are listed 'Best First'.
Re: Loading Hash From FileHandle - Cannot Chomp Input Keys
by bmann (Priest) on Dec 22, 2004 at 02:16 UTC
    map can take a code block as the first argument, so

    my %person = map { chomp; $_ => 1 } <DATA>;
    would work.

      Thanks to all for your suggestions. They all work fine. I am using bmann's, since it is the closest to my original attempt.
      Thanks again, roho.

      "Its not how hard you work, its how much you get done."

Re: Loading Hash From FileHandle - Cannot Chomp Input Keys
by saskaqueer (Friar) on Dec 22, 2004 at 01:57 UTC

    Untested, should work fine. Not really sure if the defined() test is really necessary, but I figured I'd slip it in.

    #!perl -w use strict; my %person = map { m!^(.*)$!; defined($1) and length($1) ? ($1 => 1) : () } <DATA>; __DATA__ Ruth Samuel Paul John Silas

    quick update: a simple loop would also work of course:

    #!perl -w use strict; my %person; while ( chomp($_ = <DATA>) ) { $person{$_} = 1; } __DATA__ Ruth Samuel Paul John Silas
Re: Loading Hash From FileHandle - Cannot Chomp Input Keys
by johnnywang (Priest) on Dec 22, 2004 at 03:00 UTC
    or:
    use strict; my %person; map{chomp; $person{$_} = 1} <DATA>; print join(",", keys %person); __DATA__ Ruth Samuel Paul John Silas
      While map in void context is no longer inefficient compared to a simple for loop, I still feel that map has a different meaning and should not be used in void context. It may be as efficient as a for loop to the machine, but I feel, not as efficient for the reader.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2020-02-24 00:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (104 votes). Check out past polls.

    Notices?