[newbie] how to get just the first value of a hash??

  • Thread starter Janek Schleicher
  • Start date
J

Janek Schleicher

Guest1 wrote at Wed, 03 Sep 2003 14:52:22 +0100:

You can't.
Hashs are unsorted, so there is no first key or value.


Greetings,
Janek
 
J

Janek Schleicher

Guest1 wrote at Wed, 03 Sep 2003 14:52:22 +0100:

If you want to loop over the values of a hash,
you can either use the list of all values with

my $value = (values %hash)[0];

or iterate through with

while (my ($key, $value) = each %hash) {
# do something with them
}

However, the returned value is in general not the same as the first value
you've written into the hash.


Greetings,
Janek
 
B

Bigus

There is no "first value of a hash". That is, a hash just consists of keys
and values. If you want the lowest value in a hash, assuming the values are
all numbers of course, then you could do it something like this:

@keys = sort {$hash{$a} <=> $hash{$b}} keys %hash;
print $hash{$keys[0]};

Bigus
 
M

Mina Naguib

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Guest1 wrote:

In the future, please post your question in the body of your message, not just the subject.
Thanks in advance
Pedro

Unlike arrays, hashes are not an ordered list, therefore there's no concept of "first".

Think of a hash as a way to describe someone's face for example. One property is eye colour,
another is hair length, etc. None of the properties are first or last or more important than the other.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/VfVieS99pGMif6wRAlP1AJ9hD06J8DtMctfiJWNjpD1HlJt9xwCgyFGx
jmMWbG/iGJMYyI/BXxfkkLw=
=+nrC
-----END PGP SIGNATURE-----
 
G

Gunter Schelfhout

Mina said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Guest1 wrote:

In the future, please post your question in the body of your message, not
just the subject.


Unlike arrays, hashes are not an ordered list, therefore there's no
concept of "first".

Think of a hash as a way to describe someone's face for example. One
property is eye colour,
another is hair length, etc. None of the properties are first or last or
more important than the other.
Ack.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/VfVieS99pGMif6wRAlP1AJ9hD06J8DtMctfiJWNjpD1HlJt9xwCgyFGx
jmMWbG/iGJMYyI/BXxfkkLw=
=+nrC
-----END PGP SIGNATURE-----

Is this PGP-sig really nescessary on usenet? It's ugly like hell. And a lot
of work if you want to remove it on reply.
 
G

Gunter Schelfhout

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mina Naguib wrote: [snip...]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/VfVieS99pGMif6wRAlP1AJ9hD06J8DtMctfiJWNjpD1HlJt9xwCgyFGx
jmMWbG/iGJMYyI/BXxfkkLw=
=+nrC
-----END PGP SIGNATURE-----

Is this PGP-sig really nescessary on usenet? It's ugly like hell. And a lot
of work if you want to remove it on reply.

Yes, it is nescessary, it's the only way of telling who really sent the
message. And, the signatures are roughly the acceptable size for usenet
..sig's.

- Brian Harnish ([email protected])
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/Vf9RiK/rA3tCpFYRAnLaAJ9v8G2m44TI61KxVoCjmLwW6AOsugCgmzSv
BDUAWRPfYJqdoTUfOVdhEw0=
=oNJC
-----END PGP SIGNATURE-----
 
G

Gunter Schelfhout

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Gunter said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mina Naguib wrote: [snip...]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQE/VfVieS99pGMif6wRAlP1AJ9hD06J8DtMctfiJWNjpD1HlJt9xwCgyFGx
jmMWbG/iGJMYyI/BXxfkkLw=
=+nrC
-----END PGP SIGNATURE-----

Is this PGP-sig really nescessary on usenet? It's ugly like hell. And a
lot of work if you want to remove it on reply.
blabla.

Yes, it is nescessary, it's the only way of telling who really sent the
message. And, the signatures are roughly the acceptable size for usenet
.sig's.
blabla

- Brian Harnish ([email protected])
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/Vf9RiK/rA3tCpFYRAnLaAJ9v8G2m44TI61KxVoCjmLwW6AOsugCgmzSv
BDUAWRPfYJqdoTUfOVdhEw0=
=oNJC
-----END PGP SIGNATURE-----

I know where it is used for. IMHO, it doesn't make sense to use it on
usenet.
Look at the mess after only a couple of messages.
- --
Blood, sweat & tears
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)

iD8DBQE/VgUrtGqwJj9zNs4RAtMEAJ4mgu8utdi1uG7bFQKw8GdhfnekQACdGwfT
T4DXvc3eAxdt89zOXZlK6lc=
=yj94
-----END PGP SIGNATURE-----
 
G

Guest1

I have a hash of arrays, and I would like to get the length of any of
these arrays. I said first instead to say any of them (because all the
arrays have the same length), More clear my question now?

On the future I will try to be more clear from the begining.

Thanks
P
 
U

Uri Guttman

<don't top post>

G> I have a hash of arrays, and I would like to get the length of any of
G> these arrays. I said first instead to say any of them (because all the
G> arrays have the same length), More clear my question now?

perldoc -f each

G> On the future I will try to be more clear from the begining.

that is always helpful

uri
 
G

Gunnar Hjalmarsson

Guest1 said:
I have a hash of arrays, and I would like to get the length of any
of these arrays. I said first instead to say any of them (because
all the arrays have the same length), More clear my question now?

Suppose so. Assuming that you by "length" mean the number of elements,
this is one way:

print scalar @{$myhash{(keys %myhash)[0]}};
On the future I will try to be more clear from the begining.

Good. Also, in the future, please don't top post.
 
J

John Bokma

Bigus said:
There is no "first value of a hash". That is, a hash just consists of keys
and values. If you want the lowest value in a hash, assuming the values are
all numbers of course, then you could do it something like this:

@keys = sort {$hash{$a} <=> $hash{$b}} keys %hash;
print $hash{$keys[0]};

technically this is O(n log n) while just looping over all keys and
finding the lowest is O(n).

my @keys = keys %hash;
my $min = $hash{shift @keys}; # assign "first" as minimum
foreach my $key (@keys) {
$min = $hash{$key} if $hash{$key} < $min; # try to find better one
}

assuming %hash has values :)

HTH
 
B

Brian Harnish

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Gunter said:
Mina Naguib wrote: [snip...]

Is this PGP-sig really nescessary on usenet? It's ugly like hell. And a
lot of work if you want to remove it on reply.
blabla.

Yes, it is nescessary, it's the only way of telling who really sent the
message. And, the signatures are roughly the acceptable size for usenet
.sig's.
blabla

- Brian Harnish ([email protected])

I know where it is used for. IMHO, it doesn't make sense to use it on
usenet.
Look at the mess after only a couple of messages.
- --
Blood, sweat & tears

Well, we're all entitled to oppinions. I don't think it's difficult to
jump to the end of the message and delete a few lines when replying. And
the only reason it got so ugly in our quoting is because we quoted the
sigs, because thats what we were discussing. Ususally, you strip out any
text that isn't relevant to the conversation (such as pgpsigs). If someone
wants to verify that the quoted message came from the right person, they
can check the original message.

BTW: My news reader doesn't support pgp signing, so I copy/paste sign.
Takes an extra couple of seconds, but I find it worthy, so that people
know w/o a doubt that a message came from me.

- Brian
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/VhPRiK/rA3tCpFYRAte+AKCX4RXVkmbuKExeYURUeKxMnAdntACfWfMm
bcxJnKy9QK4cRKQnUE86KeM=
=E618
-----END PGP SIGNATURE-----
 
G

Gunnar Hjalmarsson

Tina said:
Gunnar said:
Assuming that you by "length" mean the number of elements,
this is one way:

print scalar @{$myhash{(keys %myhash)[0]}};

or maybe one of these:
print scalar @{ (values %myhash)[0] };
print $#{ (values %myhash)[0] }+1;
print $#{ (each %myhash)[1] }+1;
print @{ (%myhash)[1] }+0;

Yes. TMTOWTDI. But I have to admit that all your suggestions are more
straight-forward than mine. I forgot about the values function. And
each... Oh, well. ;-)
 
G

Gunter Schelfhout

Brian said:
Well, we're all entitled to oppinions.
Certainly.

I don't think it's difficult to
jump to the end of the message and delete a few lines when replying.

And on top.
And
the only reason it got so ugly in our quoting is because we quoted the
sigs, because thats what we were discussing. Ususally, you strip out any
text that isn't relevant to the conversation (such as pgpsigs). If someone
wants to verify that the quoted message came from the right person, they
can check the original message.

OK. I don't have your public key. How should I check it? Via a key-server?
You used my name, but not my headers. These should be enough to check the
postings, no?
BTW: My news reader doesn't support pgp signing, so I copy/paste sign.
Takes an extra couple of seconds, but I find it worthy, so that people
know w/o a doubt that a message came from me.

Maybe you can put it after '-- ', so it will be cut by the readers which has
the option set to cut signatures?

Anyway, enough off-topic postings.
 
A

Anno Siegel

Tina Mueller said:
Suppose so. Assuming that you by "length" mean the number of elements,
this is one way:
print scalar @{$myhash{(keys %myhash)[0]}};

or maybe one of these:
print scalar @{ (values %myhash)[0] };
print $#{ (values %myhash)[0] }+1;
print $#{ (each %myhash)[1] }+1;
print @{ (%myhash)[1] }+0;

The "each"-solution is different from the others -- it doesn't expand
all of the hash, which is good. It also leaves the hash with a not-
quite-fresh iterator, which is bad. In a one-off program it probably
doesn't matter, but in a general-purpose routine it would. Where is
uneach?

Anno
 
U

Uri Guttman

AS> The "each"-solution is different from the others -- it doesn't expand
AS> all of the hash, which is good. It also leaves the hash with a not-
AS> quite-fresh iterator, which is bad. In a one-off program it probably
AS> doesn't matter, but in a general-purpose routine it would. Where is
AS> uneach?

it is called keys.

uri
 
A

Anno Siegel

Uri Guttman said:
AS> The "each"-solution is different from the others -- it doesn't expand
AS> all of the hash, which is good. It also leaves the hash with a not-
AS> quite-fresh iterator, which is bad. In a one-off program it probably
AS> doesn't matter, but in a general-purpose routine it would. Where is
AS> uneach?

it is called keys.

Yes and no. "keys" resets the iterator. The mythical uneach restores it,
so the next call to each returns what it would have before. A tied hash
could implement it.

Anno
 
K

Keith Keller

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

it is called keys.

In scalar context, will keys run through the whole hash? If so,
then it doesn't sound the same as Anno's suggested ''uneach''.

- --keith

- --
(e-mail address removed)-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAj9WVJYACgkQhVcNCxZ5ID9AQQCfUlN7rTA5utK0D2RJ/02BYHoi
ZOQAn3WLbI9/fx8/cexin2qQu2tcrSJ9
=ZXbe
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top