[31999] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3263 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Jan 21 21:09:29 2011

Date: Fri, 21 Jan 2011 18:09:11 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Fri, 21 Jan 2011     Volume: 11 Number: 3263

Today's topics:
    Re: HTML purifier for Perl <sherm.pendley@gmail.com>
    Re: HTML purifier for Perl <paul@pstech-inc.com>
    Re: Learning Object Oriented Programming (newbie) <rodbass63@gmail.com>
    Re: Learning Object Oriented Programming (newbie) <sherm.pendley@gmail.com>
    Re: Learning Object Oriented Programming (newbie) <rodbass63@gmail.com>
    Re: Learning Object Oriented Programming (newbie) <sherm.pendley@gmail.com>
    Re: Learning Object Oriented Programming (newbie) <hjp-usenet2@hjp.at>
    Re: Memory limit question sln@netherlands.com
    Re: Memory limit question <hjp-usenet2@hjp.at>
        what is the cheapest way to just initialize a hash key? jidanni@jidanni.org
    Re: what is the cheapest way to just initialize a hash  <bugbear@trim_papermule.co.uk_trim>
    Re: what is the cheapest way to just initialize a hash  <peter@makholm.net>
    Re: what is the cheapest way to just initialize a hash  (David Canzi)
    Re: what is the cheapest way to just initialize a hash  <cartercc@gmail.com>
    Re: what is the cheapest way to just initialize a hash  <kst-u@mib.org>
    Re: what is the cheapest way to just initialize a hash  <jurgenex@hotmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Fri, 21 Jan 2011 11:20:11 -0500
From: Sherm Pendley <sherm.pendley@gmail.com>
Subject: Re: HTML purifier for Perl
Message-Id: <m2wrlycjpg.fsf@sherm.shermpendley.com>

"P E Schoen" <paul@pstech-inc.com> writes:

> I could not find a Perl script to
> accomplish the same thing

How hard did you look? I went to <http://search.cpan.org>, typed in HTML,
and in about three minutes I found HTML::Declaw on the second page of
results:

 <http://search.cpan.org/perldoc?HTML::Declaw>

Perl generally doesn't rely on separate, self-contained scripts to do
such things. Instead, HTML::Declaw is a module that one can use in one's
own script.

sherm--

-- 
Sherm Pendley
                                   <http://camelbones.sourceforge.net>
Cocoa Developer


------------------------------

Date: Fri, 21 Jan 2011 13:50:53 -0500
From: "P E Schoen" <paul@pstech-inc.com>
Subject: Re: HTML purifier for Perl
Message-Id: <nAk_o.44$OY2.19@newsfe11.iad>

"Sherm Pendley"  wrote in message=20
news:m2wrlycjpg.fsf@sherm.shermpendley.com...

> "P E Schoen" <paul@pstech-inc.com> writes:

>> I could not find a Perl script to
>> accomplish the same thing

> How hard did you look? I went to <http://search.cpan.org>, typed
> in HTML, and in about three minutes I found HTML::Declaw on the
> second page of results:

     <http://search.cpan.org/perldoc?HTML::Declaw>

> Perl generally doesn't rely on separate, self-contained scripts to
> do such things. Instead, HTML::Declaw is a module that one can
> use in one's own script.

I admit that I just did a quick Dogpile search rather than cpan. By that =

time I already had some of the HTMLpurifier system working, and I was=20
impressed by the quick response from the support forum and the =
references=20
from various prestigious sites that are using it. Also, I am not very=20
proficient in Perl (or PHP or even HTML for that matter), so I really =
needed=20
a simple example from which I could build my application. And it was a =
good=20
learning experience to become familiar with at least a little basic PHP, =
and=20
ways to interface between PHP and Perl.

Thanks for the information.

Paul=20



------------------------------

Date: Fri, 21 Jan 2011 05:52:32 -0800 (PST)
From: Nene <rodbass63@gmail.com>
Subject: Re: Learning Object Oriented Programming (newbie)
Message-Id: <22b931b6-16ef-44f4-9939-5da4d75ca245@k22g2000yqh.googlegroups.com>

On Jan 20, 11:22=A0pm, "Uri Guttman" <u...@StemSystems.com> wrote:
> >>>>> "N" =3D=3D Nene =A0<rodbas...@gmail.com> writes:
>
> =A0 N> sub print_variables {
> =A0 N> print "Do something with $learn_oop->{farm} and with $learn_oop-
> =A0 >> {port}";
> =A0 N> }
>
> others pointed out you didn't declare $learn_oop and you didn't assign
> it the object coming in from @_.


Mr. Schwartz is correct (thanks), I didn't declare $learn_oop.


>
> =A0 N> $test->print_varibles();
>
> but that line will fail as well at runtime. the method here isn't
> spelled the same as the one you created in the module.

Thank you, I see the typo, I fixed it and this simple OOP program not
works as expected.

>
> uri
>
 I have another question. The $farm and $port belong to an environment
that I want to do something with down the road in the print_variable
method. But I have multiple kinds of $farm and $port variables that I
want to do something with at the same time.

Presently, I have $farm =3D WEB, and port =3D 1234. But I also have a
$farm =3D "ABC", port =3D "2345, and $farm =3D "XYZ" and $port =3D "9876" a=
nd
I want to apply those to the print_variables method as well. Can
somebody point me in the right direction?


> --
> Uri Guttman =A0------ =A0u...@stemsystems.com =A0-------- =A0http://www.s=
ysarch.com--
> ----- =A0Perl Code Review , Architecture, Development, Training, Support =
------
> --------- =A0Gourmet Hot Cocoa Mix =A0---- =A0http://bestfriendscocoa.com=
---------



------------------------------

Date: Fri, 21 Jan 2011 11:30:35 -0500
From: Sherm Pendley <sherm.pendley@gmail.com>
Subject: Re: Learning Object Oriented Programming (newbie)
Message-Id: <m2sjwmcj84.fsf@sherm.shermpendley.com>

Nene <rodbass63@gmail.com> writes:

> Presently, I have $farm = WEB, and port = 1234. But I also have a
> $farm = "ABC", port = "2345, and $farm = "XYZ" and $port = "9876" and
> I want to apply those to the print_variables method as well. Can
> somebody point me in the right direction?

You could store your data as a hash called "farms," and read/write it
like so:

sub setPortForFarm {
    my ($self, $port, $farm) = @_;
    $self->{'farms'}->{$farm} = $port;
}

sub portForFarm {
    my ($self, $farm) = @_;
    return $self->{'farms'}->{$farm};
}

You'd call the above with:

  my $farms = Farms->new();
  $farms->setPortForFarm(1234, 'WEB');
  $farms->setPortForFarm(8675309, 'Jenny');

For more about hashes of hashes, lists of lists, etc., see:

  perldoc perlreftut
  perldoc perldsc
  perldoc perllol

sherm--

-- 
Sherm Pendley
                                   <http://camelbones.sourceforge.net>
Cocoa Developer


------------------------------

Date: Fri, 21 Jan 2011 14:58:57 -0800 (PST)
From: Nene <rodbass63@gmail.com>
Subject: Re: Learning Object Oriented Programming (newbie)
Message-Id: <a9b4c9c8-f2ff-409d-b1f4-c8e19caa1d6a@w2g2000yqb.googlegroups.com>

On Jan 21, 11:30=A0am, Sherm Pendley <sherm.pend...@gmail.com> wrote:
> Nene <rodbas...@gmail.com> writes:
> > Presently, I have $farm =3D WEB, and port =3D 1234. But I also have a
> > $farm =3D "ABC", port =3D "2345, and $farm =3D "XYZ" and $port =3D "987=
6" and
> > I want to apply those to the print_variables method as well. Can
> > somebody point me in the right direction?
>
> You could store your data as a hash called "farms," and read/write it
> like so:
>
> sub setPortForFarm {
> =A0 =A0 my ($self, $port, $farm) =3D @_;
> =A0 =A0 $self->{'farms'}->{$farm} =3D $port;
>
> }
>
> sub portForFarm {
> =A0 =A0 my ($self, $farm) =3D @_;
> =A0 =A0 return $self->{'farms'}->{$farm};
>
> }
>
> You'd call the above with:
>
> =A0 my $farms =3D Farms->new();
> =A0 $farms->setPortForFarm(1234, 'WEB');
> =A0 $farms->setPortForFarm(8675309, 'Jenny');
>
> For more about hashes of hashes, lists of lists, etc., see:
>
> =A0 perldoc perlreftut
> =A0 perldoc perldsc
> =A0 perldoc perllol
>
> sherm--
>
> --
> Sherm Pendley
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0<h=
ttp://camelbones.sourceforge.net>
> Cocoa Developer

Thanks Sherm.

I changed my .pm file and inputted what you suggested above:

sub setPortForFarm {
     my ($self, $port, $farm) =3D @_;
     $self->{'farms'}->{$farm} =3D $port;

 }

 sub portForFarm {
     my ($self, $farm) =3D @_;
     return $self->{'farms'}->{$farm};

In my perl script when I called the method portForFarm, it failed.
This is how I called it. I'm assuming since the
portForFarm had the return statement, it will print the data.
my $farms =3D Learn->new();
$farms->setPortForFarm(1234, 'WEB');
$farms->setPortForFarm(8675309, 'Jenny');
$farms->portForFarm();

exit 0;

I'm getting this error:
Use of uninitialized value $farm in hash element at /usr/lib/perl/5.10/
Learn.pm line 53.

Line 53 had this    ....   return $self->{'farms'}->{$farm};


------------------------------

Date: Fri, 21 Jan 2011 18:25:02 -0500
From: Sherm Pendley <sherm.pendley@gmail.com>
Subject: Re: Learning Object Oriented Programming (newbie)
Message-Id: <m28vyddelt.fsf@sherm.shermpendley.com>

Nene <rodbass63@gmail.com> writes:

> On Jan 21, 11:30 am, Sherm Pendley <sherm.pend...@gmail.com> wrote:
>> Nene <rodbas...@gmail.com> writes:
>> > Presently, I have $farm = WEB, and port = 1234. But I also have a
>> > $farm = "ABC", port = "2345, and $farm = "XYZ" and $port = "9876" and
>> > I want to apply those to the print_variables method as well. Can
>> > somebody point me in the right direction?
>>
>> You could store your data as a hash called "farms," and read/write it
>> like so:
>>
>> sub setPortForFarm {
>>     my ($self, $port, $farm) = @_;
>>     $self->{'farms'}->{$farm} = $port;
>>
>> }
>>
>> sub portForFarm {
>>     my ($self, $farm) = @_;
>>     return $self->{'farms'}->{$farm};
>>
>> }
>>
>> You'd call the above with:
>>
>>   my $farms = Farms->new();
>>   $farms->setPortForFarm(1234, 'WEB');
>>   $farms->setPortForFarm(8675309, 'Jenny');
>>
>> For more about hashes of hashes, lists of lists, etc., see:
>>
>>   perldoc perlreftut
>>   perldoc perldsc
>>   perldoc perllol
>
> Thanks Sherm.
>
> I changed my .pm file and inputted what you suggested above:
>
> sub setPortForFarm {
>      my ($self, $port, $farm) = @_;
>      $self->{'farms'}->{$farm} = $port;
>
>  }
>
>  sub portForFarm {
>      my ($self, $farm) = @_;
>      return $self->{'farms'}->{$farm};
>
> In my perl script when I called the method portForFarm, it failed.

Of course - you didn't pass an argument to it.

  my $port = $farms->portForFarm('Jenny');

> This is how I called it. I'm assuming since the
> portForFarm had the return statement, it will print the data.

Why would you assume that? "print" ne "return". The latter returns a
value to the caller. So, after doing the above:

  print "$port\n";

Will print "8675309".

> I'm getting this error:
> Use of uninitialized value $farm in hash element at /usr/lib/perl/5.10/
> Learn.pm line 53.
>
> Line 53 had this    ....   return $self->{'farms'}->{$farm};

Right - $farm is uninitialized, because you didn't pass a value for the
argument when you wrote "$farms->portForFarm()".

sherm--

-- 
Sherm Pendley
                                   <http://camelbones.sourceforge.net>
Cocoa Developer


------------------------------

Date: Sat, 22 Jan 2011 00:39:12 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Learning Object Oriented Programming (newbie)
Message-Id: <slrnijk690.gee.hjp-usenet2@hrunkner.hjp.at>

On 2011-01-21 13:52, Nene <rodbass63@gmail.com> wrote:
>  I have another question. The $farm and $port belong to an environment
> that I want to do something with down the road in the print_variable
> method. But I have multiple kinds of $farm and $port variables that I
> want to do something with at the same time.
>
> Presently, I have $farm = WEB, and port = 1234. But I also have a
> $farm = "ABC", port = "2345, and $farm = "XYZ" and $port = "9876" and
> I want to apply those to the print_variables method as well. Can
> somebody point me in the right direction?

I'm not sure whether I understand you correctly. Part of the problem is
that you named your class "Learn", and I have a hard time figuring out
what kind of thing a "Learn" might be which has a "farm" and a "port". 

I am assuming that you want to have many "Learn" thingies, each of which
has exactly one farm and one port. Then your class is already ok and you
can just create many "Learn" objects with your new method:

    my $learn_web = Learn->new();
    $learn_web->set_farm('WEB');
    $learn_web->set_port(1234);

    my $learn_abc = Learn->new();
    $learn_abc->set_farm('ABC');
    $learn_abc->set_port(2345);

    my $learn_xyz = Learn->new();
    $learn_xyz->set_farm('AXYZ);
    $learn_xyz->set_port(9876);

    $learn_web->print_variables();
    $learn_abc->print_variables();
    $learn_xyz->print_variables();


or (almost equivalent):

    my @learn_thingies;
    my $learn_thing;

    $learn_thing = Learn->new();
    $learn_thing->set_farm('WEB');
    $learn_thing->set_port(1234);
    push @learn_thingies, @learn_thing;

    $learn_thing = Learn->new();
    $learn_thing->set_farm('ABC');
    $learn_thing->set_port(2345);
    push @learn_thingies, @learn_thing;

    $learn_thing = Learn->new();
    $learn_thing->set_farm('AXYZ);
    $learn_thing->set_port(9876);
    push @learn_thingies, @learn_thing;

    $_->print_variables() for (@learn_thingies);

	hp


------------------------------

Date: Fri, 21 Jan 2011 08:26:31 -0800
From: sln@netherlands.com
Subject: Re: Memory limit question
Message-Id: <4qajj6h0r9qrq0rpeqgu0fch3nbr65gv2u@4ax.com>

On Thu, 20 Jan 2011 12:26:12 -0600, "E.D.G." <edgrsprj@ix.netcom.com> wrote:

>"Peter J. Holzer" <hjp-usenet2@hjp.at> wrote in message 
>news:slrnijc4ih.k4u.hjp-usenet2@hrunkner.hjp.at...
>> On 2011-01-18 00:27, E.D.G. <edgrsprj@ix.netcom.com> wrote:
>>> The ActiveState version of Perl is being used with Windows XP and Vista 
>>> on
>>> standard PC computers.
>>>
>>> QUESTION:  Does Perl have a limit on how much memory it can use with 
>>> Windows
>>> XP and Vista?
>>
>> 32 bit or 64 bit Windows?
>
>> 32 Bit Windows has a 2 GB limit per process.
>
>Thanks for the comments.
>
>I am presently using 32 bit windows.  The new machine will likely have the 
>64 bit system.
>
>It sounds as if it would probably be impossible to change some Windows 
>parameter so that a larger size program could run.  So, for the moment I am 
>going to plan on having to run multiple programs if the present Vista 
>computer is used.  If the 64 bit system will run larger programs then 
>perhaps a single program can be used on the new system.
>
>As it is somewhat uncertain regarding what results will be obtained here 
>some additional checking will be done before the new computer is built.

Some of it is coming back to me now.

To start, virtual address space for XP/Vista 32-bit is 4 gig.
The OS claims the high 'half' of that, ie: 2 gigs. This leaves
2 gig of virtual space for user mode.
So 'user mode (2gig)' + 'kernel mode (2gig)' = total virtual address space (4gig).

However, this has nothing to do with physicall ram per se.

The way windows switches threads/processes is via a context switch,
which loads segment registers of the task in question. There is no swapping
of physical ram unless its absolutely necessary.

So, if Windows had access to all 4 gig's of physical ram (with modern cpu's and
motherboard IOMMU/hardware virtualization it can be done),
then it can use it, but not all of it in a single process.

The process consist of the kernel and user mode portions in the 4 gig
address space. The kernel never gets switched, only the user space gets
switched.

Both the kernel and application use physical ram. The kernel NEVER uses more
than 1 gig of physical ram, and rarely uses more than 512 megs at most.
The kernel can also page its physical ram to disk as needed.

So basically that leaves about at least 3 gigs of physical ram for ALL
applications combined, with the limit of each process (because of virtual user/kernel
enforced space partition of 2 gig's per) being 2 gig of physical ram.

So your application can allocate 2 gigs of physical ram right out of the gate.
The next app can allocate 1.5 gigs. Both of the apps will exist in physical ram
at the same time. You can start another app, allocate 2 gigs and the first app
will be swapped out to the page file. Now olny the second and third app will exist
in physical ram. And on and on..

The big thing is how do you map graphic cards and such into that 4 gig virtual
address space without cutting into the 4 gig of physical ram the apps and kernel
want to use. The answer is its done with virtualization hardware on the motherboard
and special cpu instructions. This appears to be implemented after 2008 with
AMD's > Opteron and Intel's cpu changes (don't know the name VT something?)

Bottom line, each process can use only 2 gig of physical ram, although
multiple process can be resident where the combined usage equals (for the most
part) the entire 4 gigs of physical ram (in theory). Its still motherboard/cpu/bios
dependent however.

End of mildly unsure rant..

-sln



------------------------------

Date: Sat, 22 Jan 2011 00:21:39 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Memory limit question
Message-Id: <slrnijk584.gee.hjp-usenet2@hrunkner.hjp.at>

On 2011-01-21 16:26, sln@netherlands.com <sln@netherlands.com> wrote:
> On Thu, 20 Jan 2011 12:26:12 -0600, "E.D.G." <edgrsprj@ix.netcom.com> wrote:
>
>>"Peter J. Holzer" <hjp-usenet2@hjp.at> wrote in message 
>>news:slrnijc4ih.k4u.hjp-usenet2@hrunkner.hjp.at...
>>> On 2011-01-18 00:27, E.D.G. <edgrsprj@ix.netcom.com> wrote:
>>>> The ActiveState version of Perl is being used with Windows XP and
>>>> Vista on standard PC computers.
>>>>
>>>> QUESTION:  Does Perl have a limit on how much memory it can use
>>>> with Windows XP and Vista?
>>>
>>> 32 bit or 64 bit Windows?
>>
>>> 32 Bit Windows has a 2 GB limit per process.
>>
[...]
> To start, virtual address space for XP/Vista 32-bit is 4 gig.
> The OS claims the high 'half' of that, ie: 2 gigs. This leaves
> 2 gig of virtual space for user mode.
> So 'user mode (2gig)' + 'kernel mode (2gig)' = total virtual address space (4gig).

Correct.


> So, if Windows had access to all 4 gig's of physical ram (with modern cpu's and
> motherboard IOMMU/hardware virtualization it can be done),

Intel 32 bit CPUs (since the Pentium Pro introduced in 1995) can address
64 GB of physical RAM (they have a 36 bit address bus - google "PAE" for
details).


> So your application can allocate 2 gigs of physical ram right out of the gate.
> The next app can allocate 1.5 gigs. Both of the apps will exist in physical ram
> at the same time. You can start another app, allocate 2 gigs and the first app
> will be swapped out to the page file. Now olny the second and third app will exist
> in physical ram. And on and on..

This is wrong. Windows (or any other OS released in the last 20+ years)
does not swap whole processes at a time. Instead it swaps pages of
4 kB. If you have 3 processes with 2, 1.5 and 2 GB virtual memory in a
machine with 4 GB of physical RAM, each process will have some parts in
RAM and some parts in the page file. The OS tries to keep those parts
which are frequently accessed in RAM and those which are used only
infrequently (or not at all) in the page file.

	hp



------------------------------

Date: Fri, 21 Jan 2011 12:29:32 +0000 (UTC)
From: jidanni@jidanni.org
Subject: what is the cheapest way to just initialize a hash key?
Message-Id: <ihbu7b$d5i$1@news.datemas.de>

Gentlemen, I'm only interested in the keys to my hash,
what is the cheapest way to just initialize the key? Who cares about values.
for (...) { $myhash{ f($_) }++ } #or yet cheaper:
for (...) { $myhash{ f($_) } = 1 } #or yet cheaper still?:
for (...) { $myhash{ f($_) } = undef } #anything even yet more cheaper?
print keys %myhash;
(No I don't want to use an array, as I might be adding a key twice and
would rather not notice it.)


------------------------------

Date: Fri, 21 Jan 2011 13:14:01 +0000
From: bugbear <bugbear@trim_papermule.co.uk_trim>
Subject: Re: what is the cheapest way to just initialize a hash key?
Message-Id: <xImdnc_Wq5QEG6TQnZ2dnUVZ8vKdnZ2d@brightview.co.uk>

jidanni@jidanni.org wrote:
> Gentlemen, I'm only interested in the keys to my hash,

So you're implementing a set.

> what is the cheapest way to just initialize the key? Who cares about values.
> for (...) { $myhash{ f($_) }++ } #or yet cheaper:
> for (...) { $myhash{ f($_) } = 1 } #or yet cheaper still?:
> for (...) { $myhash{ f($_) } = undef } #anything even yet more cheaper?
> print keys %myhash;
> (No I don't want to use an array, as I might be adding a key twice and
> would rather not notice it.)

There are standard timing tools, but until the "obvious" way
has proven to be too slow in practice, I would code
as cleanly and clearly as you can, and optimise later
if needed.

   BugBear


------------------------------

Date: Fri, 21 Jan 2011 14:14:52 +0100
From: Peter Makholm <peter@makholm.net>
Subject: Re: what is the cheapest way to just initialize a hash key?
Message-Id: <87lj2e1jqr.fsf@vps1.hacking.dk>

jidanni@jidanni.org writes:

> Gentlemen, I'm only interested in the keys to my hash,
> what is the cheapest way to just initialize the key? Who cares about values.

I think you are looking for the Benchmark module.

http://search.cpan.org/perldoc?Benchmark

//Makholm


------------------------------

Date: Fri, 21 Jan 2011 17:35:31 +0000 (UTC)
From: dmcanzi@remulak.uwaterloo.ca (David Canzi)
Subject: Re: what is the cheapest way to just initialize a hash key?
Message-Id: <ihcg53$hct$1@rumours.uwaterloo.ca>

In article <ihbu7b$d5i$1@news.datemas.de>,  <jidanni@jidanni.org> wrote:
>Gentlemen, I'm only interested in the keys to my hash,
>what is the cheapest way to just initialize the key? Who cares about values.
>for (...) { $myhash{ f($_) }++ } #or yet cheaper:
>for (...) { $myhash{ f($_) } = 1 } #or yet cheaper still?:
>for (...) { $myhash{ f($_) } = undef } #anything even yet more cheaper?
>print keys %myhash;
>(No I don't want to use an array, as I might be adding a key twice and
>would rather not notice it.)

In circumstances like this I just do the simplest thing:
$hash{$key} = 1;

A second of brain time is worth more than a second of processor
time.

-- 
David Canzi		| Life is too short to point out every mistake. |


------------------------------

Date: Fri, 21 Jan 2011 11:52:24 -0800 (PST)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: what is the cheapest way to just initialize a hash key?
Message-Id: <6fe1d93a-a266-4df6-bbad-120b450d69b3@o4g2000yqd.googlegroups.com>

On Jan 21, 7:29=A0am, jida...@jidanni.org wrote:
> Gentlemen, I'm only interested in the keys to my hash,
> what is the cheapest way to just initialize the key? Who cares about valu=
es.
> for (...) { $myhash{ f($_) }++ } #or yet cheaper:
> for (...) { $myhash{ f($_) } =3D 1 } #or yet cheaper still?:
> for (...) { $myhash{ f($_) } =3D undef } #anything even yet more cheaper?
> print keys %myhash;
> (No I don't want to use an array, as I might be adding a key twice and
> would rather not notice it.)

I frequently must save unique keys, and although I haven't benchmarked
this, the scripts run quickly enough that it's not an issue. This also
has the advantage of giving you the option of checking to see what
keys are repeated, and how many times. My data files typically contain
between 1,000 and 50,000 records -- IO is by far the most time
consuming parts of the scripts.

my %uniques;
while (<DATA>)
{
  my ($id, @rest) =3D split(/,/, $_);
  $uniques{$id}++;
}

CC


------------------------------

Date: Fri, 21 Jan 2011 13:16:44 -0800
From: Keith Thompson <kst-u@mib.org>
Subject: Re: what is the cheapest way to just initialize a hash key?
Message-Id: <lnfwsmc5z7.fsf@nuthaus.mib.org>

dmcanzi@remulak.uwaterloo.ca (David Canzi) writes:
> In article <ihbu7b$d5i$1@news.datemas.de>,  <jidanni@jidanni.org> wrote:
>>Gentlemen, I'm only interested in the keys to my hash,
>>what is the cheapest way to just initialize the key? Who cares about values.
>>for (...) { $myhash{ f($_) }++ } #or yet cheaper:
>>for (...) { $myhash{ f($_) } = 1 } #or yet cheaper still?:
>>for (...) { $myhash{ f($_) } = undef } #anything even yet more cheaper?
>>print keys %myhash;
>>(No I don't want to use an array, as I might be adding a key twice and
>>would rather not notice it.)
>
> In circumstances like this I just do the simplest thing:
> $hash{$key} = 1;
>
> A second of brain time is worth more than a second of processor
> time.

Agreed.

On the other hand, if it turned out that
    $hash{$key} = undef;
were significantly faster than
    $hask{$key} = 1;
(or vice versa) then (a) it could be worth using the faster form
*if* this turned out to be a bottleneck in the performance of the
program, and (b) it could point to an opportunity for a worthwhile
optimization in a future release of the Perl interpreter.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"


------------------------------

Date: Fri, 21 Jan 2011 17:20:07 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: what is the cheapest way to just initialize a hash key?
Message-Id: <n2ckj6p3omf5hkho6a4o12qu646dma5lhj@4ax.com>

Keith Thompson <kst-u@mib.org> wrote:
>On the other hand, if it turned out that
>    $hash{$key} = undef;
>were significantly faster than
>    $hask{$key} = 1;
>(or vice versa) then (a) it could be worth using the faster form
>*if* this turned out to be a bottleneck in the performance of the
>program, 

And this is a very, very big *if*. Normally the real bottleneck is a
poor choice of algorithm and typically you can gain way more performance
by redesigning your algorithm instead of micro-optimizing a single
operation.

jue


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 3263
***************************************


home help back first fref pref prev next nref lref last post