Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Create a hash by spliting string

by Utilitarian (Vicar)
on Feb 09, 2011 at 14:25 UTC ( [id://887213]=note: print w/replies, xml ) Need Help??


in reply to Create a hash by spliting string

You have the basics of it already. you can split on a regular expression, so a character class that included both colon ":" and semi-colon ";" would split your string into a list of 6 elements.

At this point, knowing that any list with an even number of elements can be assigned to a hash is important because that's how you'd complete the task, simply by assigning the output of the split to %hash

print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."

Replies are listed 'Best First'.
Re^2: Create a hash by spliting string
by sundialsvc4 (Abbot) on Feb 10, 2011 at 01:36 UTC

    Whereas I, being forever distrustful of data, would split the string on semicolons, and then, in a loop, split each piece separately.   And, my code would verify that each of these splits generated exactly two pieces, and it would croak if ever it did not.   So, this would not only accomplish the task of splitting the string, but also confirm that it is well-formed.

    Yup... guess I’m a Perl Dork.

      Hardly a dork, after all verifying your data is valid before throwing it into a structure is the correct thing to do. However I suspect our Anonymous friend isn't writing production code here

      print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://887213]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found