Re: Filling a hash
by Skeeve (Parson) on Oct 26, 2005 at 07:04 UTC
|
In the sense of "having fun with homework questions" how about this? M-)
perl -MData::Dumper -ne '/^exit$/i&&die"Ok. I\x27m done".Dumper(\%h);chomp;$h{$_}=$_'
s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
| [reply] [d/l] [select] |
|
|
Here is another simple solution. Inputs are taken 2 per line, separated by ^Q for clarity's sake, ignoring malformed input.
until (defined($flag) and defined($flag) == $flag) {
($_=substr(<STDIN>,0,-1)) !~ /\A^\be{1,1}\B(?=x(?=i(?=t)))\Bx{1,1}
+\B(?=i(?=t))i(?=t)\Bt{1,1}\b\Z\z$/
and ((index($_,"\x11") == -1) or ($_{ substr($_,0,index($_,"\x
+11")) } = substr($_,index($_,"\x11")+1)))
or ($flag = print map {$_.$".$_{$_}.$/} keys %_)
}
| [reply] [d/l] |
Re: Filling a hash
by inman (Curate) on Oct 26, 2005 at 09:00 UTC
|
I need to generate a hash.
You need to declare the hash first if you want extra marks.
The program asks the user to enter elements
You will have to collect user input. This can come from a file, the keyboard or other device. read about <STDIN> for more details.
until they enter 'exit' which stops the loop.
you need to check the input for the word 'exit'. If you are only covering hashes at this stage then you will want to use a string equality operator
Then each hash is printed to the screen.
This is up to you but look at the keys function. This gives you a list of the keys in the hash which you can use to iterate through the hash printing the values one at a time.
The function 'push' is not allowed.
I guess you covered this in the last lesson and the teacher wants you to do something different...
| [reply] |
Re: Filling a hash
by sauoq (Abbot) on Oct 26, 2005 at 06:50 UTC
|
Most of us have already done our homework.
Do you have a specific problem with code you've written?
-sauoq
"My two cents aren't worth a dime.";
| [reply] |
Re: Filling a hash
by polettix (Vicar) on Oct 26, 2005 at 12:44 UTC
|
Considerations in random order.
The function 'push' is not allowed.
This is a strange requirement for two reasons: first , because we should be free of using push wherever we think it's good to; second, how I can add an element to a hash using the push function in an intuitive way? Nothing comes to my mind.
Then each hash is printed to the screen.
Either your teacher missed something, or you failed to copy the input of your homework. I thought we were talking about one hash, not many. Anyway, if you want to print hash contents you could find Data::Dumper interesting.
Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf
Don't fool yourself.
| [reply] [d/l] |
|
|
Actually, I use push to put items in a hash all the time. Of course, it's because I want a HoA so I can detect and deal with duplicates. One of the places I use this I then go through the HoA at the end, verify that all arrays have a single member, and then convert from HoA to straight H. I suppose I could do something like this:
@h{keys %h} = map { $_->[0] } values %h;
but I think I do this in a slightly longer piece of code.In another location, I keep the arrays - I need to keep duplicate entries. Think of an XML file - each element can have multiple subelements. In my case, order between elements of different names doesn't matter, so I can put each name into a hash, and have an array of elements that all have that name as a child of the current element. (Does that even make sense?)
So, yes, I use push to populate hashes. It's not the usual way, but it's not uncommon.
(All actual examples of how I do it are removed so as not to give the OP any silly ideas to cut&paste...) | [reply] [d/l] |
|
|
Uh, I was thinking about pushing both key and values, but yes, your solution is a viable one using push.
All actual examples of how I do it are removed so as not to give the OP any silly ideas to cut&paste...
Given the fact that the OP cannot use push, an example would be safe here :)
Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf
Don't fool yourself.
| [reply] [d/l] |
|
|
| [reply] |
|
|
{
my @ary = %hash;
push @ary, $k => $v;
%hash = @ary;
}
I suppose. But it doesn't seem much natural :) I only wondered why the teacher had to specify this strange requirement.
Update: corrected bug in snippet, thanks to QM
Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf
Don't fool yourself.
| [reply] [d/l] |
Re: Filling a hash
by planetscape (Chancellor) on Oct 26, 2005 at 06:56 UTC
|
| [reply] |