[24619] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 6795 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jul 12 20:13:42 2004

Date: Mon, 12 Jul 2004 08:05:06 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Mon, 12 Jul 2004     Volume: 10 Number: 6795

Today's topics:
    Re: date problem <1usa@llenroc.ude.invalid>
    Re: LAMP - Program Design with Perl (Dieter)
    Re: looping through array <1usa@llenroc.ude.invalid>
    Re: Negation of RegEx <pinyaj@rpi.edu>
        Question about arrays & memory alloaction/de allocation <dlaw001@yahoonospam.co.uk>
    Re: Question about arrays & memory alloaction/de alloca <1usa@llenroc.ude.invalid>
    Re: Question about arrays & memory alloaction/de alloca <noreply@gunnar.cc>
    Re: Question about arrays & memory alloaction/de alloca <karel@e-tunity.com>
        Test <jwkenne@attglobal.net>
    Re: Test <1usa@llenroc.ude.invalid>
    Re: Useful one-liners and other short Perl scripts (J. Romano)
    Re: Useful one-liners and other short Perl scripts (J. Romano)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 12 Jul 2004 14:16:58 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: date problem
Message-Id: <Xns9524689A38EF7asu1cornelledu@132.236.56.8>

Gabkin <gabriell@programmer.net> wrote in
news:2lf7l0Fbtt9nU1@uni-berlin.de: 

> Gabkin wrote:
>> I need to generate a date in the format YYYYMMDDHHMMSSS for a perl 
>> program. (Note the _three_ digits allocated for Seconds, not the
>> usual _two_)

I am going to suggest:

http://www.catb.org/~esr/faqs/smart-questions.html#explicit

 ...

> Update: the correct answer is
> "date +%Y%m%d%H%M%S%-1N"
> 
> However as it turns out, even this was inadequate as perl executed the
> task too rapidly. This is basically what I am thinking of using...
> (feel free to criticize my code)

The questions is: Do you just need consecutive 15 digit numbers or do 
these have to be the date?

If your program is going to run continuously for less than a month, %Y%m 
do nothing for you in terms of ensuring uniqueness. How about shaving of 
the year digits and adding some more nanoseconds?

If you explained exactly what you are doing, it would be far easier help 
you.

> #!/usr/local/bin/perl
> for(1..15000) {
>      print(&getUnique15DigitDate(),"\n");
> }
> exit;

See perldoc perlsub for why getUnique15DigitDate() is preferable to 
&getUnique15DigitDate().

> I chose 15000 for this script because I know for a fact that my
> program will loop 15472 times, thus using waiting 0.1 seconds
> everytime could push up execution speed by just over 25 minutes, a
> 500% increase! 

ITYM 'execution time'.

> Is there a modulus function in perl?
> I can't find one....

perldoc perlop

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



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

Date: 12 Jul 2004 06:06:55 -0700
From: affenkopf2004@yahoo.de (Dieter)
Subject: Re: LAMP - Program Design with Perl
Message-Id: <34dd0a89.0407120506.56df6add@posting.google.com>

anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote in message news:<ccs72r$17i$1@mamenchi.zrz.TU-Berlin.DE>...

Dear Anno,

thanks for your patience.
It's true that I first have to become clear about the problem.
Maybe it's simpler as I thought. Why not doing this:


%info = cgi -> Vars # What infos about which fruits are needed

#some code for parsing

foreach $fruit (@fruits){
do_this if defined $info{taste};
do_that if defined $info(color)
 ....
}

Such a loop could be used for constructing the statement for the 
database and also for formatting the output.

As I already said, the problem remains that I have to keep track
what kind of information is returned by the database.
One time the third column could be '"taste", next time it could be
"shape"
or "color". By the way, I didn't mention that the info from the
database
is sucked into an hash of arrays. The fruits are the keys and the
infos
are the values of this HoA. Maybe there's a better way.  

Wait a minute, maybe this works also:
Just get the whole row from the DB, and then select the desired
information with the loop described above. Well, that's brute force,
since the rows contain a lot of data. 


Best Regards, Dieter


> 
> If you find it hard to explain, you have not fully understood your
> problem yet.

That's true, I admit.
 
> Does that mean that the second column of the database stores all
> three types of data, and you must know from somewhere else what
> it is?

No, each piece of information is stored in its own column.

+cherry+red+sweet+"Cherries are delicious bla+ ...
+lemon+yellow+sour+"Lemon trees bla"+ ...

 
> Or are we talking multiple databases,

No, definitely not.

> 
> > Maybe I do it that way: slurp all parameters from the user
> > into a hash and then go through all possible parameters
> > with "foreach" and  "if defined". 
> 
> No comment on that either, unless you say what you mean by "parameter"

Well, the options chosen from the HTML-form to
be queried from the database.


> > Maybe the trick is to name the parameters in the HTML-form
> > exactly the same as they are named in the database and store
> > the database information in an hash of hashes. 
> 
> That sounds distinctly like a bad idea.  Don't tie data together that
> underlie independent constraints.  (Column-) names in a data base have
> one syntax, parameter names in HTML-forms have another.  They have
> independent life cycles and change for different reasons.  Don't force
> them together.  If those names are user-visible (still not sure what
> exactly we're talking about), that's a further no-no.  

That's true. Might be a security issue.


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

Date: 12 Jul 2004 14:18:23 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: looping through array
Message-Id: <Xns952468D7771DBasu1cornelledu@132.236.56.8>

svenne <bobsmith@[no-thankyou-very-much]jippii.fi> wrote in news:9NrIc.3073
$0l2.1932@reader1.news.jippii.net:

> so basically 
> my %h_array2 = map { $_, 1 } @ar_array2;
> for ( my $i = 0; $i < scalar( @ar_array1 ); $i++ )
> {
>     if ( exists( $h_array2{ $ar_array1[$i] } ) ){
>                 return true;
>         }
>         return false;
> }
> ...is what I need,

In case you do not mean that sarcastically, please see 

perldoc -q intersection

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



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

Date: Mon, 12 Jul 2004 09:42:11 -0400
From: Jeff 'japhy' Pinyan <pinyaj@rpi.edu>
To: Dan <dan_yuan@trendmicro.com>
Subject: Re: Negation of RegEx
Message-Id: <Pine.SGI.3.96.1040712094125.921072A-100000@vcmr-64.server.rpi.edu>

[posted & mailed]

On 11 Jul 2004, Dan wrote:

>The negation should be right in the boundary "/Negation of RegEx/".
>Do you have any solution for that?

Hasn't the solution already been provided?

/^(?!.*foo)/s is the negation of the pattern /foo/.

--
Jeff "japhy" Pinyan         %  How can we ever be the sold short or
RPI Acacia Brother #734     %  the cheated, we who for every service
RPI Corporation Secretary   %  have long ago been overpaid?
http://japhy.perlmonk.org/  %  
http://www.perlmonks.org/   %    -- Meister Eckhart




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

Date: Mon, 12 Jul 2004 14:36:11 GMT
From: "LeTubs" <dlaw001@yahoonospam.co.uk>
Subject: Question about arrays & memory alloaction/de allocation
Message-Id: <vlxIc.560$Gl6.186@newsfe2-win.ntli.net>

Hi
I'm trying to look for info on how perl handles arrays
in terms of memory. The reason why is below, the first
loop ensures that the array is empty, while the second
loop checks to see if the information already exists within
the array, if not it adds it & does some processing.
What I am concerned about is possible memory problems
As I'm not sure if this is the correct way to NULL an array
as I want to zero out that array at the end of the $count loop
get the next batch information and then start the process all
over again.
Is what I'm doing safe ? or is there a better/safer way ?

Thanks
David

my(@Array);

for( $i; ..;$i++){
#make sure that the array is empty
@Array = ();
for( $count = 1; .....; $count++ ){
# now start processing information.
   for( $w = 0; $w < @Array; $w++ ){
    # See if SomeInformation already exists in the Array
    if( $Array[$w] != SomeInformation){
      $Array[$count] = SomeInformation;
       .....
      }
    }
  }
}




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

Date: 12 Jul 2004 13:52:25 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Question about arrays & memory alloaction/de allocation
Message-Id: <Xns95246470D9D1Fasu1cornelledu@132.236.56.8>

"LeTubs" <dlaw001@yahoonospam.co.uk> wrote in news:vlxIc.560$Gl6.186
@newsfe2-win.ntli.net:

> Hi
> I'm trying to look for info on how perl handles arrays
> in terms of memory. The reason why is below,

 ...

> my(@Array);
> 
> for( $i; ..;$i++){
> #make sure that the array is empty
> @Array = ();
> for( $count = 1; .....; $count++ ){
> # now start processing information.
>    for( $w = 0; $w < @Array; $w++ ){
>     # See if SomeInformation already exists in the Array
>     if( $Array[$w] != SomeInformation){
>       $Array[$count] = SomeInformation;
>        .....
>       }
>     }
>   }
> }


You probably want to use a hash :)
-- 
A. Sinan Unur
1usa@llenroc.ude.invalid 
(remove '.invalid' and reverse each component for email address)



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

Date: Mon, 12 Jul 2004 16:15:57 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Question about arrays & memory alloaction/de allocation
Message-Id: <2lfklsFbq79dU1@uni-berlin.de>

LeTubs wrote:
> I'm trying to look for info on how perl handles arrays in terms of
> memory. The reason why is below, the first loop ensures that the
> array is empty, while the second loop checks to see if the
> information already exists within the array, if not it adds it &
> does some processing.

To empty an array, it's correct that you just need to do:

     @array = ();

But the code your posted confuses me, and it appears to be
inefficient, if it at all does what you want it to do. To make it
easier to advise you, you may want to explain a little more about what
you want to accomplish.

For instance, I wouldn't be surprised if a hash proved to suit your
needs better than an array...

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: Mon, 12 Jul 2004 16:15:29 +0200
From: Karel Kubat <karel@e-tunity.com>
Subject: Re: Question about arrays & memory alloaction/de allocation
Message-Id: <40f29d06$0$30992$e4fe514c@dreader12.news.xs4all.nl>

Hi,

> I'm trying to look for info on how perl handles arrays
> in terms of memory. The reason why is below, the first
> loop ensures that the array is empty, while the second
> loop checks to see if the information already exists within
> the array, if not it adds it & does some processing.
> What I am concerned about is possible memory problems
> As I'm not sure if this is the correct way to NULL an array
> as I want to zero out that array at the end of the $count loop
> get the next batch information and then start the process all
> over again.
> Is what I'm doing safe ? or is there a better/safer way ?
> 
> Thanks
> David
> 
> my(@Array);
> 
> for( $i; ..;$i++){
> #make sure that the array is empty
> @Array = ();
> for( $count = 1; .....; $count++ ){
> # now start processing information.
>    for( $w = 0; $w < @Array; $w++ ){
>     # See if SomeInformation already exists in the Array
>     if( $Array[$w] != SomeInformation){
>       $Array[$count] = SomeInformation;
>        .....
>       }
>     }
>   }
> }

Why not use a second data structure to see whether something's in your array
or not?

my (@array,             # Stuff I need to process
    %present);          # Already present? If so, the value
                        # is the index where it's at.
for (my $i = 0; $i < $SOME_MAX_VAL; $i++) {
    my $present_index;
    if ($present_index = $present{$SomeInformation}) {
        # Process $array[$present_index], because 
        # $SomeInformation is already there.
    } else {
        # Not yet found. Store in the array.
        push (@array, $SomeInformation);
        # Log as present.
        $present{$SomeInformation} = $#array;
    }
}

I hope you get the general idea. I'm not sure I understand _why_ you need
this processing, but anyway here's the approach. Probably you're better off
anyway by using two hashes; one where you some processed data, and one
where you flag whether you've already processed the data before. Both
hashes would be indexed by $SomeInformation. This would probably work best.

One last word of advice: put "use strict;" at the top of your script and
always declare your variables..

Cheers,
-- 
Karel Kubat <karel@e-tunity.com, karel@qbat.org>
Phone: mobile (+31) 6 2956 4861, office (+31) (0)38 46 06 125
PGP fingerprint: D76E 86EC B457 627A 0A87  0B8D DB71 6BCD 1CF2 6CD5

  Bible stories, retold by young scolars (original phrasing):
  "The First Commandment was when Eve told Adam to eat the
  apple."



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

Date: Mon, 12 Jul 2004 14:39:02 GMT
From: "John W. Kennedy" <jwkenne@attglobal.net>
Subject: Test
Message-Id: <aoxIc.1653$_b.2566865@news4.srv.hcvlny.cv.net>

This is a test


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

Date: 12 Jul 2004 14:42:08 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Test
Message-Id: <Xns95246CDE52B0Dasu1cornelledu@132.236.56.8>

"John W. Kennedy" <jwkenne@attglobal.net> wrote in news:aoxIc.1653
$_b.2566865@news4.srv.hcvlny.cv.net:

> This is a test
> 

Your test has failed. Post test messages only to news.test or similar 
groups provided by your ISP.

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



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

Date: 12 Jul 2004 07:38:21 -0700
From: jl_post@hotmail.com (J. Romano)
Subject: Re: Useful one-liners and other short Perl scripts
Message-Id: <b893f5d4.0407120638.410d2618@posting.google.com>

> "J. Romano" wrote:
> > 
> > Use this to count number of characters in a string (not counting the
> > newline):
> > 
> >  perl -lne "chomp; print tr///c"

"John W. Krahn" <krahnj@acm.org> wrote in message news:<40F1E7AB.313F6996@acm.org>...
>
> The chomp is superfluous as the -l switch does a chomp for you:
> 
> perl -lne "print y///c"

   Oh, I didn't know that the -l switch did a chomp automatically. 
Thanks for pointing that out to me.  (It's a good thing I didn't use
"chop"!  :)

> And if you want to include the newline in the count:
> 
> perl -ne "print y///c,$/"

   That was for DOS, of course.  And for the sake of completeness, the
Unix equivalent is:

      perl -ne 'print y///c,$/'

   Thanks for pointing these out, John.

   -- Jean-Luc


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

Date: 12 Jul 2004 07:47:09 -0700
From: jl_post@hotmail.com (J. Romano)
Subject: Re: Useful one-liners and other short Perl scripts
Message-Id: <b893f5d4.0407120647.1ff40598@posting.google.com>

> "J. Romano" wrote:
> > 
> > Here's a program that I like to call the "Spooky Writer":
> > 
> > # Unix:
> >  perl -e'$|=1;for(split//,join"",<>){print;select(undef,undef,undef,rand(.5))}'

"John W. Krahn" <krahnj@acm.org> wrote in message news:<40F1EE5D.17CAA01A@acm.org>...
>
> You can save a few key strokes like this:
> 
> perl -e'$|=$/=\1;print,select undef,undef,undef,rand.5for<>'


   Wow!  That's impressive!  And I thought I had squeezed it as small
as it was going to go.

   Actually, if you examine my JAPH, you'll see another trick I use to
make it even smaller.  I do this by using undefined variables in the
place of undef.  Applying this to your modified version of my
one-liner, we would get:

   # Unix:
      perl -e'$|=$/=\1;print,select $J,$L,$R,rand.5for<>'
   # DOS:
      perl -e"$|=$/=\1;print,select $J,$L,$R,rand.5for<>"

   Thanks for pointing that out, John.

   -- Jean-Luc


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

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


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