leocharre has asked for the wisdom of the Perl Monks concerning the following question:
i got a neat question..
i'm reading a conf file, splitting it up into key val pairs ti put into a hash. this sub is some'pin like..
An example conf file...sub conf2hash { my %argv = @_; =pod usage: conf2hash( file=>'path/2/file', hashref=> \%yourhash, splitchar=>' +=', commentchar=> '#'); if splitchar is null, default is = sign if commentchar is null, default is # mark # one key val pair per line # returns 0 on errors =cut defined $argv{splitchar} or $argv{splitchar}='='; defined $argv{hashref} or return 0; defined $argv{commentchar} or $argv{commentchar}='#'; defined $argv{file} or return 0; open (IN, "< $argv{file}") or die($!); while (<IN>) { if ( $_=~m/^$argv{commentchar}|^\s+$/ ) { next;} $_=~s/\s//g; my @a = split(/$argv{splitchar}/,$_); ${$argv{hashref}}{$a[0]}=$a[1]; } close IN; return 1; }
THIS=that name=joe height=heyman #this is a comment age=14
Here's my question... where i read in the line and then split it..
my @a = split(/$argv{splitchar}/,$_); #and then, place it into the hash.... ${$argv{hashref}}{$a[0]}=$a[1];
isn't there some other way to do this like...
split(/$argv{splitchar}/,$_); ${$argv{hashref}}{$_[0]}=$_[1]; # is that ok ?? # and what about some thing like.. # (and I DONT KNOW the syntax, that's why im here!!) ( ${$argv{hashref}}{$_} = $_ )= split(/$argv{splitchar}/,$_); #is that totally making it up ?
any code shortcuts ?:)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: split line and populate hash shortcut ?
by BrowserUk (Patriarch) on Feb 14, 2006 at 17:31 UTC | |
by traveler (Parson) on Feb 14, 2006 at 18:21 UTC | |
by blokhead (Monsignor) on Feb 14, 2006 at 19:39 UTC | |
by leocharre (Priest) on Feb 14, 2006 at 18:10 UTC | |
|
Re: split line and populate hash shortcut ?
by salva (Canon) on Feb 14, 2006 at 17:25 UTC | |
by BrowserUk (Patriarch) on Feb 14, 2006 at 17:43 UTC | |
by salva (Canon) on Feb 14, 2006 at 17:51 UTC | |
|
Re: split line and populate hash shortcut ?
by Praveen (Friar) on Feb 20, 2006 at 15:36 UTC | |
|
Re: split line and populate hash shortcut ?
by revdiablo (Prior) on Feb 15, 2006 at 17:16 UTC |