Re: chomp hash keys?
- From: Uri Guttman <uri@xxxxxxxxxxxxxxx>
- Date: Sat, 29 Apr 2006 13:03:06 -0400
"R" == Ruud <rvtol+news@xxxxxxxxxxxx> writes:
R> Good question this, and I like the answers too:
R> my %person = grep [ chomp ], <DATA>; (John W. Krahn)
R> my %person = map {chomp; $_} <DATA>; (Tad McClellan)
R> Maybe it should be included in a perlfaq: how to populate a hash from
R> linefeed-ed data.
R> my $sep = qr/ \s* : \s* /x;
R> my %person = map { chomp; split $sep } <DATA>;
R> print Dumper \%person;
i dunno why all this chomp and stuff is needed. do it the other way
around and grab the keys/values:
my %hash = map /(.+)$sep(.+)$/, <DATA> ;
in the File::Slurp package (in extras/slurp_article.pod or search
perl.com for it) i show an example of doing that for disk files too.
my $text = read_file( $file ) ;
my %config = $text =~ /^(\w+)=(.+)$/mg ;
you can easily tune the regex for the separator, whitespace, etc. you
don't even need the temp var:
my %config = read_file( $file ) =~ /^(\w+)=(.+)$/mg ;
nary a chomp in sight!
uri
--
Uri Guttman ------ uri@xxxxxxxxxxxxxxx -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
.
- Follow-Ups:
- Re: chomp hash keys?
- From: Dr.Ruud
- Re: chomp hash keys?
- References:
- chomp hash keys?
- From: usenet
- Re: chomp hash keys?
- From: Dr.Ruud
- chomp hash keys?
- Prev by Date: Re: random array elements and speed
- Next by Date: Multi-process Win32 HTTP Daemon
- Previous by thread: Re: chomp hash keys?
- Next by thread: Re: chomp hash keys?
- Index(es):
Relevant Pages
|