Re: chomp hash keys?



Uri Guttman schreef:
Ruud:

my $sep = qr/ \s* : \s* /x;
my %person = map { chomp; split $sep } <DATA>;

my %hash = map /(.+)$sep(.+)$/, <DATA>
[...]
my %config = read_file( $file ) =~ /^(\w+)=(.+)$/mg ;

nary a chomp in sight!


OK, let me rewrite that with map.

#!/usr/bin/perl
use strict;
use warnings;

my %config = map /^(\w+)=(.+)$/, <DATA>;

print "keys: @{[keys %config]}\n";

while ((my $key, my $value) = each %config) {
print "$key\t=> $value\n";
}
__DATA__
#comment-1
[block]
#comment-2=1+1
ones=11111
twos=222
example=1+2=3
ones=1


Viva that TIMTOWTDI.

--
Affijn, Ruud

"Gewoon is een tijger."


.



Relevant Pages

  • Re: Printing text between two blank lines
    ... Aren't you using strictures and warnings?? ... I'd use a foreach loop instead of map in void context. ... strict and warnings, ... blah blah weather, sports ...
    (perl.beginners)
  • Learning how to map a drive in Perl
    ... I am looking at how I could connect to share and map a local drive in a ... perl script. ... use strict; ... use warnings; ...
    (perl.beginners)
  • need help on map function
    ... Can anyone tell me why the map will not map out the unitialized ... variable in this code with 'str'? ... use strict; ... use warnings; ...
    (perl.beginners)
  • Re: Sharing variables between modules
    ... use strict; ... use warnings; ... # Note that this must also come *before* the use Foo line. ... package Foo; ...
    (comp.lang.perl.misc)
  • Re: Dynamic directory handles?
    ... > 1) You should always enable warnings (and strict) when ... > perldoc -q filehandle ... >> sub dircount { ... > use warnings; ...
    (comp.lang.perl.misc)