chomp hash keys?

U

usenet

According to perldoc -f chomp:keys.

Is there an easy way to "overcome" this? Kindly consider this code
which illustrates my motivation for asking:

#!/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!

Thanks!
 
U

usenet

build a 2-at-a-time loop and populate one pair per iteration? That

I suppose I could do something like this:

chomp (my @person = (<DATA>));
my %person = @person;

But that still seems crummy. I use two data structures that contain the
same data.

There's gotta be a better way!
 
J

John W. Krahn

According to perldoc -f chomp:
keys.

Is there an easy way to "overcome" this? Kindly consider this code
which illustrates my motivation for asking:

#!/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!

my %person = grep [ chomp ], <DATA>;



John
 
K

Keith Keller

According to perldoc -f chomp:
keys.

Is there an easy way to "overcome" this? Kindly consider this code
which illustrates my motivation for asking:

#!/usr/bin/perl
use strict; use warnings;
use Data::Dumper;

chomp (my %person = (<DATA>));
print Dumper \%person;

__DATA__
name
fred
occupation
quarryman
friend
barney

TIMTOWTDI, of course, but one option is to assign <DATA> to an array,
then chomp it, then assign the array to a hash. It's a waste of an
array, so if your file is large, this might not work so well. This
seems to work okay:

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

use Data::Dumper;

chomp (my @person = (<DATA>));
my %person=@person;
print Dumper \%person;

__DATA__
name
fred
occupation
quarryman
friend
barney


--keith
 
T

Tad McClellan

According to perldoc -f chomp:
keys.

Is there an easy way to "overcome" this?


You can't change a key in the context of a hash, because the
key is the index.

You can create a new element with a newlineless key and delete
the element with the newlined key.

chomp (my %person = (<DATA>));
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?


my %person = map {chomp; $_} <DATA>;
 
D

Dr.Ruud

(e-mail address removed) 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
 
M

Michele Dondi

my %person = grep [ chomp ], <DATA>;

Thanks, John - that is much better. I keep forgetting how useful grep()
can be!

But that is somewhat an abuse of grep() -although a very smart one!-,
since it's not really selecting elements from the input list. It
rather exploits a side effect. I would write

my %person = map { chomp; $_ } <DATA>;

instead, although it is not just as compact...


Michele
 
U

Uri Guttman

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
 
D

Dr.Ruud

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top