Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > [newbie] how to get just the first value of a hash??

Reply
Thread Tools

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

 
 
Anno Siegel
Guest
Posts: n/a
 
      09-03-2003
Keith Keller <kkeller-> wrote in comp.lang.perl.misc:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 2003-09-03, Uri Guttman <> wrote:
> >
> > 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''.


No, it won't. It does hash magic and retrieves the number of keys
directly. It also resets each-iteration.

Anno
 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      09-03-2003
>>>>> "KK" == Keith Keller <kkeller-> writes:

KK> On 2003-09-03, Uri Guttman <> wrote:
>>
>> it is called keys.


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

from doop.c:

if (! SvTIED_mg((SV*)keys, 'P'))
i = HvKEYS(keys);
else {
i = 0;
/*SUPPRESS 560*/
while (hv_iternext(keys)) i++;
}

and HvKEYS is a macro that gets the key count from the hv. so it looks
like it is O(1)

uri

--
Uri Guttman ------ -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Damian Conway Class in Boston - Sept 2003 -- http://www.stemsystems.com/class
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      09-04-2003
Guest1 wrote:
[see subject]

Hashes are mappings and by definition don't have an order, therefore there
is no first element or first value.
Or how would you select the first value of let's say the cubicroot function?

jue


 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      09-04-2003
Brian Harnish wrote:
> 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.


If it would be a sig then probably nobody would complain because smart
Newsreaders will remove the sig automatically in replies.
Unfortunately it is not marked a sig, so it is body content, and will be
copied automatically, and requires manual effort to be removed.

Sucks IMHO

jue


 
Reply With Quote
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      09-04-2003
Also sprach Jürgen Exner:

> Guest1 wrote:
> [see subject]
>
> Hashes are mappings and by definition don't have an order, therefore there
> is no first element or first value.
> Or how would you select the first value of let's say the cubicroot function?


Well, the first one is probably 0 (at least for real numbers). The
challenge is to find the second value since it has a non-discrete
carrier.

Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
 
Reply With Quote
 
Dave Saville
Guest
Posts: n/a
 
      09-04-2003
On Wed, 03 Sep 2003 12:41:13 +0200, Janek Schleicher wrote:

>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.


What I thought the OP meant was something I had wondered myself. If you
have a function/module/whatever that returns a hash and you happen to
*know* that there will only be one value in it, is there an easy way to
get that single value?

Regards

Dave Saville

NB switch saville for nospam in address


 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      09-04-2003
Dave Saville <> wrote in comp.lang.perl.misc:
> On Wed, 03 Sep 2003 12:41:13 +0200, Janek Schleicher wrote:
>
> >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.

>
> What I thought the OP meant was something I had wondered myself. If you
> have a function/module/whatever that returns a hash and you happen to
> *know* that there will only be one value in it, is there an easy way to
> get that single value?


Sure:

my ( $value) = values %hash;

Anno
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      09-04-2003
Tassilo v. Parseval wrote:
> Also sprach Jürgen Exner:
>
>> Guest1 wrote:
>> [see subject]
>>
>> Hashes are mappings and by definition don't have an order, therefore
>> there is no first element or first value.
>> Or how would you select the first value of let's say the cubicroot
>> function?

>
> Well, the first one is probably 0 (at least for real numbers). The
> challenge is to find the second value since it has a non-discrete
> carrier.


You fell for the trap
For squareroot(x) you are probably right.
But I was talking about cubicroot and I would think that -5 comes before 0.

You could also argue that in case of squareroot i comes before 0. But there
are too many people who don't know complex numbers, therefore it makes a
poor example.

jue


 
Reply With Quote
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      09-04-2003
Also sprach Jürgen Exner:

> Tassilo v. Parseval wrote:
>> Also sprach Jürgen Exner:
>>
>>> Guest1 wrote:
>>> [see subject]
>>>
>>> Hashes are mappings and by definition don't have an order, therefore
>>> there is no first element or first value.
>>> Or how would you select the first value of let's say the cubicroot
>>> function?

>>
>> Well, the first one is probably 0 (at least for real numbers). The
>> challenge is to find the second value since it has a non-discrete
>> carrier.

>
> You fell for the trap
> For squareroot(x) you are probably right.
> But I was talking about cubicroot and I would think that -5 comes before 0.


Oups, right. I somehow failed to see that the cubicroot is x^(1/3) and
not x^(1/2).

> You could also argue that in case of squareroot i comes before 0. But there
> are too many people who don't know complex numbers, therefore it makes a
> poor example.


Well, i would be part of the result anyway (when taking the squareroot
of a negative number). If you define the range of your result to be
within the real numbers, the smallest input that is allowed is 0.

But indeed, finding the first number is even harder for complex numbers
since you suddenly have two dimensions to consider.

Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Open file, get first line, delete first line close file Richard Schneeman Ruby 16 08-26-2008 11:54 PM
how common is it to get email spam with your first, first and last and/or home town in it? Lookout Computer Support 16 04-27-2006 07:42 AM
All my drop down lists get reset to the first value when I go in to edit a record in the datagrid.... Josh Behl ASP .Net Datagrid Control 3 10-29-2003 05:38 PM
ASP.NET - DropDownList Get's First Select value Micheal ASP .Net 1 07-30-2003 08:10 PM



Advertisments