[8951] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2569 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue May 12 16:09:59 1998

Date: Tue, 12 May 98 13:01:58 -0700
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Tue, 12 May 1998     Volume: 8 Number: 2569

Today's topics:
    Re: Msql-Perl (brian d foy)
    Re: Perl for Non-programmers (was Re: Does Perl have a  (Mark-Jason Dominus)
    Re: Perl for Non-programmers (was Re: Does Perl have a  <merlyn@stonehenge.com>
    Re: Perl for Non-programmers (was Re: Does Perl have a  <tchrist@mox.perl.com>
    Re: Perl for Non-programmers (was Re: Does Perl have a  <tchrist@mox.perl.com>
    Re: Perl script on Internet Information Server <upsetter@shore.net>
    Re: Question about regular expression in perl script (M.J.T. Guy)
        regexp for strings of chars <lhorow@corp.idt.net>
    Re: removing last line from file <franzen@pmel.noaa.gov>
    Re: removing nested parentheses (M.J.T. Guy)
    Re: Scanning Perl scripts for Y2K problems <angst@scrye.com>
    Re: Scanning Perl scripts for Y2K problems (Stuart McDow)
    Re: Set Difference Problem! <e78199@ceng.metu.edu.tr>
    Re: Set Difference Problem! (Allan M. Due)
    Re: Set Difference Problem! (John Porter)
    Re: Significant digit? (brian d foy)
    Re: Significant digit? <jkry3025@comenius.ms.mff.cuni.cz>
    Re: Significant digit? (Craig Berry)
    Re: Small Perl quick-reference? <dcameron@bcs.org.uk>
    Re: What happenned to all the 1's. <info@purco.qc.ca>
        Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)

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

Date: Tue, 12 May 1998 14:20:12 -0400
From: comdog@computerdog.com (brian d foy)
Subject: Re: Msql-Perl
Message-Id: <comdog-ya02408000R1205981420120001@news.panix.com>
Keywords: from just another new york perl hacker

In article <355883B6.47BB@xures.ei.uvigo.es>, agro@xures.ei.uvigo.es posted:

>        Once that I have established a conexion to a msql database from a perl
>scritp with the command
>        $dbh=Msql->connect($hostname,$databasename);
>is there any command to close the conexion??????

if you are using the Msql.pm module (not the DBI one), the connection
should close when it is no longer used:

from the man page (perldoc Msql)

     No close statement

     Whenever the scalar that holds a database or statement
     handle loses its value, Msql chooses the appropriate action
     (frees the result or closes the database connection). So if
     you want to free the result or close the connection, choose
     to do one of the following:

     undef the handle

     use the handle for another purpose

     let the handle run out of scope

     exit the program.


this is not the same for the DBI package though, so see the docs 
for the DBD::mSQL driver as well as the general DBI docs for the
details.

good luck :)

-- 
brian d foy                                  <comdog@computerdog.com>
CGI Meta FAQ <URL:http://computerdog.com/CGI_MetaFAQ.html>
Comprehensive Perl Archive Network (CPAN) <URL:http://www.perl.com>
Perl Mongers <URL:http://www.pm.org>


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

Date: 12 May 1998 14:17:51 -0400
From: mjd@op.net (Mark-Jason Dominus)
Subject: Re: Perl for Non-programmers (was Re: Does Perl have a IDE?I don't like command line.
Message-Id: <6ja3of$abb$1@monet.op.net>
Keywords: acquisitive Elisabeth replaceable twinge


In article <6j9t37$10i$2@info.uah.edu>, Greg Bacon <gbacon@cs.uah.edu> wrote:
>You could probably get away with it grandly (many universities use C and
>even *shudder* Java as teaching languages), provided that you're willing
>to lie to your students initially and then uncover your lies where
>appropriate.

I agree.  I taught university C, and it went just fine.  I think that
the biggest problem with most university prorgramming courses is that
they're taught by people who don't know much about the languages or
even about programming.

I had a prof once who said that teaching was an art of telling lies;
you say, ``Well, it's like this...'' and then when the student is
comfortable with that, you say ``Well, actually, it isn't really like
that; it's more like this instead...''  That made a big impression on me.  


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

Date: Tue, 12 May 1998 18:40:25 GMT
From: Randal Schwartz <merlyn@stonehenge.com>
Subject: Re: Perl for Non-programmers (was Re: Does Perl have a IDE?I don't like command line.
Message-Id: <8c7m3r9vv4.fsf@gadget.cscaper.com>

>>>>> "Mark-Jason" == Mark-Jason Dominus <mjd@op.net> writes:

Mark-Jason> I had a prof once who said that teaching was an art of
Mark-Jason> telling lies; you say, ``Well, it's like this...'' and
Mark-Jason> then when the student is comfortable with that, you say
Mark-Jason> ``Well, actually, it isn't really like that; it's more
Mark-Jason> like this instead...''  That made a big impression on me.

I say that on slide one of day one of the classes I teach nearly every
week.  The important thing is to lie with confidence, but remember
where you are lying. :-)

It's also important when you are training other instructors to let
*them* know where you are lying... I forgot once, and it cost someone
else a bit of embarassment. :-) (Sorry!)

print "Just another Perl hacker," # but not what the media calls "hacker!" :-)
## legal fund: $20,990.69 collected, $186,159.85 spent; just 111 more days
## before I go to *prison* for 90 days; email fund@stonehenge.com for details

-- 
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying
Email: <merlyn@stonehenge.com> Snail: (Call) PGP-Key: (finger merlyn@teleport.com)
Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me


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

Date: 12 May 1998 19:37:47 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Perl for Non-programmers (was Re: Does Perl have a IDE?I don't like command line.
Message-Id: <6ja8eb$g2k$4@csnews.cs.colorado.edu>

 [courtesy cc of this posting sent to cited author via email]

In comp.lang.perl.misc, mwmaurer@mtu.edu (Mark Maurer) writes:
:I think it could be wise to throw Perl in with C as a language NOT to teach
:to an aspiring programmer first...

That depends on how it's taught.  Certainly the crux of becoming a programmer
is developing the algorithmic mentality.  Worrying about memory management,
strong typing, and other idiocies interferes with that process.

--tom
-- 
    last|perl -pe '$_ x=/(..:..)...(.*)/&&"'$1'"ge$1&&"'$1'"lt$2'
    That's gonna be tough for Randal to beat...  :-)
            --Larry Wall in  <1991Apr29.072206.5621@jpl-devvax.jpl.nasa.gov>


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

Date: 12 May 1998 19:36:31 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: Perl for Non-programmers (was Re: Does Perl have a IDE?I don't like command line.
Message-Id: <6ja8bv$g2k$3@csnews.cs.colorado.edu>

 [courtesy cc of this posting sent to cited author via email]

In comp.lang.perl.misc, Art Cohen <upsetter@shore.net> writes:
:part of the problem is that Perl gives you all sorts of useful
:shortcuts that programmers tend to want but which might overwhelm a
:non-programmer. 

That's right.  Perl was designed to make the programmer's life 
easier.  It does that magnificently.  But nothing can make a 
nonprogrammer's life easier other than hiring themself a programmer.

--tom
-- 
: 1.  What is the possibility of this being added in the future?
In the near future, the probability is close to zero.  In the distant
future, I'll be dead, and posterity can do whatever they like...  :-) --lwall


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

Date: 12 May 1998 18:53:00 GMT
From: Art Cohen <upsetter@shore.net>
Subject: Re: Perl script on Internet Information Server
Message-Id: <6ja5qc$6ui@fridge.shore.net>

Silvana Gsmez <silvana@ptg.es> wrote:
: I have NT 4.O and IIS v. 2.0 and I want executing perl scripts from my
: html pages but I can4t. I don4t know how to do it. Do I have to
: configure IIS to execute perl scripts?

Yes. Yes you do. Any and all web servers must be configured to run any CGI
script, whether written in perl or some other language. The docs to IIS
should tell you how to do this.

You'll probably find a lot more information from newsgroups, books or
websites devoted to CGI (as opposed to perl). 

--Art

National Ska/Reggae Calendar: www.ziplink.net/~upsetter/ska/calendar.html
        Boston Ska Home Page: www.ziplink.net/~upsetter/ska/index.html



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

Date: 12 May 1998 18:37:45 GMT
From: mjtg@cus.cam.ac.uk (M.J.T. Guy)
Subject: Re: Question about regular expression in perl script
Message-Id: <6ja4tp$1lk$1@lyra.csx.cam.ac.uk>

Martien Verbruggen <mgjv@comdyn.com.au> wrote:
>In article <355367F1.50903B7E@charlotte.infi.net>,
>	Gary Nielson <gnielson@charlotte.infi.net> writes:
>
>> perl -p -i.bak -e 's/[^x]\.htm/\.x\.htm/' *.htm
>> 
>> That definitely avoids adding a .x to any file that already has a .x
>> before .htm. But now it deletes the last character before the .x. So
>> test.htm becomes tes.x.htm. I am having trouble figuring out how to
>> correct this.
>
>This might also do odd things for file names like: a.htm.b.htm. maybe
>you can anchor the regexp in some way?
>
>Have a look at lookahead assertions in perlre. Something like
>/(?!x)\.htm$/ should do the job. Beware of shells trying to do history
>substitution on the !.

Nope.   That says "check the next character isn't an `x', then check that
that same character _is_ a `.' ... ".   Lookahead doesn't help here  -  you
need negative lookbehind, which won't be available till Perl 5.005.

So in the meantime, I'd do it in two passes  -  remove any previous .x
then re-insert (version deals also with .x.x.htm):

         1 while s/.x.htm/.htm/; s/.htm/.x.htm/g;

If you _really_ want to do it in a single s///, I think the following will
work, but haven't tested it:

           s/([^x]|[^.]x).htm/$1.x.htm/g;


Mike Guy


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

Date: Tue, 12 May 1998 15:16:24 -0400
From: Lee M Horowitz <lhorow@corp.idt.net>
Subject: regexp for strings of chars
Message-Id: <3558A008.13552D3E@corp.idt.net>

I cant help thinking there's a "neat" way to do this, but somehow I cant
think of it...
I'ld like a regexp that matches a string containing a "run" of lets say
THREE  of the SAME
letter or number. Any letter or number would do. In other words xaaabcds
would match
but so would abcxxx or abc111qzd or .... you get it.



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

Date: Tue, 12 May 1998 11:09:34 -0700
From: Nathan Franzen <franzen@pmel.noaa.gov>
Subject: Re: removing last line from file
Message-Id: <Pine.SOL.3.96.980512110115.26128B-100000@corona.pmel.noaa.gov>


On Tue, 12 May 1998, Tom Phoenix wrote:
> On 12 May 1998, Baton wrote:
> > how would i go about removing the last line of a text file?
> 
> You may be able to do this with truncate, possibly in conjunction with
> tell.

I first thought of something along the lines of:

perl -nibak -e 'unless(eof){print} else{undef $^I;print "last: $_"}' fname

However, the "undef $^I" didn't do what I expected, and I had to resort to 

perl -nibak -e 'unless(eof){print} else{undef $^I;print STDOUT "last: $_"}'

to get what I wanted.  The mysteries of $^I are still shrouded to me.
So I wonder: once I have invoked $^I, how do I get rid of it?

Now, my one-line response to baton's question probably isn't what is
desired, since baton probably wants to have user input as to whether the
last line should be deleted or not.  I think more that one line will be
required. 

-Nathan



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

Date: 12 May 1998 18:00:22 GMT
From: mjtg@cus.cam.ac.uk (M.J.T. Guy)
Subject: Re: removing nested parentheses
Message-Id: <6ja2nm$k9$1@lyra.csx.cam.ac.uk>

Ronald J Kimball  <rjk@coos.dartmouth.edu> wrote:
>Ala Qumsieh wrote:
>> 
>> Doug E Fresh wrote:
>> 
>> > [...]
>> > $string = "2*(3+(4+5))"
>> 
>>  Hmmm  ... I suggest you take a closer look at the greediness of matching
>> operators!
>> After you do that, you can try something like this:
>> $string =~ s/.*\((.*)\)?/&process($1)/e;
>
>That will match '(4+5))' in the above string, and set $1 to '4+5))'.  How
>would that be useful, exactly?

Actually, it's worse than that.    Because of the spurious initial .*
it actually matches the whole string.    I expect Ala meant to say

   $string =~ s/\((.*?)\)/&process($1)/e;

where the key character is that `?', which says "find the shortest match"
and therefore in particular one not containing an inner ')'.

But that still isn't right.   In the example, that would match `(3+(4+5)'
and set $1 to `3+(4+'.    You have to match an inner string which doesn't
contain either `(' or `)'.    So you want

   $string =~ s/\(([^()]*)\)/&process($1)/e;

As a general rule, it's better to write explicitly what you don't want
in your string rather than using non-greedy matching, which is both
inefficient (sometimes) and error-prone.


Mike Guy


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

Date: 12 May 1998 18:00:05 GMT
From: angst <angst@scrye.com>
Subject: Re: Scanning Perl scripts for Y2K problems
Message-Id: <6ja2n5$c64$2@jelerak.scrye.com>

Stuart McDow <smcdow@arlut.utexas.edu> wrote:

: localtime is Y2K, sure. But so what? Just because it is doesn't mean
: that its use will me. Scan the script for localtime, and wherever it
: pops up, make sure that (localtime)[5] is being used correctly.

Your third sentence makes absolutely no sense.

This brings up something that's been bothering me...I think UNIX vendors
ought to be expanding their dates by an extra bit or so, to keep the
2038 thing from being a problem.  They ought to do this now, to avoid such
travesties as people making money off of idiots by publishing miriad
Y2K doomsday books, and keep the media from having a field day with
how unix is going to break and the world is going to end 40 years from now.

-- 
Erik Nielsen <eln@rmci.net>
solaris/perl/qmail/dns weenie
this post != views of anyone at all, really
"You are like...unix GOD" -- local tech support


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

Date: 12 May 1998 19:33:35 GMT
From: smcdow@arlut.utexas.edu (Stuart McDow)
Subject: Re: Scanning Perl scripts for Y2K problems
Message-Id: <6ja86f$42i$1@ns1.arlut.utexas.edu>

angst <angst@scrye.com> writes:
> Stuart McDow <smcdow@arlut.utexas.edu> wrote:
> 
> : localtime is Y2K, sure. But so what? Just because it is doesn't mean
> : that its use will me. Scan the script for localtime, and wherever it
> : pops up, make sure that (localtime)[5] is being used correctly.
> 
> Your third sentence makes absolutely no sense.

Oh, sorry, typo. Or maybe complete ambiguity.

Just because it (localtime) is (Y2K) doesn't mean that its
(localtime's) use will be. As we all know, localtime() is as much Y2K
as the OSes' underlying date information API (ie time(2)) is.

What I suggested was a quick-n-dirty, first pass scan to see if a
*script* (not localtime nor perl itself) is Y2K was to

1. look for use of localtime() in the script.
2. check to see how the script writer used the year information given
   by localtime()

$year = (localtime)[5];
EX1: print "The year is ", 1900 + $year, "\n";  # Y2K
EX2: print "The year is 19${year}\n";           # not Y2K

I wouldn't be at all surprised to see, in older legacy code, usage
similar to EX2.

This isn't the only way that a script might not be Y2K, but it seemed
like an obvious "first thing to look at" to me.

--
Stuart McDow                                     Applied Research Laboratories
smcdow@arlut.utexas.edu                      The University of Texas at Austin
            "Look for beauty in roughness, unpolishedness"


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

Date: Tue, 12 May 1998 21:25:11 +0300
From: Walid ALSAQAF <e78199@ceng.metu.edu.tr>
Subject: Re: Set Difference Problem!
Message-Id: <35589407.CF28E942@ceng.metu.edu.tr>

Thanks for the reply. 

 But actually, the output was truly (3,4,5,6) for the difference. But
that is correct for Array1-Array2 but not correct for array2-array which
means the set of elements in array2 and NOT in array1, this in our case
is (6) alone. I can add steps to exclude the elements in array1, but do
you know a better solution?

Allan M. Due wrote:
@array1=(1,2,3,4,5)
@array2=(1,2,6)

> >but when I try to get the @difference of (array2-array1) I get:
> >(3,4,5,6) while I expected (6). How can I modify the code to correct this
> >problem? Please try to email me the reply as well..
> 
> When I run your example I get:
> @union = (1,2,3,4,5,6)
> @difference  = (3,4,5,6)
> @intersection = (1,2)
> 
> Which, to my way of thinking is correct.  My understanding is that the union of
> two sets A and B contains each of the elements contained in either A or B or
> both.  The intersection is the set that consists of each element contained in
> both A and B.  And I would interpret the term difference here to mean the set
> such that  Union = intersection + difference.  Difference then being  those
> elements that are unique to either A or B.  All of which matches the output
> from your code.
> 
> Allan M. Due
> Due@DiscoverNet.Net
> Admin@WhiteCrow.net
> ======================
> perl -e '$_="Just another Perl Hacker";for($i=-11;$i<12;$i++){$d=12-abs($i);
> if(/(^.{$d}).*(.{$d}$)/){$t=(13+$d);printf"%${t}s\n",$1.$2;}}'

-- 
/*********************************************/
Walid ALSAQAF

METU 
Computer Engineering Department
mailto:e78199@ceng.metu.edu.tr
http://www.ceng.metu.edu.tr/~e78199/Walid
ICQ # : (9762042)
/*********************************************/


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

Date: 12 May 1998 19:02:49 GMT
From: due@murray.fordham.edu (Allan M. Due)
Subject: Re: Set Difference Problem!
Message-Id: <6ja6cp$mh1$0@206.165.146.138>

In article <35589407.CF28E942@ceng.metu.edu.tr>, Walid ALSAQAF 
(e78199@ceng.metu.edu.tr) stated...

[snip]

Walid,

	Hmm, first time I saw a reply to an email posted in a newsgroup.  
Anyway,You are not thinking about this correctly.  In this instance order 
is unimportant, the operations are symmetrical.  A Union B = B Union A,  
A intersection B = B intersection A, and A difference B = B difference A.   
Difference here is not subtraction.  This is not arithmetic.  It must 
always be true that A Union B = A intersect B + A difference B.  If they 
were non-symmetrical this property would not hold.

[snip]
>that is correct for Array1-Array2 but not correct for array2-array which
>means the set of elements in array2 and NOT in array1, this in our case
>is (6) alone.

This is not correct.  It may be what you want, but it is not a correct
interpertation of the code.  If you just look at the code you can tell it 
is doing exactally what I outlined above.

HTH

Allan M. Due



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

Date: Tue, 12 May 1998 19:49:41 GMT
From: jdporter@min.net (John Porter)
Subject: Re: Set Difference Problem!
Message-Id: <MPG.fc273885a72c419896b3@news.min.net>

On 12 May 1998 19:02:49 GMT,
in article <6ja6cp$mh1$0@206.165.146.138>,
due@murray.fordham.edu (Allan M. Due) wrote:
> 
> ... In this instance order 
> is unimportant, the operations are symmetrical.  A Union B = B Union A,  
> A intersection B = B intersection A, and A difference B = B difference A.   
> Difference here is not subtraction.  This is not arithmetic.  It must 
> always be true that A Union B = A intersect B + A difference B.  If they 
> were non-symmetrical this property would not hold.
> ...

Sorry, but they're not symmetrical, and that property does not hold for
sets.

As support, here's a quote from page 3 of "Discrete Mathematics", by
Richard Johnsonbaugh; Macmillan, 1986:

	The set
		X - Y = { x | x in X and x not in Y }
	is called the difference (or relative complement). The
	difference X - Y consists of all elements in X that are
	not in Y.

This operation is clearly not symmetric.

John Porter




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

Date: Tue, 12 May 1998 14:05:59 -0400
From: comdog@computerdog.com (brian d foy)
Subject: Re: Significant digit?
Message-Id: <comdog-ya02408000R1205981405590001@news.panix.com>
Keywords: from just another new york perl hacker

In article <Pine.GSO.3.96.980511164241.21974b-100000@user2.teleport.com>, Tom Phoenix <rootbeer@teleport.com> posted:

>On Mon, 11 May 1998, Yong Huang wrote:
>
>> Did somebody write code to extract significant digits out of a number?
>
>The FAQ talks about this. Hope this helps!

actually, i think the problem is a different one than printing a number
with the appropriate number of digits.  for instance, the number 1234 to
two significant digits is 1200.  the conventional use of sprintf and 
friends won't help this.  it looks like it's a problem in rounding, but
it's a bit more involved.

give me a little time to work on a solution - it's something chemists
and such like to think about :)

-- 
brian d foy                                  <comdog@computerdog.com>
CGI Meta FAQ <URL:http://computerdog.com/CGI_MetaFAQ.html>
Comprehensive Perl Archive Network (CPAN) <URL:http://www.perl.com>
Perl Mongers <URL:http://www.pm.org>


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

Date: Tue, 12 May 1998 21:23:04 -0700
From: Jan Krynicky <jkry3025@comenius.ms.mff.cuni.cz>
Subject: Re: Significant digit?
Message-Id: <35592028.60DE@comenius.ms.mff.cuni.cz>

Yong Huang wrote:
> 
> I don't want to reinvent the wheel. Did somebody write code to extract
> significant digits out of a number? I want to say something like
> $newnum=sig_dig($raw,3) where 3 means 3 significant digits and $raw can
> be 0.23, .23, 0.2345, 1234.2, 123456... Hope somebody already
> implemented the sig_dig function. Thanks.
> 
> Note: I didn't find anything by searching in www.perl.com.
> 
> Yong Huang
> Email:yong@shell.com

I hope I understand you well :

#!/usr/bin/perl
@zk = (0, 0.2, 0.23, .238, 0.2345, 1234.2, 123456, 1.46500, 0.00234,
0.001);
#   =>(0, 0.2, 0.23, 0.23, 0.23  , 1200  , 120000, 1.4    , 0.0023 ,
0.001);

sub foo {
 my ($string,$num) = @_;
 $string+=0;
 ( 
  $string =~ s/^(0\.0*.{$num}).*$/$1/
  or
  $string =~ s/^(\d{$num})([^\.]*).*$/$1 . ('0' x length $2)/e
 )
 and return $string;

 $num++;
 $string =~ s/^(.{$num}).*$/$1/;
 return $string;
}

foreach (@zk) {
 print "$_ => ",foo($_,2),"\n\n";
}


#with Interpolation module you may write this:

use Interpolation foo => sub {foo(split /$;/o, $_[0])};
   # http://www.plover.com/~mjd/perl/Interpolation/

print "\nAnd at last 4.526234 => $foo{4.526234,2}\n"

# or with the new version (not yet available)

use Interpolation 'foo:$$->$' => \&foo;

print "\nAnd at last 4.526234 => $foo{4.526234}{2}\n"

__END__


HTH, Jenda


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

Date: 12 May 1998 19:35:39 GMT
From: cberry@cinenet.net (Craig Berry)
Subject: Re: Significant digit?
Message-Id: <6ja8ab$8p7$1@marina.cinenet.net>

brian d foy (comdog@computerdog.com) wrote:
: In article <Pine.GSO.3.96.980511164241.21974b-100000@user2.teleport.com>, Tom Phoenix <rootbeer@teleport.com> posted:
: >On Mon, 11 May 1998, Yong Huang wrote:
: >> Did somebody write code to extract significant digits out of a number?
: >
: >The FAQ talks about this. Hope this helps!
: 
: actually, i think the problem is a different one than printing a number
: with the appropriate number of digits.  for instance, the number 1234 to
: two significant digits is 1200.  the conventional use of sprintf and 
: friends won't help this.  it looks like it's a problem in rounding, but
: it's a bit more involved.
: 
: give me a little time to work on a solution - it's something chemists
: and such like to think about :)

How about this, only tested in a casual way, but seems to do the job:

#!/usr/bin/perl -w
# sigfig - A perl func for significant-figure rounding, and a demo
# of its use.
# Useage: Takes lines containing a value and a desired number of
# sigfigs (e.g., "1.2345 2") from standard input, and sends the
# resulting rounded number (e.g., "1.2") to standard output.

use strict;
use POSIX qw( floor log10 pow );

while (<>) {
  my ($val, $figs) = split;

  print sigfig_round($val, $figs), "\n";
}

sub sigfig_round
{
  my ($val, $figs) = @_;

  return 0 if $val == 0;

  my $sign = $val < 0 ? -1 : 1;

  $val = abs $val;

  my $exp       = floor(log10($val)) + 1;
  my $expFactor = pow(10, $exp);
  my $abcissa   = $val / $expFactor;
  my $trunc     = sprintf "%.*f", $figs, $abcissa;

  return $sign * $trunc * $expFactor;
}


---------------------------------------------------------------------
   |   Craig Berry - cberry@cinenet.net
 --*--    Home Page: http://www.cinenet.net/users/cberry/home.html
   |      Member of The HTML Writers Guild: http://www.hwg.org/   
       "Every man and every woman is a star."


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

Date: 12 May 1998 19:37:49 GMT
From: "Duncan Cameron" <dcameron@bcs.org.uk>
Subject: Re: Small Perl quick-reference?
Message-Id: <01bd7ddd$4e8f4ae0$cb3663c3@dns.btinternet.com>

The Quick Reference Guide by Johan Vromans is available as both Postscript
and .pdf files.  You can print it as double sided and it comes out as a
neat 32 page booklet. Look through CPAN to find them.

It's here in front of me!

HTH

Duncan Cameron

Allan M. Due <due@murray.fordham.edu> wrote in article
<6ja1tk$eqb$0@206.165.146.138>...
>     Kevin Reid wrote in message
> <1d8x34q.7tbnvcp7mjysN@slip166-72-108-231.ny.us.ibm.net>...
> 
>  [snip]
>     I like "Perl 5 Desktop Reference" by Johan Vromans, OReilly and
> Associates (list:$6.95).  Or not quite as good, slightly more detail, and
> way too expensive:  "Perl 5 Quick Reference" by Micheal O Foghlu, Que
> Corporation (list $19.95).  Just a couple.
> 
> Allan M. Due



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

Date: Tue, 12 May 1998 19:52:43 GMT
From: Leon Stepanian <info@purco.qc.ca>
Subject: Re: What happenned to all the 1's.
Message-Id: <3558A974.D6F24C9D@purco.qc.ca>



Tom Phoenix wrote:

> On Tue, 12 May 1998, Leon Stepanian wrote:
>
> > open(MAINFILE, "$basedir/accespage.txt");
>
> Even when your script is "just an example" (and perhaps especially in that
> case!) you should _always_ check the return value after opening a file.
>
> > @mainpage=split(s/SCBDOPL/$FORM{accesscode}/, $_);
>
> I don't think that's what you want. Check the docs for split. Hope this
> helps!

If I am correct, the above statement I am using takes the file
$basedir/accespage.txt through <MAINFILE> splits each line of text into
@mainpage, and during the split, if it recognises a text string SCBDOPL, it
replaces it with the value in $FORM{accesscode}. This is working fine but and
my return html is perfect except for one problem. Any time $FORM{accesscode}
has a value containing a 1 (integer) such as;
12 prints 2
112 prints 2
11212 prints 22
12121212 prints 2222  and so on. The 1's are being removed from the value
before they are replaced.
But when the value in $FROM{accesscode} equals any other number without a 1 in
it, no problem, and there is also no problem when the value for
$FORM{accesscode} is text.

I have tried many variations but no luck. Maybe there is an inherent problem
in Perl or maybe the split I am doing by looking for SCBDOPL in the html text
$basedir/accespage.txt, when this becomes true, split is returning a 1 to then
purge off the 1 value off the value in $FROM{accesscode} before it is returned
to @mainpage. But I though the above statement is very straightforward and
should not lend itself to any other interpretations by Perl. I'm still
stumped, but I'll keep at it.

Leon Stepanian



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

Date: 8 Mar 97 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 8 Mar 97)
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.  

To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.

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.

The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.

The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.

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 V8 Issue 2569
**************************************

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