Next time, please embed your code in CODE tags, that will enlighten your post considerably ;-). Read the Site how to while you're at it.

You already seem to have a firm grasp of regexs. You can read a bit more in perlre, Death to Dot Star! and 7 Stages of Regex Users. Look also at the links in regex tutorial, and of course at YAPE-Regex-Explain-1.01 released!. What you want, is simply an insertion of \s* around the '=' to allow any amount of whitespace to be read in.

... $line =~ /^([^=]+)\s*=\s*(.*)/;

Furthermore, I would add a chomp $line; at the beginning of your while loop to remove newlines from your hash. Other issues: You don't check for the occurrence of a comment-line; If you only can have one '=', you'd better stick with split. That all results in:

while( <> ){ next if /^#/; #check for comment next unless /=/; #check if = present chomp; #remove newline split /\s*=\s*/, $_, 2; #split line to @_ $values{ $_[0] } = $_[1]; #assign to hash }
I used $_, it gets automagically assinged to by the while. Removes a bit of the code's complexity.

Cheers,

Jeroen
"We are not alone"(FZ)


In reply to Re: Read file and remove whitespace by jeroenes
in thread Read file and remove whitespace by grim

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.