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

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; }
An example conf file...
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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.