A better way to add to a hash?

U

usenet

Kindly consider this code which builds a hash from associated __DATA__
pairs:

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

my %setting;
foreach my $pair(<DATA>) {
$setting{$1} = $2 for ( $pair =~ m{(.*)=(.*)} ); # [yuck??]
}

print Dumper(\%setting); #yeah, it works, but...

__DATA__
user=root
password=easy
hostname=abcxyz

This works. But I'm not really happy with using a "for" loop like that
- it just doesn't seem like the best way to go (but I can't really
explain why; I just kinda linger there and stare at it when I mentally
parse the code).

Is there a better way to do this than using the "for" loop that way?

Thanks!
 
J

John W. Krahn

Kindly consider this code which builds a hash from associated __DATA__
pairs:

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

my %setting;
foreach my $pair(<DATA>) {
$setting{$1} = $2 for ( $pair =~ m{(.*)=(.*)} ); # [yuck??]
}

print Dumper(\%setting); #yeah, it works, but...

__DATA__
user=root
password=easy
hostname=abcxyz

This works. But I'm not really happy with using a "for" loop like that
- it just doesn't seem like the best way to go (but I can't really
explain why; I just kinda linger there and stare at it when I mentally
parse the code).

Is there a better way to do this than using the "for" loop that way?

Maybe not "better".

while ( <DATA> ) {
@setting{ /(.*)=/ } = /=(.*)/;
}


John
 
A

Anno Siegel

Kindly consider this code which builds a hash from associated __DATA__
pairs:

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

my %setting;
foreach my $pair(<DATA>) {
$setting{$1} = $2 for ( $pair =~ m{(.*)=(.*)} ); # [yuck??]
}

print Dumper(\%setting); #yeah, it works, but...

__DATA__
user=root
password=easy
hostname=abcxyz

This works. But I'm not really happy with using a "for" loop like that
- it just doesn't seem like the best way to go (but I can't really
explain why; I just kinda linger there and stare at it when I mentally
parse the code).

One problem is to get rid of the newlines.

my @setting = map split( /[=\n]), <DATA>;
or
my %setting = map split( /[=]/), map /(.*)/, <DATA>;;

Anno
 
X

Xicheng

Kindly consider this code which builds a hash from associated __DATA__
pairs:

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

my %setting;
foreach my $pair(<DATA>) {
$setting{$1} = $2 for ( $pair =~ m{(.*)=(.*)} ); # [yuck??]

$pair =~ m{(.*)=(.*)} and $setting{$1} = $2;
 
A

A. Sinan Unur

(e-mail address removed) wrote in @f14g2000cwb.googlegroups.com:
Kindly consider this code which builds a hash from associated __DATA__
pairs:

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

my %setting;
foreach my $pair(<DATA>) {

Well, first of all, I would stick with reading the file line-by-line
when processing is to be done line-by-line. I know it doesn't matter
much in this case, but it is still a good habit to get into.

$setting{$1} = $2 for ( $pair =~ m{(.*)=(.*)} ); # [yuck??]
}

Second, I am just too conservative I guess.

Also, I think the regex is too permissive. Are there really no
restrictions on the keys of the hash?

If the settings are being read from an external file, I would prefer to
initialize the hash with the keys that are acceptable, and only set
those.

Given the possibility of further restrictions on keys and/or values, I
would prefer the more boring, but easier to read (to me) and extend form
below.

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

my %setting = (
user => 'default_user',
password => undef,
hostname => 'default_host',
);

while(<DATA>) {
if( m{\A (\w+) \s* = \s* ([^\n]+) \n \z }xms ) {
if( exists $setting{$1} ) {
$setting{$1} = $2;
}
}
}

print Dumper(\%setting);

__DATA__
user = root
password = easy
hostname = abcxyz
empty =
bogus = ewhfljeh lksadfj sadkjf sadlkfj
 

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

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top