pysome has asked for the wisdom of the Perl Monks concerning the following question:
I wanna change $h to a hash like this:my $h= { (4,-1), (2,6), (6,4), (3,5), (5,-1), (99,-1), };
That's to say, i wanna join them in <key,value> order,the pre-element's value is post-element's key,until the key is -1$hh{2}{6}{4} = -1; $hh{3}{5} = -1; $hh{99} = -1;
The keys is unique ,but the "-1" is not. How to fulfill it? Thanks
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: How to get a ideal hash
by BrowserUk (Patriarch) on Apr 03, 2009 at 13:08 UTC | |
This could probably be simplified a little:
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
Re: How to get a ideal hash
by derby (Abbot) on Apr 03, 2009 at 13:14 UTC | |
I've often seen this for hierarchical data -- like you have flat hash (sql results) for some type of hierarchical data. There's probably a more efficient way of doing this but basically you want to chain the keys together until you hit some base criteria. Here's a first cut attempt (using references because i find it easier): produces:
-derby
| [reply] [d/l] [select] |
Re: How to get a ideal hash
by ELISHEVA (Prior) on Apr 03, 2009 at 14:44 UTC | |
In your post (OP) you specify that all numbers other than "-1" are "unique" - but what exactly do you mean? Do you mean that The two statements are quite different. Unless you are absolutely certain a priori that (2) is true, your algorithm is going to need to check for multiple paths to each number. Then you will have to decide whether those multiple paths are an error or not. Assuming that multiple paths to each number are an error, your code will need to keep track of each path found to each number. I posted code below to illustrate the point, mainly as a contrast to BrowserUk's elegant solution. I do so with reservations. Bloodnok is right - it is considered bad form on Perl Monks to post a question without at least trying to show your own efforts. But BrowserUK is right too - the OP has posted a difficult problem. We aren't trying to be mean when we say do it yourself: it is just that we are donating our time so others can learn. Trying to solve the problem on your own is essential to really understanding the solution. For further discussion of the point, see The path to mastery. Read more... (2 kB)
Best, beth Update: added my agreement with BrowserUk's observation about difficulty below. | [reply] [d/l] [select] |
by BrowserUk (Patriarch) on Apr 03, 2009 at 15:04 UTC | |
I do so with reservations. Bloodnok is right - it is considered bad form on Perl Monks to post a question without at least trying to show your own efforts. For simple problems I might agree, but this is decidely non-trivial. Should people refrain from asking if they have a difficult problem and have no idea how to start to solve it? Personally, I think this is an interesting problem, clearly defined and it deserved an answer. I also find that many of the "what have you tried" missives are self-serving. Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] |
by dwm042 (Priest) on Apr 03, 2009 at 16:26 UTC | |
I've answered questions others regarded as simple, trivial, or homework simply because I found the problem to be interesting. And who is to say what any individual finds interesting? Mikhail Tal, for one, delighted in chess problems designed for children. | [reply] |
by Argel (Prior) on Apr 03, 2009 at 21:12 UTC | |
Elda Taluta; Sarks Sark; Ark Arks | [reply] |
by pysome (Scribe) on Apr 07, 2009 at 02:36 UTC | |
by Argel (Prior) on Apr 09, 2009 at 21:39 UTC | |
Re: How to get a ideal hash
by Bloodnok (Vicar) on Apr 03, 2009 at 11:16 UTC | |
As ELISHEVA is probably tired of saying, this isn't a code writing service - try explaining what you've done and what doesn't seem to work and you might then get some constructive answers.
A user level that continues to overstate my experience :-))
| [reply] |
by Anonymous Monk on Apr 03, 2009 at 11:27 UTC | |
| [reply] [d/l] |
by Bloodnok (Vicar) on Apr 03, 2009 at 11:56 UTC | |
FWIW, I've solved it thusly: gives: As, I think, required Update: Added my attempt at a solution.
A user level that continues to overstate my experience :-))
| [reply] [d/l] [select] |
Re: How to get a ideal hash
by eric256 (Parson) on Apr 03, 2009 at 21:22 UTC | |
As always there is more than one way to do it. I see chains as arrays, so i built arrays first, then looped those to make hashes. Of coures as I wrote this and pasted I realized that this method depends on the right order, so I'm gonna make a second shot, but here is this version for fun:
___________ Eric Hodges | [reply] [d/l] |
by eric256 (Parson) on Apr 03, 2009 at 21:32 UTC | |
Okay this version finds the ends of the paths, then builds them backwards from their. As long as the data is good it will run fine ;) I added the pair (1,2) which breaks the above code.
___________ Eric Hodges | [reply] [d/l] |
Re: How to get a ideal hash
by ig (Vicar) on Apr 05, 2009 at 02:06 UTC | |
Here is yet another solution. This one has a single loop and no recursion.
| [reply] [d/l] |
Re: How to get a ideal hash
by rir (Vicar) on Apr 08, 2009 at 18:53 UTC | |
rir
| [reply] [d/l] |