[24112] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 6306 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Mar 26 14:05:43 2004

Date: Fri, 26 Mar 2004 11:05:06 -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, 26 Mar 2004     Volume: 10 Number: 6306

Today's topics:
        Best Control Structure for Iteration <ihatespam@hotmail.com>
    Re: Best Control Structure for Iteration <nobull@mail.com>
        Crypt::OpenPGP <reid@surfline.ne.jp>
        Hash Reference Syntax (Andrew Bell)
    Re: Hash Reference Syntax <ittyspam@yahoo.com>
    Re: how get next 5 lines? <geoffacox@dontspamblueyonder.co.uk>
    Re: how get next 5 lines? <nobull@mail.com>
    Re: how get next 5 lines? <nobull@mail.com>
    Re: How to store multiple value in a Hash for cookies <sbryce@scottbryce.com>
    Re: I want to scanf, dammit! ctcgag@hotmail.com
    Re: listen socket during 3 seconds and then, give up <ThomasKratz@REMOVEwebCAPS.de>
    Re: parsing config file <nobull@mail.com>
        Perl:  Generate registration code (John)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 26 Mar 2004 08:40:52 -0800
From: BigDaDDY <ihatespam@hotmail.com>
Subject: Best Control Structure for Iteration
Message-Id: <1068n89285r0g91@corp.supernews.com>

Hi all,

I am trying to do an iteration loop, but I'm not sure if the control 
structure I am using is the best way to do it.  Does anyone have any 
suggestions as to how to improve this code?  The biggest problem I am 
having is with the last line.  I don't want to explicitly have to specify 
each value of the tolerance hash to equal zero.


I basically call three subroutines.  The first time through I feed the 
compact_2D_fast subroutine starting values (guess values) in the pointer to 
%WF, then the calculated wf's are returned in $wf_flex_ref, which is a 
pointer to the hash which contains calculated wf's. Then the whole process 
starts over, but instead of feeding guess values, I feed in the calculated 
values, to get new calculated values.  When the guess values and the 
calculated values are equal, then my iteration has converged and the 
tolerances are zero and I have the actual values for wf.

This code works as is, but I would like it to be more readable.  Thanks for 
any help you can provide.

Matt



#!usr/bin/perl

### snippet below

($Px_ref, $Py_ref, $Pr_ref, $th_ref, $WF_ref) = compact_2D_fast(\%WF, 
\%Gfast);

$Ex_ref = bearing_moduli($stack_ref, $th_ref, $E11, $E22, $nu12, $G12, 
$tlam, $tply);

$wf_flex_ref = fastflex($Ex_ref, $tlam, \%t_strap, $E_strap, \%Dfast, 
\%Efast, \%Afast, \%Gfast);


%W1 = %$WF_ref;
%W2 = %$wf_flex_ref;


do {

   %W1 = %W2; 

   ($Px_ref, $Py_ref, $Pr_ref, $th_ref, $WF_ref) = compact_2D_fast(\%W1, 
\%Gfast);

   $Ex_ref = bearing_moduli($stack_ref, $th_ref, $E11, $E22, $nu12, $G12, 
$tlam, $tply);

   $wf_flex_ref = fastflex($Ex_ref, $tlam, \%t_strap, $E_strap, \%Dfast, 
\%Efast, \%Afast, \%Gfast);

   %W2 = %$wf_flex_ref;

   while (($k, $v) = each %W2){

      $tol{$k} = $W2{$k} - $W1{$k};
   }

} until ($tol{1} == 0 && $tol{2} == 0 && $tol{3} == 0 && $tol{4} == 0);


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

Date: 26 Mar 2004 17:36:39 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: Best Control Structure for Iteration
Message-Id: <u965crecig.fsf@wcl-l.bham.ac.uk>

BigDaDDY <ihatespam@hotmail.com> writes:

> Does anyone have any suggestions as to how to improve this code?

Always declare all variables as lexically scoped in the smallest
applicable scope unless you have a possitive reason to do otherwise.
This is not a joke.  We really mean it.

I'm damn sure you personally have been told this many times.  The fact
that you've not followed them makes me seriously doubt that you are
serious about wanting to improve your code.

>  The biggest problem I am having is with the last line.  I don't
> want to explicitly have to specify each value of the tolerance hash
> to equal zero.

> } until ($tol{1} == 0 && $tol{2} == 0 && $tol{3} == 0 && $tol{4} == 0);

Assuming all the values in the hash are numbers and that these are all
of them...

 } until ( ! grep { $_ } values %tol );

>    %W2 = %$wf_flex_ref;
> 
>    while (($k, $v) = each %W2){

Why are you copying the hash?  Are you really concerned about
preserving the each-cursor in %$wf_flex_ref ?

>    while (($k, $v) = each %W2){
> 
>       $tol{$k} = $W2{$k} - $W1{$k};
>    }

Why are you saying $W2{$k} rather than $v.

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: Fri, 26 Mar 2004 15:18:25 +0900
From: "Alex Reid" <reid@surfline.ne.jp>
Subject: Crypt::OpenPGP
Message-Id: <c40i09$558$1@news.surfline.ad.jp>

I am trying to implement the Crypt::OpenPGP module for a client to encrypt
an html
e-mail form. I am not sure where to set the path for the public key on the
server. I can get the script to work fine when I have a passphrase set but I
want to use a public key uploaded to the server and not a passphrase.
However when I comment the passphrase out and and use, for example, PubRing
or Recipients I get an error message "can't build entity: no body, and not
multipart". The documentation for OpenPGP states that when using recipients
I do not need a passphrase so I don't understand what I am doing wrong. The
script I am using for encrypt is below. Any help with this would be
appreciated.

my $pgp = Crypt::OpenPGP->new;
my $mailData = $pgp->encrypt(
                                Compat     => 'PGP5',
                                Data       => $message_body,
                                Recipients => '0x0BFC37B5',
                                PubRing          =>
"/home/keys/publickey.asc",
                                Armour     => 1,
#                                Passphrase   => 'jubgg',
                                );



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

Date: 26 Mar 2004 08:10:45 -0800
From: acbell@iastate.edu (Andrew Bell)
Subject: Hash Reference Syntax
Message-Id: <74dc3b26.0403260810.15142845@posting.google.com>

Hi,

I am having trouble passing a hash reference to a subroutine.  Any
help on the syntax would be helpful.

$hash{$k1}{$k2} = "foo";
&doSub(\($hash{$k1}));

sub doSub {
  my($r) = @_;

  if (defined(${$r}{$k2)) {
    do something;
  } 
}

I get an error on the line containing the "defined" statement saying:

Not a HASH reference at file.pl line xx.

Cheers,


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

Date: Fri, 26 Mar 2004 11:50:06 -0500
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: Hash Reference Syntax
Message-Id: <20040326114618.G21521@dishwasher.cs.rpi.edu>

On Fri, 26 Mar 2004, Andrew Bell wrote:

> Hi,
>
> I am having trouble passing a hash reference to a subroutine.  Any
> help on the syntax would be helpful.
>
> $hash{$k1}{$k2} = "foo";
> &doSub(\($hash{$k1}));


You made it more complicated than it is.  Assigning 'foo' to
$hash{$k1}{$k2} automatically makes $hash{$k1} a hash reference.  That,
therefore, is what you should be passing in:

doSub($hash{$k1});

(on another note, don't put & in your function call. That's Perl4 syntax,
and shouldn't be used unless you're sure you need the special behavior
that comes with it - and you generallyd don't.)

>
> sub doSub {
>   my($r) = @_;
>
>   if (defined(${$r}{$k2)) {
>     do something;
>   }
> }
>

Within the sub, $r is the hash reference you've passed in, so you can say:

if (defined $r->{$k2}) { ... }
or
if (defined $$r{$k2}) { ... }


Paul Lalli


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

Date: Fri, 26 Mar 2004 16:05:48 GMT
From: Geoff Cox <geoffacox@dontspamblueyonder.co.uk>
Subject: Re: how get next 5 lines?
Message-Id: <tok8601um5dje4bqk1sbl3e90026kurlh3@4ax.com>

On 26 Mar 2004 14:32:03 GMT, roberson@ibd.nrc-cnrc.gc.ca (Walter
Roberson) wrote:

>In article <8vc860l0c6ifrepujr8pm89upcgua6pp59@4ax.com>,
>Geoff Cox  <geoffacox@dontspamblueyonder.co.uk> wrote:
>:I am opening a file to look for a piece of data and having found that
>:data I want to read the next 5 lines...as there is another bit of data
>:on one of those lines...
>
>my (undef, undef, undef, undef, $interestingline) =
>   map { <FILEHANDLE> } (1..5);

Walter 

Thanks for the above- will get read up on this one! I found following
which works but does not look too elegant I suppose!

my $pattern = $1;

open (IN, "d:/file.htm");
while (<IN>){
    last if /$pattern/;
    }
    my ($curr, $next1, $next2, $next3) = <IN>;
    close (IN);

etc etc


Cheers

Geoff



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

Date: 26 Mar 2004 17:38:13 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: how get next 5 lines?
Message-Id: <u91xnfecfu.fsf@wcl-l.bham.ac.uk>

Geoff Cox <geoffacox@dontspamblueyonder.co.uk> writes:

>     my ($curr, $next1, $next2, $next3) = <IN>;

That slurps the whole remainder of the file into memory then throws
away all but the first 4 lines.  See other branches of this thread.

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: 26 Mar 2004 17:39:15 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: how get next 5 lines?
Message-Id: <u9wu57cxto.fsf@wcl-l.bham.ac.uk>

John Bokma <postmaster@castleamber.com> writes:

> Walter Roberson wrote:
> 
> > In article <8vc860l0c6ifrepujr8pm89upcgua6pp59@4ax.com>,
> > Geoff Cox  <geoffacox@dontspamblueyonder.co.uk> wrote:
> > :I am opening a file to look for a piece of data and having found that
> > :data I want to read the next 5 lines...as there is another bit of data
> > :on one of those lines...
> > my (undef, undef, undef, undef, $interestingline) =
> >    map { <FILEHANDLE> } (1..5);
> 
> uhm, on "one of those lines" :D
> 
> my @oneofthoselines = map { <FILEHANDLE> } (1..5);

That is equivalent to 

  my @oneofthoselines = <FILEHANDLE>;

You meant 

  my @oneofthoselines = map { scalar <FILEHANDLE> } (1..5);


-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: Fri, 26 Mar 2004 09:06:45 -0700
From: Scott Bryce <sbryce@scottbryce.com>
Subject: Re: How to store multiple value in a Hash for cookies
Message-Id: <1068lv9kkfkno76@corp.supernews.com>

Nvidia wrote:

> Hello
> 
> I have just started learning PERL and I have written a simple CGI script.
> 
> Inside this script, I have a group of checkboxes:
> 1.    name: "tools"
>     values: "hammer", "nails", "screwdriver"
> 2.    name: "cars"
>     values: "sedan", "hatch", "sports"
> 
> and also a submit button.

No, you don't have those things in a Perl script. Perhaps you have them 
in an HTML form that calls the script?


> BUT I'm having problem storing multiple values into a hash. Even when 
> user click more than 1 value in each group of checkboxes, my hash only 
> have one item in each group.

Show us code. We can't offer help with code we can't see.


> I need to use hash because I want to store the number of checkboxes the 
> user "checked" in the cookies.

Are you sure you want to do that? It is possible (Likely? Certain?) that 
you will wind up storing a reference to the hash in the cookie. When you 
try to restore the data, you will get an error, because the hash being 
referenced will not exist. Cookies store strings.



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

Date: 26 Mar 2004 16:46:56 GMT
From: ctcgag@hotmail.com
Subject: Re: I want to scanf, dammit!
Message-Id: <20040326114656.676$PN@newsreader.com>

Dmitry Epstein <mitia.nospam@northwestern.edu.invalid> wrote:
> I know this has been asked before, mostly by novice percolytes (is
> that what you call Perl adherents?), but the sages just replied:
> scanf sucks, we don't need it.  Well, here is a real-life situation
> that I have to deal with quite frequently.  See if you can suggest
> an acceptable solution using the all-mighty pattern search.
>
> I have a file with floats separated by spaces and newlines.  There
> can be several numbers per line, but I don't know and don't care
> about exactly how they are written because that has no relation to
> the logic of the problem.

Well, there's your problem.  Why store your data in some random,
unproductive fashion?

perl -lne'print foreach split' < randomly_formatted_file > one_per_line

(Alas, this does assume each line is short enough to comfortably fit into
memory.)

> Suppose the file contains 2500 numbers
> and I need to read the first 500, or better yet, read just the
> number #500.  This number can be in the middle of a line, for all I
> know.
>
> Using something like scanf or the C++ stream input operator, the
> solution is ludicrously simple: just read the requisite number of
> times in a loop using a single statement.  But with Perl I just
> don't see a simple way to do it that does not involve slurping the
> entire file into memory first (and then possibly duplicating it
> while applying split).  Oh sure, I could read line by line, split
> each line, and count the number of words read.  But that's not
> nearly as simple, and besides, if I have to continue working with
> the file, then the issue of having read more numbers than was
> needed may come up (remember, that target number may have been in
> the middle of a line).

If you don't want to simply store the file in a sane format, you
can convert it on the fly from within your main program:

open FH, "perl -lne'print foreach split' $filename |" or die $!;

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


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

Date: Fri, 26 Mar 2004 17:59:29 +0100
From: Thomas Kratz <ThomasKratz@REMOVEwebCAPS.de>
Subject: Re: listen socket during 3 seconds and then, give up
Message-Id: <406462a2.0@juno.wiesbaden.netsurf.de>

luc2 wrote:

[ snipped ]

I replied to your post in comp.lang.perl.modules

Don't multipost. If you really have to post to more than one newsgroup, do 
a crosspost (see the posting guidelines for this group, if you don't know, 
what I'm talking about)

Thomas
-- 
open STDIN,"<&DATA";$=+=14;$%=50;while($_=(seek( #J~.> a>n~>>e~.......>r.
STDIN,$:*$=+$,+$%,0),getc)){/\./&&last;/\w| /&&( #.u.t.^..oP..r.>h>a~.e..
print,$_=$~);/~/&&++$:;/\^/&&--$:;/>/&&++$,;/</  #.>s^~h<t< ..~. ...c.^..
&&--$,;$:%=4;$,%=23;$~=$_;++$i==1?++$,:_;}__END__#....>>e>r^..>l^...>k^..


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

Date: 26 Mar 2004 17:21:26 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: parsing config file
Message-Id: <u9ad23ed7t.fsf@wcl-l.bham.ac.uk>

Simon.Lee@asia.ing.com (Simon Lee) writes:

> I'd like to parse a config file like this (match pattern I want in log
> files, and just want to send warning msgs to "adderss3" & "address4"):
> 
> /tmp/a.a {
>     DEFAULT_MAIL_ADDR "addr1
>     MATCH "[eE]rror" {
>         MAIL "address1", "address2"
>         EXCEPT "No [Ee]rror"                ## comments here
>     }
>     MATCH "[Ww]arn" {
>         MAIL "address3", "address4"
>     }                          
> }   

Is the format pre-defined?

If not then consider making your application into a module and the
"config file" into a script that uses the module.

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: 26 Mar 2004 10:30:13 -0800
From: john_1998@hotmail.com (John)
Subject: Perl:  Generate registration code
Message-Id: <c2c15952.0403261030.bb12901@posting.google.com>

I am new to Perl.  I want to write a Perl script that generates a
registration code on my website.  It's basically a hex of the week of
the year and year, plus and multiplied by some number.  I have a
sample of what I would like in VB.  Can someone help convert this to
Perl.  I'm hoping it's as simple as VB.  Thanks for your help...

------- VB Code ------------
Sub RegCode()
Dim strCode as String
strCode = Hex(((DatePart("ww", Date) & Year(Date)) + 12345) * 12345)
Debug.Print strCode
End Sub
---------------------------

John


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

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 6306
***************************************


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