in reply to Create a hash by spliting string

Use the magical split_and_store_in_hash function!

What, your version of Perl doesn't have it? In that case you will have to start writing it yourself or look into other functions. It may be that it is named a bit different.

Come back here once you have shown some effort and we will gladly help you.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Replies are listed 'Best First'.
Re^2: Create a hash by spliting string
by arivu198314 (Sexton) on Feb 10, 2011 at 06:35 UTC
    try the below code,
    $string="1:one;2:two;3:three"; %hash=grep(/\w+/, split(/(;|:)/, $string));
    Arivu

      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

        This question seems to be popping up all over the forum here, and people are making it harder than it has to be.
        Maybe because it's obvious this is homework and the students have apparently put zero thought into it?

        Elda Taluta; Sarks Sark; Ark Arks