[16918] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4330 Volume: 9

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Sep 15 06:06:35 2000

Date: Fri, 15 Sep 2000 03:05:11 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <969012311-v9-i4330@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Fri, 15 Sep 2000     Volume: 9 Number: 4330

Today's topics:
    Re: a wannabe <qvyht@iobox.fi>
    Re: Building and traversing trees <cingram-at-pjocs-dot-demon-dot-co-dot-uk>
    Re: can i run a cgi script within javascript tag? <klidge@mailbox.gr>
    Re: can i run a cgi script within javascript tag? <peter@wastholm.com>
        Can't make DBI <grichards@flashcom.net>
    Re: escape strings ? (James Stevenson)
    Re: Help in RegExp to highlight keyword in HTML file <jaap@stack.nl>
    Re: help regarding split <philipg@atl.mediaone.net>
    Re: HTML::LinkExtor and <applet> problem <gisle@ActiveState.com>
    Re: Is there a good freeware/shareware IDE for PERL? (Tim Hammerquist)
    Re: Just Cant Get The File Upload Thing paul5544@my-deja.com
    Re: kill a process in Win32 <hg_zauner@gmx.de>
        Module for CGI Session Management? <mkruse@netexpress.net>
        Monitoring multiple child process's children ? <rhsteven@eng.uct.ac.za>
    Re: MS-DOS window closes <nuklearsite@crosswinds.net>
    Re: MS-DOS window closes (Tim Hammerquist)
        Newbie tempted to use perl - please advise <john@seaway.demon.co.uk>
        perlxs - finding the class name <sm_jamiesonNOsmSPAM@hotmail.com.invalid>
    Re: Req.: The perfect Perl Editor? (Tim Hammerquist)
    Re: Req.: The perfect Perl Editor? (Tim Hammerquist)
    Re: Shortest code for Fibonacci? (Gwyn Judd)
    Re: Shortest code for Fibonacci? (Gwyn Judd)
    Re: Shortest code for Fibonacci? (Mark-Jason Dominus)
    Re: Shortest code for Fibonacci? <bruce@hoult.org>
        RE: space available in hard disk for NT using perl <hopes@ole.com>
    Re: sprintf() rounding problem <bart.lateur@skynet.be>
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

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

Date: Fri, 15 Sep 2000 08:21:28 GMT
From: Hessu <qvyht@iobox.fi>
Subject: Re: a wannabe
Message-Id: <39C1DA10.663B1E45@iobox.fi>



Neil Jones wrote:
> 
> give me a clue where do i start
> i know zero
> i want to know much

http://www.perl.com/

http://www.cm.cf.ac.uk/Dave/PERL/

http://www.taxpolicy.com/tutorial/pltutor.htm

Go to a library, read a book, use a pen and a paper


<There's no stupid questions, only stupid people>


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

Date: Fri, 15 Sep 2000 10:31:02 +0100
From: "Clyde Ingram" <cingram-at-pjocs-dot-demon-dot-co-dot-uk>
Subject: Re: Building and traversing trees
Message-Id: <969009922.561.0.nnrp-01.9e98e5bc@news.demon.co.uk>


Mark-Jason Dominus <mjd@plover.com> wrote in message
news:39c163e0.468c$1e5@news.op.net...
> In article <8prkj5$rtt$1@news5.svr.pol.co.uk>,
> Clyde Ingram <clyde@NOSPAMgetofftheline.freeserve.co.uk> wrote:
> >To extend the problem, how do I insert a string on some nodes?
>
> Now's the part of the evening when you look at the code I sent and
> think about how it works and then adapt it to your own problem.

Point taken, Mark-Jason.

I've trawled the books for several long fruitless evenings, trying to work
out a solution, before placing the first posting on this.

The closest I got was from O'Reilly's Advanced Perl Programming.

Chapter 1.3 "Nested Data Structures" builds a 2-level tree, with each node
have a name plus 1 other string ('age').  But the author does not volunteer
a generic function to populate the tree to any number of levels.  This is
where I cannot make the breakthrough to extend your generic solution.  I
suspect it must be an easy, standard algorithm.

Regards,
Clyde

P.S. Here is the quote from "Advanced Perl Programming":

Let us say we would like to track a person's details and that of their
dependents. One approach is to create separate named hash tables for each
person:

%sue = (              # Parent
    'name' => 'Sue',
    'age'  => '45');
%john = (             # Child
    'name' => 'John',
    'age'  => '20');
%peggy = (            # Child
    'name' => 'Peggy',
    'age'  => '16');
The structures for John and Peggy can now be related to Sue like this:

@children = (\%john, \%peggy);
$sue{'children'} = \@children;

# Or
$sue{'children'} = [\%john, \%peggy];





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

Date: Fri, 15 Sep 2000 10:27:01 +0300
From: "klidge" <klidge@mailbox.gr>
Subject: Re: can i run a cgi script within javascript tag?
Message-Id: <8psj0p$17d7$1@ulysses.noc.ntua.gr>

it seems that tony got the idea.
i actually tried this:
<script language src="http://etc.etc/cgi-bin/myscript.cgi">
</script>

But well,it didn't work,
myscript.cgi was something like this:
#!/bin/perl
use CGI;
$q = new CGI;

print "document.write('<b>Test</b>');"
 ------------------------------
any (more) ideas?

Tony L. Svanstrom <tony@svanstrom.com> wrote in message
news:1egzggo.1d40lem1zg29kN%tony@svanstrom.com...
> .. <drclue@drclue.net> wrote:
>
> > klidge wrote:
> > >
> > > I would like to ask you if it's possible for a cgi script written in
perl
> > > to run from within a javascript script
> > > example:
> > > Html source:
> > > <script language="javascript">
> > > --the code i need here---
> > > </script>
> > > In order for the "result" after accessing the page would be the same
as the
> > > htm source was:
> > > <script language="javascript">
> > > document.write('this line has been generated from a cgi perl script');
> > > </script>
> > >
> > > thank you in advance
> >
> > Sortof.
> >
> > You could declare an ILAYER inside a DIV , then if IE is detected
> > overwrite the contents of the DIV with an IFRAME.
> >
> > Now you can create a request in the IFRAME/ILAYER that represents
> > a request to your server side javascript , which would in turn , output
> > javascript to update the interaction
>
> Nah, iframe doesn't have to be used, if you are going to use a
> SCRIPT-tag then simply add a src to it and get the result of the
> Perl-script that way.
>
>
>      /Tony
> --
>      /\___/\ Who would you like to read your messages today? /\___/\
>      \_@ @_/  Protect your privacy:  <http://www.pgpi.com/>  \_@ @_/
>  --oOO-(_)-OOo---------------------------------------------oOO-(_)-OOo--
>    on the verge of frenzy - i think my mask of sanity is about to slip
>  ---ôôô---ôôô-----------------------------------------------ôôô---ôôô---
>     \O/   \O/  ©99-00 <http://www.svanstrom.com/?ref=news>  \O/   \O/




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

Date: Fri, 15 Sep 2000 08:13:16 GMT
From: Peter Wastholm <peter@wastholm.com>
Subject: Re: can i run a cgi script within javascript tag?
Message-Id: <39C1D98F.638C3C81@wastholm.com>

klidge wrote:
> 
> But well,it didn't work,
> myscript.cgi was something like this:
> #!/bin/perl
> use CGI;
> $q = new CGI;
> 
> print "document.write('<b>Test</b>');"
> ------------------------------
> any (more) ideas?

Try outputting an HTTP header as well, and not just the body.

/Peter
------------------------------------------------------------------------
Peter Wastholm      As far as we know, our computer has never had an
peter@wastholm.com  undetected error.                            Weisert
"Aphorisms Galore!" -- Feed Your Wit     http://www.aphorismsgalore.com/


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

Date: Fri, 15 Sep 2000 01:52:59 -0700
From: "Gabe" <grichards@flashcom.net>
Subject: Can't make DBI
Message-Id: <ss3ojneadc6117@corp.supernews.com>

I've got a fresh installation of Redhat 6.2 with Perl 5.00503 that the
Redhat installer installed. When I try to make DBI I get a bunch of errors
referring to files with an .h extension saying "No such file or directory".
The readme says if you get errors related to perl's header files then you
have a bad perl installation. I don't know how I could have a bad
installation since perl was installed automatically. I tried reinstalling
the perl RPM, but that didn't resolve the problem. I couldn't find help in
the DBI list archives. Does anyone know what the problem is and how to fix
it?

Thanks!
Gabe




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

Date: 15 Sep 2000 00:15:34 GMT
From: James@linux.home (James Stevenson)
Subject: Re: escape strings ?
Message-Id: <slrn8s2u2s.635.James@linux.home>

Hi

did not realize they had drivers for the database system that i am using but there is
i just founf them and installed them :)


On Thu, 14 Sep 2000 16:32:29 -0700, Christopher M. Jones <christopher_j@uswest.net> wrote:
>
>I assume you're using the DBI module (and if not why not?!), so
>you can simply use the quote() function (created for exactly
>this purpose).
>
>


-- 
---------------------------------------------
Check Out: http://www.users.zetnet.co.uk/james/
E-Mail: mistral@stevenson.zetnet.co.uk
  1:10am  up 6 days, 15:51,  8 users,  load average: 0.45, 0.61, 0.57


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

Date: Fri, 15 Sep 2000 05:49:28 GMT
From: "Jaap" <jaap@stack.nl>
Subject: Re: Help in RegExp to highlight keyword in HTML file
Message-Id: <ILiw5.220165$LX4.1789886@nlnews00.chello.com>

Try
$html =~ s/ keyword /<font color=...>keyword<\/font>/g;
mind the spaces

<parvaist@my-deja.com> schreef in bericht news:8pqsrr$n5$1@nnrp1.deja.com...
> Hello,
>
> I would like to create a regexp to highlight a keyword in a html text.
> Highlight means add the <FONT COLOR=...>keyword</FONT> tags. Of course,
> if the keyword in contained in an HTML tag (example: <A
> HREF="keyword.html">), it should not been highlighted !!!!




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

Date: Fri, 15 Sep 2000 04:49:30 GMT
From: "Philip Garrett" <philipg@atl.mediaone.net>
Subject: Re: help regarding split
Message-Id: <uThw5.1684$jC4.275243@typhoon.southeast.rr.com>

yingying3 <yingying3@peoplepc.com> wrote in message
news:39c1763e$1@flexnet239.uunt.net...
> Please advise if possible to split a delimited(",") string, in which all
> fields are wraped with double quota. But some fields include comma(",")
> within the double quotas, such as,
> "aaa","bbb bb","ccc,cc","ddd, d, ddd","ee"
>
> If the split function doesn't work, what is the better way to split the
> string?

Try Text::CSV

hth,
p




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

Date: 15 Sep 2000 10:17:30 +0200
From: Gisle Aas <gisle@ActiveState.com>
Subject: Re: HTML::LinkExtor and <applet> problem
Message-Id: <m34s3ijch1.fsf@eik.g.aas.no>

walter@ganymede.frii.com (Walter Pienciak) writes:

> I'm using HTML::LinkExtor to extract links from some HTML documents.
> Works pretty well.
> 
> But, when it hits an <applet> tag looking like
> 
>     <applet codebase="./classes" code="Bounce">
> 
> which reflects an actual directory structure of ./classes/Bounce.class
> it's not reporting back one link to
> 
>     ./classes/Bounce.class
> 
> or even one link to
> 
>     ./classes/Bounce
> 
> but rather 2 links to
> 
>     ./classes
>     Bounce
> 
> 
> Any thoughts, ideas?

Resolving it to a single link (code => './classes/Bounce') makes
sense.  Patches are welcomed.

-- 
Gisle Aas


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

Date: Fri, 15 Sep 2000 06:44:58 GMT
From: tim@degree.ath.cx (Tim Hammerquist)
Subject: Re: Is there a good freeware/shareware IDE for PERL?
Message-Id: <slrn8s3i3e.4e8.tim@degree.ath.cx>

Abigail <abigail@foad.org> wrote:
> Victor Koldun (lkh@class.class.lviv.ua) wrote:
> __ Does anyone know of some good and freeware/shareware PERL IDE to build
> __ and debug scripts without the Internet access?
> 
> FreeBSD, and don't listen to those penguin freaks!

Though I use linux myself, I will admit FreeBSD and its relatives are at
times more stable and more true to their roots than Linnus' creation.

But as long as they're running a PC with a UNIXish OS, I have no qualms.
=)

-- 
-Tim Hammerquist <timmy@cpan.org>

Genius without education is like silver in the mine.
	-- Ben Franklin


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

Date: Fri, 15 Sep 2000 05:19:40 GMT
From: paul5544@my-deja.com
Subject: Re: Just Cant Get The File Upload Thing
Message-Id: <8psbgv$p3v$1@nnrp1.deja.com>

In article <8okjs1$6ao$1@nnrp1.deja.com>,
  paul5544@my-deja.com wrote:
> Was hoping someone could give me a hand. I have read the CGI docs,
> looked at the forums for days, reviewed several freeware code samples
> and just cannot get this thing through my thick skull.
>
> Here is the code I am trying to use to upload a file.
> NOTE: $pFIle1 variable is a value grabbed from a parsed input form.
The
> value is something to the effect of C:\My Documents\graphic.gif or
> whever their file resides.
> The results of this rounting are that it creates the file, but
> transfers nothing so if I disable the unlink, I end up with a filename
> with a size of 0.
>
> I would really apprecaite any help on this.
>
> Anyway, here is the subrountine
>
> use CGI;
>     my $bytesread;
>     my $size;
>     my $fileName = $pFile1;
> if ($pFile1 ne "")
> {
>    $fileName =~ s!^.*(\\|\/)!!;
>    open (OUTFILE, ">$basedir/$fileName");
>    binmode OUTFILE;
>    print "$basedir/$fileName<br>";
>   while ($bytesread = read($pFile1,$buffer,1024))
>   {
>    print OUTFILE $buffer;
>    $total_size += $size;
>   }
> close OUTFILE;
> }
> if ($total_size > 0)
> {
>  #file was transferred
>  &create_main_form('File Upload was sucessfull.');
>  exit(0);
> }
> else
> {
>  #file was not transferred
>  unlink "$basedir/$short_name";
>  &create_main_form('Warning! File Upload was NOT sucessfull. Please
Try
> Again.');
>  exit(0);
> }
>
> I apprecaite anyones help..
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
>

I would like to thank Joe and Brian for their assistance.
I figured out why I was having some much trouble. In my main
script I parse the form data. When I put the subroutine for
the image upload features into the main script using the
CGI.pm module's form features it was not picking up the
form value. Maybe I am wrong in saying this, but I assume
you cannot parse a form, then in a subroutine try to
pick out a form value with the CGI features.
Anyway, I dumped it into its own little script and passed
values between the two.
If my deductions are incorrect, please let me know. I am
quickly learning more and more every day and am hoping
to contribute back to the community when I am up to speed
on these things.
Again, thanks for everyone's help!!!


Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: Fri, 15 Sep 2000 12:02:24 +0100
From: "Hansgeorg Zauner" <hg_zauner@gmx.de>
Subject: Re: kill a process in Win32
Message-Id: <8pss3i$dvnkk$1@ID-45951.news.cis.dfn.de>

You might also have a look at Win32::Process.
It includes Win32::Process::KillProcess($pid, $exitcode)
So you don't have to call external programs

Hansgeorg




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

Date: Fri, 15 Sep 2000 05:50:10 GMT
From: "Matt Kruse" <mkruse@netexpress.net>
Subject: Module for CGI Session Management?
Message-Id: <mMiw5.43285$Qx4.1470559@news1.rdc1.il.home.com>

Is there a perl module available whose purpose is just to maintain a
"session" in CGI programming?

I haven't found one, and there doesn't seem to be a definitive answer in
past newsgroup postings.

What I'd be looking for is a completely portable solution - that is, not
reliant on web server or OS.
In a couple of minutes, I wrote down these thoughts:

  - Sessions stored as files in a configurable directory
  - Session ID's generated randomly (best way to do this?)
  - Files hold name/value pairs of session data
  - Check session file dates to see if session has expired (last modified
date).
  - When a session expires, do full cleanup of old expired sessions (won't
scale well, depends on app)

So, I could see it working like this:

$Session = new CGI::Session($id);  # Reads in existing session or creates
new
unless ($Session->isExpired()) {
    $Session->setValue("requests",$Session->getValue("requests")++);
    }
if ($Session->getValue("requests") > 50) {
    $Session->End();
    }
else {
    $Session->Save();
    }

Makes logical sense to me. This wouldn't be too difficult, really. Just
takes time. If anyone has already written it, please let me know. Also, I
know that CGI.pm does some sort of session management, but it doesn't really
do what I would like, or in a way I would like to do it.

Thanks!

Matt Kruse
mkruse@netexpress.net
http://www.mattkruse.com/





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

Date: Fri, 15 Sep 2000 08:33:39 +0200
From: Richard <rhsteven@eng.uct.ac.za>
Subject: Monitoring multiple child process's children ?
Message-Id: <39C1C2C3.FB1C2B97@eng.uct.ac.za>

Hi

There is probably a FAQ on this somewhere, but I have an interesting
problem (at least, I think so ;):

I am writing a perl program to control another program (call it {A})
which will do some computing for me.  I now have access to a decent
4 proccessor machine, so I really want to use all 4 proccessors.

Now the problem.
{A} spawns a {B} which spawns {C} (typical scientific Fortran written
 program)

Anyway, I want to use a perl program to run multiple occurences of {A}
and then monitor each programs results as it moves to {B}, {C}.  I need
to then read in the results of {C} and carry on.

However, each process has a new PID on the unix machine.  The O'Reilly
book I have (Programming Perl) is not much help on this, and I have
not found anything on the net either :(

FYI: this is a Genetic Algorithm which controls a FEA code.

Ideas/comments/help appreciated :)

-Bye
-Richard


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

Date: Fri, 15 Sep 2000 04:27:31 GMT
From: "cwin17" <nuklearsite@crosswinds.net>
Subject: Re: MS-DOS window closes
Message-Id: <Tyhw5.4440$m7.2052340@news3.rdc1.on.home.com>

I used a batch file, for whatever script I happened to be running, simply
containing "D:\Perl\perl.exe file.pl" (or whatever your drive/path is), and
executed it from a dos box (as Brandon said). since i am only working on one
script at a time, i just use the batch file to avoid having to type paths
over and over again.
--
cwin17

http://www.confusion.ipfox.com




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

Date: Fri, 15 Sep 2000 06:42:37 GMT
From: tim@degree.ath.cx (Tim Hammerquist)
Subject: Re: MS-DOS window closes
Message-Id: <slrn8s3hv1.4e8.tim@degree.ath.cx>

Razib <raldanash@my-deja.com> wrote:
> i'm using ACTIVEPERL, and every time i try to open a .pl script i've
> set up in the MS-DOS window, it closes before i can see what's going on.
> 
> is there some way i can figure out what's going on?  i downloaded
> ACTIVEPERL several times, so i know it's not that

My best advice is: run it from the command line. Go to the Start menu,
find the MS-DOS shortcut, click on it.  If your PATH is set up, there
should be a problem typing "perl script.pl".  True, you lose Windows'
file association "feature", but I always get a little tingle when I take
some of Windows' power in my own hands.  I don't run Windows without a
DOS window (or equivalent text-based shell window).

-- 
-Tim Hammerquist <timmy@cpan.org>
Nearly all men can stand adversity, but if you
want to test a man's character, give him power.
	-- Abraham Lincoln


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

Date: Fri, 15 Sep 2000 10:08:05 +0100
From: "John G.Burns" <john@seaway.demon.co.uk>
Subject: Newbie tempted to use perl - please advise
Message-Id: <39C1E6F5.5750@seaway.demon.co.uk>

I've been looking for some scripting software to run under NT and I
think Perl may be the answer. I normally program C++ and Java under Unix
but have only limited Perl experience.

What I need to do is build a list of files changed in the last 24 hours
and copy them to a directory. I then need to bring up a modem link and
copy the files to a remote host. The program should invoke itself at a
regular time every night.

I could do this in Unix but I have to use NT for this project.

If Perl isn't the best way to do this under NT then what is?

Where can I download the most suitable version of Perl for NT?

-- 
"It is better to remain silent & be thought a fool than to open your
mouth in a meeting with users & prove it." Ancient programming proverb

Views expressed here are not those of Stolt Offshore. Aberdeen, Bonny
Scotland

Business   john@seaway.demon.co.uk
Home page  http://www.unixnerd.demon.co.uk john@unixnerd.demon.co.uk


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

Date: Fri, 15 Sep 2000 02:20:12 -0700
From: SimonJ <sm_jamiesonNOsmSPAM@hotmail.com.invalid>
Subject: perlxs - finding the class name
Message-Id: <14b5ef1c.4cfc7d44@usw-ex0106-048.remarq.com>

A simple question:
If I pass a blessed reference into an perl XS function, how can I find
out the class name, i.e. do the C equivalent of the perl "ref" command.

Simon.


* Sent from Novell Discussion Forums http://novell.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!



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

Date: Fri, 15 Sep 2000 06:37:39 GMT
From: tim@degree.ath.cx (Tim Hammerquist)
Subject: Re: Req.: The perfect Perl Editor?
Message-Id: <slrn8s3hln.4e8.tim@degree.ath.cx>

Ben Kennedy <bkennedy@hmsonline.com> wrote:
> Editing perl scripts with emacs is great because emacs has a standard 'Perl'
> major mode, which does nice things like indenting code properly by hitting
> tab and flashing back to the opening curly when you type a closing curly.

I have this in vim.  And I have no problem taking advantage of vi modes;
as a touch-typist, I truly appreciate not have to move my fingers off
the home row.

I apologize.  I really didn't want to start this in clpm.

-- 
-Tim Hammerquist <timmy@cpan.org>

Legend -- a lie that has attained the dignity of age.
	-- H. L. Mencken


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

Date: Fri, 15 Sep 2000 06:39:43 GMT
From: tim@degree.ath.cx (Tim Hammerquist)
Subject: Re: Req.: The perfect Perl Editor?
Message-Id: <slrn8s3hpj.4e8.tim@degree.ath.cx>

Mark-Jason Dominus <mjd@plover.com> wrote:
> I find pico impossible because it doesn't do line numbering.  When the
> compiler says you have an error on line 57, you have to count 57 lines
> down manually. 

In larger files, I might actually prefer an X implementation of emacs
(it has scroll bars) over pico, but since I used Win32 Notepad for a
long time before I opened my eyes to power editors (and unix apps in
general), I don't complain much with smaller files.  But I definitely
have my preferences. =)

-- 
-Tim Hammerquist <timmy@cpan.org>

My life has a superb cast but I can't figure out the plot.
	-- Ashleigh Brilliant


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

Date: Fri, 15 Sep 2000 05:01:22 GMT
From: tjla@guvfybir.qlaqaf.bet (Gwyn Judd)
Subject: Re: Shortest code for Fibonacci?
Message-Id: <slrn8s3b8u.4rs.tjla@thislove.dyndns.org>

I was shocked! How could Clint Mario Cleetus <cmc77@pantheon.yale.edu>
say such a terrible thing:
>Hi,
>
>I am looking for a concise and elegant way to calculate the Fibonacci
>Number corresponding to a given number. But it should also avoid recursion
>in order to be efficient. I wrote a standard subroutine for this:

<snip>

>But this seems to be like any other standard C subroutine. Please let me
>know if you have one-liners or atleast a much more elegant solution for
>this.

perl -le 'print length while ($ / =~ s|(.*)|s ,. *,$ /,ex, $ / . $ &|e)'

Concise: Check
Elegant: ummm
Avoids Recursion: Check
Efficient: Define "Efficient"
Standard: Nope
One-Line: Check
Obfuscated: I hope so :)

-- 
Gwyn Judd (print `echo 'tjla@guvfybir.qlaqaf.bet' | rot13`)
Things will be bright in P.M. A cop will shine a light in your face.


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

Date: Fri, 15 Sep 2000 05:22:10 GMT
From: tjla@guvfybir.qlaqaf.bet (Gwyn Judd)
Subject: Re: Shortest code for Fibonacci?
Message-Id: <slrn8s3cfv.4rs.tjla@thislove.dyndns.org>

I was shocked! How could Gwyn Judd <tjla@guvfybir.qlaqaf.bet>
say such a terrible thing:
>I was shocked! How could Clint Mario Cleetus <cmc77@pantheon.yale.edu>
>say such a terrible thing:
>>Hi,
>>
>>I am looking for a concise and elegant way to calculate the Fibonacci
>>Number corresponding to a given number. But it should also avoid recursion
>>in order to be efficient. I wrote a standard subroutine for this:
>
><snip>
>
>>But this seems to be like any other standard C subroutine. Please let me
>>know if you have one-liners or atleast a much more elegant solution for
>>this.
>
>perl -le 'print length while ($ / =~ s|(.*)|s ,. *,$ /,ex, $ / . $ &|e)'

Whoops. Remember kiddies, this is what happens when you make last minute
changes to mission critical code just before it goes out the door! You
make a mistake. And that is baaaaad. Of course it will be obvious to all
of you that I should have done this instead:

perl -le 'print length while ($" =~ s|(.*)|s c. *c$ "cex, $ ". $ &|e)'

:)

-- 
Gwyn Judd (print `echo 'tjla@guvfybir.qlaqaf.bet' | rot13`)
Never ruin an apology with an excuse.
-Kimberly Johnson


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

Date: Fri, 15 Sep 2000 06:47:11 GMT
From: mjd@plover.com (Mark-Jason Dominus)
Subject: Re: Shortest code for Fibonacci?
Message-Id: <39c1c5ee.5f9e$1f8@news.op.net>

In article <Pine.GSO.4.10.10009142301000.15482-100000@morpheus.cis.yale.edu>,
Clint Mario Cleetus  <cmc77@pantheon.yale.edu> wrote:
>I am looking for a concise and elegant way to calculate the Fibonacci
>Number corresponding to a given number. But it should also avoid recursion
>in order to be efficient. 

I think you are looking for this:

        sub fib {
          my ($n) = @_;
          my ($a, $b) = (0, 1);
          while ($n--) {
            ($a, $b) = ($b, $a+$b);
          }
          $a;
        }

It is O(n).

However, this is more striaghtforward (because it uses recursion)
*and* it is efficient:

    use Memoize;
    sub fib { my ($n) = @_; 
              $n < 2 ? $n : fib($n-1) + fib($n-2) }
    BEGIN { memoize 'fib' }

This is also O(n).

If you really need it to be efficient, and you expect n to be large,
there is an O(log n) algorithm:

        sub fib {
          my ($a) = mm($_[0]);
          $a;
        }
        
        sub mm {
          my ($n) = @_;
          return (1, 0, 0, 1) if $n == 0;
          return (1, 1, 1, 0) if $n == 1;
          if ($n % 2) {                 # odd n
            my ($a, $b, $c, $d) = mm($n-1);
            ($a+$b, $a, $c+$d, $d);
          } else {                      # even n
            my ($a, $b, $c, $d) = mm($n>>1);
            my ($ad, $bc) = ($a+$d, $b*$c);
            ($a*$a+$bc, $b*$ad, $c*$ad, $d*$d+$bc);
          }
        }

If you want to compute, say, fib(1000000000), this is definitely the
way to go.  If I were you I would translate it to C before optimizing
it any further.

There may be a faster O(log n) algorithm that is based more directly
on the identity

        F(2n) = F(n)(F(n+1) + F(n-1))

but it's past 2:30 AM here and I can't remember it offhand.





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

Date: Fri, 15 Sep 2000 18:49:18 +1200
From: Bruce Hoult <bruce@hoult.org>
Subject: Re: Shortest code for Fibonacci?
Message-Id: <bruce-B1AEA3.18491815092000@news.akl.ihug.co.nz>

In article 
<Pine.GSO.4.10.10009142301000.15482-100000@morpheus.cis.yale.edu>, 
Clint Mario Cleetus <cmc77@pantheon.yale.edu> wrote:

> Hi,
> 
> I am looking for a concise and elegant way to calculate the Fibonacci
> Number corresponding to a given number. But it should also avoid recursion
> in order to be efficient. I wrote a standard subroutine for this:
> 
> sub Fib
> {
> 	$num = shift;	
> 
> 	return 1 if $num < 2;
> 	
> 	$f1 = $f2 = 1;
> 	for ($i = 1; $i < $num; $i++)
> 	{
> 		$f = $f1 + $f2;
> 		$f1 = $f2;
> 		$f2 = $f;
> 	}
> 	return $f;
> }
> 
> But this seems to be like any other standard C subroutine. Please let me
> know if you have one-liners or atleast a much more elegant solution for
> this.
> 
> Thanks.

What's wrong with this?

   perl -pe '$_=int(sqrt(.2)*(.5+sqrt(5/4))**($_+1)+.5)."\n"'

Or, to put it your way:

  sub Fib {
    int(sqrt(.2)*(.5+sqrt(5/4))**($_[0]+1)+.5)
  }

-- Bruce


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

Date: Fri, 15 Sep 2000 09:47:06 +0200
From: "Hopes" <hopes@ole.com>
Subject: RE: space available in hard disk for NT using perl
Message-Id: <8psk1g$qou$1@diana.bcn.ttd.net>

Thanks.
May be incredible, but I have been searching something like that in the CPAN
for a long, long time.
I'm very pleased. I've though the solution was in the Windows Registry.
Thanks again

jason <elephant@squirrelgroup.com> escribió en el mensaje de noticias
MPG.142bf9f7bd81aa5b9897b3@localhost...
> Hopes <hopes@ole.com> wrote ..
> >¿Could anyone tell me if it is possible (and simple) to find the space
> >available in a NT machine using Perl?
>
> use the GetDriveSpace function contained in the Win32::AdminMisc module
> provided by Roth Consulting
>
>   http://www.roth.net/perl/adminmisc/
>
> --
>   jason -- elephant@squirrelgroup.com --




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

Date: Fri, 15 Sep 2000 07:52:28 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: sprintf() rounding problem
Message-Id: <jjk3sss9589r5tc7viaqq5qh04g8cs5me8@4ax.com>

David Hugh-Jones wrote:

>I am developing a program which has to add Value Added Tax to an order at
>17.5%. I do this roughly as follows:
>
>$tax=17.5;
>$value=57;                                 # (for example )
>$value+=$value*$tax*0.01;       # tax is a percentage
>$value = sprintf(%.2f,$value);     # to convert $value to the nearest penny
>
>My problem is that sprintf doesn't round the numbers correctly. E.g. for
>this instance, $value is 66.975 after line 3. This should round to 66.98,
>but in fact the fourth line outputs 66.97.

Yes... that's this annoying "round halves to nearest even" behaviour.
Statistically, it's "nice", but boy I sure hate the guts of it.

First of all: 0.01 is an exact number in floating point. Divide by 100
instead, which is an exact number in FP, and you might get nearer to
what you want.

Instead of what you've been trying so far, I'd attempt a multiplication
with 2. Round off, divide, and there you decide what direction you want
to go. You can work in cents, if you like, but still multiply by 2.

	$twice_cents = sprintf '%.0f', 2 * $tax * $value; # integer
	$twice_cents += 1 & $twice_cents;  # next even
	$value += $twice_cents / 200;

I have not thoroughly tested this, but seems to work for this example.

I do have the gut feeling that I'm rounding off twice, i.e. rounding off
a rounded off number. That's a nono, because if you round in the same
direction twice, you'll round up in  the wrong direction. Say I round
0.3 to 0.5, and then round 0.5 to 1? Anyway, the tax estimation will
always be rounded up, you'll never get a too low figure. I think.

   HTH,
   Bart.


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

Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.

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 V9 Issue 4330
**************************************


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