Re: chomp hash keys?
- From: "Dr.Ruud" <rvtol+news@xxxxxxxxxxxx>
- Date: Sat, 29 Apr 2006 11:22:12 +0200
usenet@xxxxxxxxxxxxxxx schreef:
#!/usr/bin/perl
use strict; use warnings;
use Data::Dumper;
chomp (my %person = (<DATA>));
print Dumper \%person;
__DATA__
name
fred
occupation
quarryman
friend
barney
The keys, of course, have linefeeds on the end, which I do not want.
What is the best way to populate my hash with linefeed-ed data? Must
I build a 2-at-a-time loop and populate one pair per iteration? That
seems like a crummy solution!
Good question this, and I like the answers too:
my %person = grep [ chomp ], <DATA>; (John W. Krahn)
my %person = map {chomp; $_} <DATA>; (Tad McClellan)
Maybe it should be included in a perlfaq: how to populate a hash from
linefeed-ed data.
With a different separator:
#!/usr/bin/perl
use strict; use warnings;
use Data::Dumper;
my $sep = qr/ \s* : \s* /x;
my %person = map { chomp; split $sep } <DATA>;
print Dumper \%person;
__DATA__
name : fred
occupation : quarryman
friend : barney
--
Affijn, Ruud
"Gewoon is een tijger."
.
- Follow-Ups:
- Re: chomp hash keys?
- From: Uri Guttman
- Re: chomp hash keys?
- References:
- chomp hash keys?
- From: usenet
- chomp hash keys?
- Prev by Date: Re: Using =~ to Include and Omit in same line
- Next by Date: using variable in search and replace
- Previous by thread: Re: chomp hash keys?
- Next by thread: Re: chomp hash keys?
- Index(es):