Re: chomp hash keys?



"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
.



Relevant Pages

  • chomp hash keys?
    ... According to perldoc -f chomp: ... 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? ...
    (comp.lang.perl.misc)
  • Re: chomp hash keys?
    ... 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? ...
    (comp.lang.perl.misc)
  • Re: Perl code for comparing two files
    ... Hash: SHA1 ... chomp $line; ... or die "could not open $second_file: ... foreach my $key (sort keys ...
    (perl.beginners)
  • Re: purging rows from fileA.txt based on fileB.txt
    ... read A line by line and check for existence in the hash. ... Also I am not sure how the memory would handle 13million lines of about ... chomp $banWord; ... we will also need some sample input from both input files if we are to ...
    (perl.beginners)
  • Re: purging rows from fileA.txt based on fileB.txt
    ... relational database. ... read A line by line and check for existence in the hash. ... Also I am not sure how the memory would handle 13million lines of about ... chomp $banWord; ...
    (perl.beginners)