Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

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

This question seems to be popping up all over the forum here, and people are making it harder than it has to be.

Perl knows how to turn a flat list into a hash. For example, you can say the following (and it will work):

my %hash = ( 1 => 'one', 2 => 'two', 3 => 'three', );

And since the => operator is just a special kind of comma (oversimplification), the code above is pretty much the same as this:

my %hash = ( '1', 'one', '2', 'two', '3', 'three', );

And that is exactly the same as this:

my %hash = ( '1', 'one', '2', 'two', '3', 'three' );

And wouldn't you know it... that works too, as it should.

By now the light bulb really ought to be switching on. You can assign a flat list to a hash. Hmm, I wonder how we can convert the string presented by the original poster to a flat list...

split still comes in handy. But the fact is, it's the only tool you need, and you need it only once. The regexp you used has the defect of using capturing parenthesis, which in the context of a split is often undesirable (unless you really do want to keep the delimiters). You could have used non-capturing parens, as in /(?:;|:)/, but you actually don't need to constrain your alternation at all; there's nothing beyond the simple alternation of /;|:/ But for single-character alternation I prefer just using a character class as in /[;:]/. However, it turns out that in the OP's example string, all of the characters he wants to keep are "word" characters (\w), which means all his delimiters are non-word characters (\W). So the split expression could be further simplified to /\W/. Splitting on non-words will give us a flat list that looks like qw/1 one 2 two 3 three/.

Putting it all together, the line that does all the work will look like this:

%hash = split /\W/, $string;

No need for grep, no need for nested splits, no need for map... just toss out the delimiters and you've got a flat list which can be assigned to a hash.


Dave


In reply to Re^3: Create a hash by spliting string by davido
in thread Create a hash by spliting string by Anonymous Monk

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 musing on the Monastery: (3)
As of 2024-04-18 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found