[25179] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 7428 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Nov 19 18:10:59 2004

Date: Fri, 19 Nov 2004 15:10:14 -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, 19 Nov 2004     Volume: 10 Number: 7428

Today's topics:
        finding the number of keys in hash <ustun@wam.umd.edu>
    Re: finding the number of keys in hash <uguttman@athenahealth.com>
    Re: finding the number of keys in hash <ustun@wam.umd.edu>
    Re: finding the number of keys in hash <uguttman@athenahealth.com>
    Re: finding the number of keys in hash <mritty@gmail.com>
    Re: finding the number of keys in hash <amead@comcast.net>
    Re: finding the number of keys in hash <mritty@gmail.com>
    Re: finding the number of keys in hash <joe@inwap.com>
    Re: Not Initializing Variables Correctly? nobull@mail.com
    Re: Problem with Hashes <nospam@nospam.com>
    Re: Problem with Hashes <spamtrap@dot-app.org>
    Re: Problem with Hashes <nospam@nospam.com>
    Re: Really? <perl@my-header.org>
    Re: script hangs waiting for key stroke (chris)
    Re: Sending Outlook Email w/voting buttons through a Pe <1usa@llenroc.ude.invalid>
    Re: the antichomp <tassilo.von.parseval@rwth-aachen.de>
    Re: the antichomp (wana)
    Re: the antichomp <tadmc@augustmail.com>
        Thread::Queue on Windows 98 (Richard Trahan)
        Win32::OLE Excel problem (ancient)
    Re: Win32::OLE Excel problem (Jay Tilton)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 19 Nov 2004 14:16:31 -0500
From: DnaDude <ustun@wam.umd.edu>
Subject: finding the number of keys in hash
Message-Id: <cnlgqh$l4h$1@grapevine.wam.umd.edu>

I would like to compute the number of
keys in a hash, as in below, but without
using a temporary variable.

#this works
use strict;
@foo = keys %$A;
$number = $#{@foo};

($A is a reference to a hash). So why does

$number = $#{keys %$A};
or
$number = $#{@{keys %$A}};

not work? I get the error

"Can't use string ("357") as an ARRAY ref while "strict refs" in use"

Many thanks,

Cev.


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

Date: Fri, 19 Nov 2004 14:19:12 -0500
From: Uri Guttman <uguttman@athenahealth.com>
Subject: Re: finding the number of keys in hash
Message-Id: <m3vfc13avj.fsf@lap.athenahealth.com>

>>>>> "D" == DnaDude  <ustun@wam.umd.edu> writes:

  D> I would like to compute the number of
  D> keys in a hash, as in below, but without
  D> using a temporary variable.

  D> #this works
  D> use strict;
  D> @foo = keys %$A;
  D> $number = $#{@foo};

perldoc -f keys.

you are using it but there is more than one way to use it.

uri


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

Date: Fri, 19 Nov 2004 14:25:06 -0500
From: DnaDude <ustun@wam.umd.edu>
Subject: Re: finding the number of keys in hash
Message-Id: <cnlhai$l4h$4@grapevine.wam.umd.edu>

Uri Guttman wrote:
>>>>>>"D" == DnaDude  <ustun@wam.umd.edu> writes:

>   D> I would like to compute the number of
>   D> keys in a hash, as in below, but without
>   D> using a temporary variable.
> 
>   D> #this works
>   D> use strict;
>   D> @foo = keys %$A;
>   D> $number = $#{@foo};
> 
> perldoc -f keys.

I've read that documentation but it doesn't seem to answer my
problem: I'd like to do something like

for(my $i=0; $i < $#{keys %$A}; $i++){

}

without having to define an auxiliary variable.

Cev.



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

Date: Fri, 19 Nov 2004 14:31:51 -0500
From: Uri Guttman <uguttman@athenahealth.com>
Subject: Re: finding the number of keys in hash
Message-Id: <m3mzxd3aag.fsf@lap.athenahealth.com>

>>>>> "D" == DnaDude  <ustun@wam.umd.edu> writes:

  D> Uri Guttman wrote:

  >> perldoc -f keys.

  D> I've read that documentation but it doesn't seem to answer my
  D> problem: I'd like to do something like

reread it again.

  D> for(my $i=0; $i < $#{keys %$A}; $i++){

  D> }

  D> without having to define an auxiliary variable.

well that doesn't use an auxiliary variable but rather an anon hash. in
any case the docs for keys has your answer and it is very early in
there.

and why would you do such a loop anyhow? looping over the keys makes
sense but looping over the number of keys without accessing them makes
little sense. and c style for loops are not popular in perl so you may
have an XY problem here as well.

uri


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

Date: Fri, 19 Nov 2004 20:21:41 GMT
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: finding the number of keys in hash
Message-Id: <pBsnd.72$K36.42@trndny03>

"DnaDude" <ustun@wam.umd.edu> wrote in message
news:cnlgqh$l4h$1@grapevine.wam.umd.edu...
> I would like to compute the number of
> keys in a hash, as in below, but without
> using a temporary variable.
>
> #this works
> use strict;
> @foo = keys %$A;
> $number = $#{@foo};

Frankly, I find that surprising.  I'm not disagreeing that it works,
because I have no interest in trying it.  I'm just surprised.

In any case, you're making it harder than it is.  'keys' can be used in
scalar context:

$number = keys %$A;

Paul Lalli



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

Date: Fri, 19 Nov 2004 14:27:45 -0600
From: Alan Mead <amead@comcast.net>
Subject: Re: finding the number of keys in hash
Message-Id: <pan.2004.11.19.20.27.43.840689@comcast.net>

On Fri, 19 Nov 2004 14:25:06 -0500, DnaDude wrote:

> Uri Guttman wrote:
>>>>>>>"D" == DnaDude  <ustun@wam.umd.edu> writes:
> 
>>   D> I would like to compute the number of
>>   D> keys in a hash, as in below, but without
>>   D> using a temporary variable.
>> 
>> perldoc -f keys.
> 
> I've read that documentation but it doesn't seem to answer my
> problem: 

try perldoc -f length (and NOTE the note)

> I'd like to do something like
> 
> for(my $i=0; $i < $#{keys %$A}; $i++){

Here's what you want, I think.. but why?

for (my $i=0; $i< scalar keys %$A; $i++) { print "$i\n"; }

for a three-pair hash, prints:

0
1
2

-Alan


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

Date: Fri, 19 Nov 2004 20:30:40 GMT
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: finding the number of keys in hash
Message-Id: <QJsnd.684$Vy.569@trndny06>

"DnaDude" <ustun@wam.umd.edu> wrote in message
news:cnlhai$l4h$4@grapevine.wam.umd.edu...
> Uri Guttman wrote:
> >>>>>>"D" == DnaDude  <ustun@wam.umd.edu> writes:
>
> >   D> I would like to compute the number of
> >   D> keys in a hash
> >
> > perldoc -f keys.
>
> I've read that documentation but it doesn't seem to answer my
> problem

Uh.  It doesn't?  Can you tell me what the 2nd sentence of that document
says?  (The one in parentheses)?

Paul Lalli



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

Date: Fri, 19 Nov 2004 21:05:16 GMT
From: Joe Smith <joe@inwap.com>
Subject: Re: finding the number of keys in hash
Message-Id: <getnd.54041$V41.52288@attbi_s52>

DnaDude wrote:
> I would like to compute the number of
> keys in a hash, as in below, but without
> using a temporary variable.
> 
> #this works
> use strict;
> @foo = keys %$A;
> $number = $#{@foo};

That is an error; it's off by one.

   $number = $#{@foo} + 1;
   $number = $#foo    + 1;
   $number = @foo;

	-Joe


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

Date: 19 Nov 2004 10:07:11 -0800
From: nobull@mail.com
Subject: Re: Not Initializing Variables Correctly?
Message-Id: <4dafc536.0411191007.7d96ece@posting.google.com>

"Paul Lalli" <mritty@gmail.com> wrote in message news:<8_4nd.7569$tS4.3778@trndny09>...
> "Buck Turgidson" <jc_va@hotmail.com> wrote in message
> news:831v62-i05.ln1@turf.turgidson.com...

> > but I have managed to put the following
> > together, based on some examples I've found on the web.
> >
> > However, I receive a long series of the following error message,

> >  "Use of uninitialized value [...]"
> 
> Those are not error messages, they are warnings.  There is a rather
> significant difference.

This is true.

> > This leads me to believe I am [...] not using variables correctly.

The OP is misreading the warning incorrectly (sic).

The phase "uninitialized value" in not meaningful.  Most people's
brain's subconciously attempt to make it meaningful by misreading it. 
The OP has misread it as "uninitialized variable".  The correct way to
misread it is "undefined value".

> >
> >
> > #!/usr/bin/perl -w
> 
> These days, it is preferred to add the line
> use warnings;
> in place of the -w switch

In particular the "use warnings" mecahanism allows you to fine tune
the warnings you want.  In particular there are a number of contexts
where undefined values can safely be treated as strings and in that
case the best thing to do is simply suspect that warning for the
duration of the operation.


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

Date: Fri, 19 Nov 2004 14:39:58 -0500
From: "daniel kaplan" <nospam@nospam.com>
Subject: Re: Problem with Hashes
Message-Id: <1100893275.98085@nntp.acecape.com>

"Tad McClellan" <tadmc@augustmail.com> wrote in message
news:slrncpqvd0.6aa.tadmc@magna.augustmail.com...

> The argument passing in the call was fine.
>
> The argument grabbing in the sub definition was not fine.

hi tad,

yeah i already posted up above that i realized i got hung up on WHAT he was
passing, and therefor totally missed that the problwm was how he assigned it
within the subroutine.....

i have to admit, in the past few weeks am totally pleased and impressed with
how much you can do with Perl by typing so little.....but here is where i
miss some of the strictness of C....

have a variable of 'this kind'?

well don't use it in 'this kind' context and the compiler will let you know,
and boom problem found quickly.....but with Perl you can have @row, $row,
$row[0], and so on and so on and it makes you have to be more digilent as
the programmer....

guess perhaps after  ten years of using just C and a compiler i've gotten
lazy, much like relying on spelling checkers have made my typing far worse
than it ever was...hope that made sense...

anyway, didn't want to extend this thread more than i already had, but don't
want to leave anyone out who was nice enough to post back to me

daniel




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

Date: Fri, 19 Nov 2004 16:57:11 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Problem with Hashes
Message-Id: <p9WdnQ3nx8yP8QPcRVn-gg@adelphia.com>

daniel kaplan wrote:

> have a variable of 'this kind'?
> 
> well don't use it in 'this kind' context and the compiler will let you know,
> and boom problem found quickly.....but with Perl you can have @row, $row,
> $row[0], and so on and so on and it makes you have to be more digilent as
> the programmer....

Context is very important in Perl. Subroutines can find out what context 
they were called in with wantarray(), and alter their return value(s) 
accordingly. An array used in scalar context has a different value than 
one used in list context. Etc.

It can be painful for a C programmer who's accustomed to strict typing, 
but context is an idea that's central to Perl and it's well worth learning.

sherm--

-- 
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org


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

Date: Fri, 19 Nov 2004 17:34:30 -0500
From: "daniel kaplan" <nospam@nospam.com>
Subject: Re: Problem with Hashes
Message-Id: <1100903749.795274@nntp.acecape.com>

"Sherm Pendley" <spamtrap@dot-app.org> wrote in message
news:p9WdnQ3nx8yP8QPcRVn-gg@adelphia.com...
> daniel kaplan wrote:

> It can be painful for a C programmer who's accustomed to strict typing,
> but context is an idea that's central to Perl and it's well worth
learning.

i believe that is the root of my "learning hump" here, but am getting over
it, one bloody inch at a time

thanks again




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

Date: Fri, 19 Nov 2004 22:11:35 +0100
From: Matija Papec <perl@my-header.org>
Subject: Re: Really?
Message-Id: <1bosp0dk67rclfljjkkjrilos1uv8ifioi@4ax.com>

X-Ftn-To: Tad McClellan 

Tad McClellan <tadmc@augustmail.com> wrote:
>I expect he was trying to say that Perl *mis*information is
>widespread, along with an example that I found a bit amusing.
>
>
>> or just want to populate killfile?
>I didn't see anything remotely killfilable in the OP.
>
>What did you see?

A troll, but wasn't sure so I asked first.


-- 
Matija


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

Date: 19 Nov 2004 14:12:19 -0800
From: nadsinoz@hotmail.com (chris)
Subject: Re: script hangs waiting for key stroke
Message-Id: <b8996f29.0411191412.b2d401c@posting.google.com>

It runs fine!  

The util/YYYYMM.pm ...


--  util/YYYYMM.pm --

#!/usr/bin/perl -w

use strict;

my @return_range;	
sub range {
	my ($start, $end) = @_;
	my $current = $start;
	push @return_range, $start; 
	while ( $current != $end ){
		$current = yyyymm_add($current);  # i.e. current++
		push @return_range, $current; # put current++ into array
	}
	return \@return_range;	
}

sub yyyymm_add {
	my ($yyyymm) = shift;

	my $YYYY = substr( $yyyymm, 0, 4 );
	my $MM = substr( $yyyymm, 4, 2 );		

	if ( $MM == 12 ) { 
		$YYYY++; $MM = 1; 
	} else {
		$MM++;
	}
	my $yyyymm_p1 = sprintf("%04d%02d",$YYYY, $MM);
	return $yyyymm_p1;
}


return 1;


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

Date: 19 Nov 2004 16:42:35 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Sending Outlook Email w/voting buttons through a Perl script...
Message-Id: <Xns95A6771FBA7D2asu1cornelledu@132.236.56.8>

"Kevin B. Pease" <kevin.pease@fmr.com> wrote in
news:Cgond.84$Nq3.15@news-srv1.fmr.com: 

> via Apache), from which I'd like to send email to people at my
> company, all of whom use Outlook 2000 (or higher).  The script is used
> for submitting requests to different support groups, and I've received
> requests to allow for the inclusion of "voting buttons" (those little
> buttons in Outlook you can add to the message that will allow you to
> respond with a pre-defined answer) by management to indicate approval
> / disapproval of various requests.  

I would go about this in one of two ways:

1. Send myself a message that looks like what the management wants. Save 
the raw message to examine what it contains. If it is in some well 
defined text format, then you can just use that as your message template 
using one of the email modules.

2. Instead of "Outlook" buttons, send an HTML formatted message which 
links (via graphics that look like buttons) to a CGI script that 
authenticates and logs the vote.

Hope this helps.

-- 
A. Sinan Unur
1usa@llenroc.ude.invalid 
(remove '.invalid' and reverse each component for email address)



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

Date: Fri, 19 Nov 2004 17:41:56 +0100
From: "Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de>
Subject: Re: the antichomp
Message-Id: <slrncps8ik.po.tassilo.von.parseval@localhost.localdomain>

Also sprach Arndt Jonasson:

> anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) writes:
>> Uri Guttman  <uri@stemsystems.com> wrote in comp.lang.perl.misc:

>> > map is easy but for some reason freaks out many perl newbies.
>> 
>> Well, it's "functional programming" which is supposedly hard.  Some
>> languages do make it hard, requiring things like "pure functions"
>> defined by "lambda expressions" to feed to things like map().
>> 
>> Perl uses coderefs and, more importantly, automatically translates
>> some blocks and expressions to coderefs, so functional programming
>> introduces no new syntax.  It's a successful case of making easy
>> things easy.
>
> One might want recursion, but deep recursion does not work well in
> perl, not even tail recursion (at least not in 5.005 - I haven't checked
> later versions).

Tail recursion in fact works very well. The only problem with it is that
perl is not smart enough to detect those cases automatically so you have
to write your own by assigning to @_ and using 'goto &func'.

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$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


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

Date: 19 Nov 2004 09:37:17 -0800
From: ioneabu@yahoo.com (wana)
Subject: Re: the antichomp
Message-Id: <bf0b47ca.0411190937.559ef2c8@posting.google.com>

Tad McClellan <tadmc@augustmail.com> wrote in message 

> If you want to "transform a list", use map().
> 
> If you want to "filter a list", use grep().

These are going to be the functions of the week for me.  I will try to
use them in a Perl sentence at least once a day.

wana - trying to build on limited Perl vocabulary.


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

Date: Fri, 19 Nov 2004 11:40:07 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: the antichomp
Message-Id: <slrncpsbvn.8m9.tadmc@magna.augustmail.com>

Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote:
> Uri Guttman  <uri@stemsystems.com> wrote in comp.lang.perl.misc:
>> >>>>> "w" == wana  <ioneabu@yahoo.com> writes:
>> 
>>   w> I would not thought of it myself (the use of map), but now I will.
>>   w> Thanks :-)
>> 
>> map is easy but for some reason freaks out many perl newbies.
> 
> Well, it's "functional programming" which is supposedly hard.


 ... but really isn't (well, _this part_ of func programming isn't hard).

I present it to students like this:

   The return value(s) of a function serves as arguments to some
   other function, and the return value(s) from that function
   serve as arguments to yet another function... as deep
   as you want to go.

   You read them "backwards", from right to left. For example:

      print sort <>;

   <> supplies a list of all the lines from all the files as
   arguments to sort().

   sort() supplies a list of all the rearranged list elements as
   arguments to print().

   and print() prints them.



-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: 19 Nov 2004 13:00:48 -0800
From: rtrahan@optonline.net (Richard Trahan)
Subject: Thread::Queue on Windows 98
Message-Id: <1bcb1039.0411191300.39d104c8@posting.google.com>

I can't get Thread::Queue to work on Windows 98, although Thread::new,
join, etc., seem to work ok. Specifically, the Queue example in Wall
doesn't dequeue anything. No error messages appear. Has anyone gotten
this to run on W98? Does it work on W2000?


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

Date: 19 Nov 2004 10:56:53 -0800
From: lordsll@ldschurch.org (ancient)
Subject: Win32::OLE Excel problem
Message-Id: <d3363bb6.0411191056.3915b56@posting.google.com>

I am trying to create a Chart using win32::OLE and excel.  I have
everything working except generating the background for the Chart. 
The command I am using is --

$Excel->ActiveChart->ChartArea->Fill->{Visible} =1;
$Excel->ActiveChart->ChartArea->Fill->{ForeColor}->{SchemeColor} = 17;
$Excel->ActiveChart->ChartArea->Fill->{BackColor}->{SchemeColor} = 2;
$Excel->ActiveChart->ChartArea->Fill->TwoColorGradient({
         Style => "msoGradientHorizontal",
         Variant => 1 });

Everything seems to work until it hits TwoColorGradient.  It finishes
executing, but does not set two colors for the gradient.  It just
shows the ForeColor.

I would appreciate any help.  I am soooo close!


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

Date: Fri, 19 Nov 2004 23:02:59 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Win32::OLE Excel problem
Message-Id: <419e7ba0.239723624@news.erols.com>

lordsll@ldschurch.org (ancient) wrote:

: I am trying to create a Chart using win32::OLE and excel.  I have
: everything working except generating the background for the Chart. 
: The command I am using is --
: 
: $Excel->ActiveChart->ChartArea->Fill->{Visible} =1;
: $Excel->ActiveChart->ChartArea->Fill->{ForeColor}->{SchemeColor} = 17;
: $Excel->ActiveChart->ChartArea->Fill->{BackColor}->{SchemeColor} = 2;
: $Excel->ActiveChart->ChartArea->Fill->TwoColorGradient({
:          Style => "msoGradientHorizontal",
:          Variant => 1 });
: 
: Everything seems to work until it hits TwoColorGradient.  It finishes
: executing, but does not set two colors for the gradient.  It just
: shows the ForeColor.

msoGradientHorizontal is the name of a constant in MSOffice.  You're
passing it to the method as a string.  Not the same thing.

Import the Office constants into your program's namespace so you can use
the msoGradientHorizontal constant:

    use Win32::OLE::Const 'Microsoft Office';

    # stuff elided

    $Excel->ActiveChart->ChartArea->Fill->TwoColorGradient({
         Style => msoGradientHorizontal,
         Variant => 1 });

You might also declutter things with a for() loop to alias a repeatedly
used object:

    for( $Excel ->ActiveChart ->ChartArea ->Fill ) {
        $_ ->{Visible} =1;
        $_ ->{ForeColor} ->{SchemeColor} = 17;
        $_ ->{BackColor} ->{SchemeColor} = 2;
        $_ ->TwoColorGradient({
            Style => msoGradientHorizontal,
            Variant => 1
        });



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

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:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#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 V10 Issue 7428
***************************************


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