Re: hash-Zufall

Michael Wenger <[email protected]>
Newsgroups gmane.linux.suse.programming
Message-ID <[email protected]>
Hi Daniel!

Daniel Feist schrieb am 16.01.2005 20:32 :
>
> Am besten wäre s, wenn ich der 2. Funktion die erste irgendwie "geben"
> könnte, also sprich mit deren Werten weiterarbeiten kann.
> Wie macht man das dann?

Z.B. so:

#!/usr/bin/perl -w

use strict;
use warnings;

&main();
exit(0);

sub main {
	my %hash = (
		"Fred"   => "Feuerstein",
		"Barney" => "Geroellheimer",
	);

	for(my $i = 0; $i < 20; $i++) {
		print $i.": ".&getRandHashElement(\%hash)."\n";
	}
}

sub getRandHashElement {
	my ($hash_ptr) = @_;

	my @hash_keys = keys(%$hash_ptr);
	my $rnd_key = $hash_keys[rand(@hash_keys)];

	return $hash_ptr->{$rnd_key};
}

hth,
Michael
signature.asc (application/pgp-signature, 546 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iQEVAwUBQerHFnGeOfdBLQhTAQJ0sAf8C/Cl9wdzaQqN7KUNNHbQWgqRbRTNtGUi
z3oWcVZR7C2aia8AStGfqWqFxhGrrHEWHB6FuCATSaghs7dH7NpNXRo0jsOzuA9k
L5g6lzDTz7mNcpbBrDkRBNA/TWuqEAlVZagTWHy8LjW0HO8WMOxv9U2ctpJ49Y14
QRQYleyCi1l0F4pRCLJgX9N1+FaNZ9s4ELMAXZMyHfYeKEnYWGMXPVrXTsyMIDC+
MYaYNYUxG8kOBYRyyjLGss54wz6TJZ3cLuaVkLFE+6Qpnjg+vG35/XKEgBEmEb8p
X9oGNrln9WehbeETwmc9AZHwxGHKChcT4Mof+gy0rgfCnErNVK+dWA==
=KiXL
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.