(jcwren) Re: hash loop print
by jcwren (Prior) on Oct 06, 2000 at 05:11 UTC
|
First, this reeks of a homework assignment.
Second, there is insufficient information. A hash has two components, a key and a value. Do you want the key and the value to be the same? Do you want to print the value portion of the hash sorted by the keys? Or perhaps vice versa?
Have you read the FAQ about posting, where it explains about making a question clear?
--Chris
e-mail jcwren | [reply] |
|
|
it is not a homework assignment. This is something I wanted to do.
To see if I could do it.
Yes I want the key and the value to be the same, no it doesn't need to be sorted.
No I have not read the FAQ about posting, where it explains making a question clear.
Sorry didn't know there was one.
I will read it, sorry for the misunderstanding.
| [reply] |
|
|
| [reply] |
|
|
(Dermot) Re: hash loop print
by Dermot (Scribe) on Oct 06, 2000 at 17:42 UTC
|
#!/usr/bin/perl -w
for (1 .. 10)
{
$numbers{$_} = $_ . "\n";
}
for (1 .. 10)
{
print $numbers{$_};
}
| [reply] [d/l] |
|
|
This time I want to do a hash referenced by an array, Would
it be something like this...it is not right yet.
for (1..10){
push@{$numbers{$_}}, $found10; }
for (1..10) {
print $numbers{$_}; }
| [reply] |
Re: hash loop print
by Fastolfe (Vicar) on Oct 06, 2000 at 19:19 UTC
|
This is an extremely basic Perl question. Not that I mind occasionally helping somebody out if there's some aspect of Perl they're having problems with, but quite frankly, if you're wanting to learn the basics, I'd suggest you find an online tutorial or buy a book such as Learning Perl or Programming Perl. Examining existing Perl code can also help if you learn by example.
In addition, read some of the stock Perl documentation (as well as the perlfaq and the Perl Monks FAQ).
I do wish you luck, though. | [reply] |
RE: hash loop print
by KM (Priest) on Oct 06, 2000 at 18:53 UTC
|
%hash = map { $_ => $_ } (1..10);
print qq{\$hash{$_} => $_\n} for sort {$a <=> $b} keys %hash;
Cheers,
KM | [reply] [d/l] |
Re: hash loop print
by kelsey (Initiate) on Oct 06, 2000 at 19:35 UTC
|
This time I want to do a hash referenced by an array,
Would it be something like this...it is not right yet.
for(1..10){ push@{$numbers{$_}}, $found10; }
for (1..10) { print $numbers{$_}; } | [reply] |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Re: hash loop print
by kelsey (Initiate) on Oct 06, 2000 at 20:24 UTC
|
I went and read my book
just like you said I should:
for basc perl
my (%numbers);
push @{$numbers{found} }, "1", "2", "3", "4", "5", "6", "7", "8", "9", "10";
for (0 .. 9) {
print $numbers{found}$_;
} | [reply] |
|
|
my (%numbers);
push @{$numbers{found} }, "1", "2", "3", "4", "5", "6", "7", "8",
+"9", "10";
for (0 .. 9) {
print $numbers{found}[$_]. "\n";
}
| [reply] [d/l] |
Re: hash loop print
by kelsey (Initiate) on Oct 06, 2000 at 05:10 UTC
|
Seekers of Perl Wisdom I am looking for an answer to this problem of printing out a hash from 1 to 10
I read this in a book push(@{$hash{"KEYNAME"} }, "new value"); To use references to arrays as the hash values
Then, deference the value as an array reference when printing out the hash
foreach $string {keys %hash) {
print $string: @{hash{$string)}\n
I would like to do thiis but I don't understand.
| [reply] |
|
|
The answer is !
push(@{ $hash{"KEYNAME"} },"1,2,3,4,5,6,7,8,9,10");
foreach $string (keys %hash) {
print "string: @{$hash{$string}} \n;
}
| [reply] |