Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

It is usually considered bad form to modify $_ during a map{} operation. People expect map{} to create a new list from an existing list, without modifying the existing list.

The expression can be written out clearer, more accurately, and using less intermediate memory by using the following code instead:

my %hash; for (@array) { if (/^([^,]+)(?:,(.*))?$/) { $hash{$1} = $2; } }

This also has the side effect of handling the case where the input takes the form "1009," or "1009". For "1009," the key will map to an empty string. For "1009" the key will map to the undefined value.

The regular expression removes the need for chomp() as the $ at the end of the regular expression will not swallow the "\n".

If efficiency is the true goal, the following code has the exact same effect:

my %hash; /^([^,]+)(?:,(.*))?$/s && ($hash{$1} = $2) for @array;

If a few CPU ticks can be spared, the former is far more preferable to the latter.

In reply to Re: Re: Re: Re: Hash references and illegal octal digits by MarkM
in thread Hash references and illegal octal digits by peppiv

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2021-06-16 02:30 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (74 votes). Check out past polls.