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":



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