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 ?:)
In reply to split line and populate hash shortcut ? by leocharre
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |