Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

G'day mrras25,

Firstly, there's two issues with how you generate $content.

The individual strings have double-quotes which interpolate and will cause problems with embedded '$' or '@' characters: use single-quotes instead to avoid this.

Any $content .= '...' line where the string starts with a digit will leave the start of that line indistinguishable from the value at the end of the previous line. Consider a situation where $content .= '3PA/Game 16.1 ...' follows $content .= '... Opp 3PM/Game 4.8': $content now contains '... Opp 3PM/Game 4.83PA/Game 16.1 ...'. A way around this is to get rid of every $content .= and just use a single join with a space: my $content = join ' ', '...', '...', '...';.

Back to your question. This code will do what you want with the data you've shown here:

my $re = qr{\s*(.+?)\s+(--|[+-]?\d+[.]\d+)}; my (%stats, @stat_order); while ($content =~ /$re/g) { push @stat_order, $1; $stats{$1} = $2; }

Here's a complete script with all your input showing full output:

Update: I made a small change to the part of the regex matching a potential leading sign: s{[+-]*}{[+-]?}

-- Ken


In reply to Re: Words and Numbers to hash by kcott
in thread Words and Numbers to hash by mrras25

Title:
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 How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found