[28848] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 92 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Feb 1 03:06:29 2007

Date: Thu, 1 Feb 2007 00:05:10 -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           Thu, 1 Feb 2007     Volume: 11 Number: 92

Today's topics:
        [ANNOUNCE] Emacs modules for Perl programming (Jari Aalto+mail.perl)
        check in mysql DB <hpbenton@gmail.com>
    Re: check in mysql DB <nobull67@gmail.com>
    Re: How to start some file as Perl script argument? <bik.mido@tiscalinet.it>
    Re: How to start some file as Perl script argument? <john@castleamber.com>
    Re: HTML:Parser how to remove "//<![CDATA[ ... //]]>" ? sl123@netherlands.area
    Re: HTML:Parser how to remove "//<![CDATA[ ... //]]>" ? <uri@stemsystems.com>
    Re: Memory leak with XMLRPC and Perl... <john@castleamber.com>
        new CPAN modules on Thu Feb  1 2007 (Randal Schwartz)
    Re: Passing results from sub class of html::parser <bik.mido@tiscalinet.it>
    Re: Passing results from sub class of html::parser <bik.mido@tiscalinet.it>
    Re: Passing results from sub class of html::parser <bik.mido@tiscalinet.it>
    Re: Passing results from sub class of html::parser (NOSPAM)
    Re: Regular Expression help! (NOSPAM)
        round numbers with a caveat? <lance@augustmail.com>
    Re: round numbers with a caveat? <watsonbladd@gmail.com>
    Re: round numbers with a caveat? <purlgurl@purlgurl.net>
    Re: round numbers with a caveat? anno4000@radom.zrz.tu-berlin.de
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 01 Feb 2007 05:29:30 GMT
From: <jari.aalto@poboxes.com> (Jari Aalto+mail.perl)
Subject: [ANNOUNCE] Emacs modules for Perl programming
Message-Id: <perl-faq/emacs-lisp-modules_1170307742@rtfm.mit.edu>

Archive-name: perl-faq/emacs-lisp-modules
Posting-Frequency: 2 times a month
Maintainer: Jari Aalto A T cante net

Announcement: "What Emacs lisp modules can help with programming Perl"

    Preface

        Emacs is your friend if you have to do anything comcerning software
        development: It offers plug-in modules, written in Emacs lisp
        (elisp) language, that makes all your programmings wishes come
        true. Please introduce yourself to Emacs and your programming era
        will get a new light.

    Where to find Emacs/XEmacs

        o   Unix:
            http://www.gnu.org/software/emacs/emacs.html
            http://www.xemacs.org/

        o   Unix Windows port (for Unix die-hards):
            install http://www.cygwin.com/  which includes native Emacs 21.x.
            and XEmacs port

        o   Pure Native Windows port
            http://www.gnu.org/software/emacs/windows/ntemacs.html
            ftp://ftp.xemacs.org/pub/xemacs/windows/setup.exe

        o   More Emacs resources at
            http://tiny-tools.sourceforge.net/  => Emacs resource page

Emacs Perl Modules

    Cperl -- Perl programming mode

        http://math.berkeley.edu/~ilya/software/emacs/
        by Ilya Zakharevich

        CPerl is major mode for editing perl files. Also included in
        latest Emacs, but newest version is at Ilya's site. Note that
        the directrory at CPAN is out of date:
        http://www.cpan.org/modules/by-authors/id/ILYAZ/cperl-mode/

        Compared to default `perl-mode' that comes with Emacs, this
        one has more features.

    TinyPerl -- Perl related utilities

        http://tiny-tools.sourceforge.net/

        If you ever wonder how to deal with Perl POD pages or how to find
        documentation from all perl manpages, this package is for you.
        Couple of keystrokes and all the documentaion is in your hands.

        o   Instant function help: See documentation of `shift', `pop'...
        o   Show Perl manual pages in *pod* buffer
        o   Grep through all Perl manpages (.pod)
        o   Follow POD references e.g. [perlre] to next pod with RETURN
        o   Coloured pod pages with `font-lock'
        o   Separate `tiperl-pod-view-mode' for jumping topics and pages
            forward and backward in *pod* buffer.

        o   Update `$VERSION' variable with YYYY.MMDD on save.
        o   Load source code into Emacs, like Devel::DProf.pm
        o   Prepare script (version numbering) and Upload it to PAUSE
        o   Generate autoload STUBS (Devel::SelfStubber) for you
            Perl Module (.pm)

    TinyIgrep -- Perl Code browsing and easy grepping

        [TinyIgrep is included in Tiny Tools Kit]

        To grep from all installed Perl modules, define database to
        TinyIgrep. There is example file emacs-rc-tinyigrep.el that shows
        how to set up dattabases for Perl5, Perl4 whatever you have
        installed

        TinyIgrep calls Igrep.el to to do the search, You can adjust
        recursive grep options, set search case sensitivity, add user grep
        options etc.

        You can find latest `igrep.el' module at
        <http://groups.google.com/groups?group=gnu.emacs.sources> The
        maintainer is Jefin Rodgers <kevinr@ihs.com>.

    TinyCompile -- To Browse grep results in Emacs *compile* buffer

        TinyCompile is a minor mode for *compile* buffer from where
        you can collapse unwanted lines or shorten file URLs:

            /asd/asd/asd/asd/ads/as/da/sd/as/as/asd/file1:NNN: MATCHED TEXT
            /asd/asd/asd/asd/ads/as/da/sd/as/as/asd/file2:NNN: MATCHED TEXT

            -->

            cd /asd/asd/asd/asd/ads/as/da/sd/as/as/asd/
            file1:NNN: MATCHED TEXT
            file1:NNN: MATCHED TEXT

End



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

Date: 31 Jan 2007 19:48:44 -0800
From: "PB0711" <hpbenton@gmail.com>
Subject: check in mysql DB
Message-Id: <1170301724.587584.9090@s48g2000cws.googlegroups.com>

Hello all ,

First THX. Second I have a program that looks through a file
extracting data and then puts it into a database. I thought I had
designed a nice check with the select * from table where name = $name
and number = $num; . Then I had an if statement to see if that
reported anything but of course it did cus sql gives the nothing found
stuff. I know this is pretty easy but I cannot think of a way to do.
So can someone suggest a way to check if the entry already exist.

Thank you,

PB



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

Date: 31 Jan 2007 23:33:03 -0800
From: "Brian McCauley" <nobull67@gmail.com>
Subject: Re: check in mysql DB
Message-Id: <1170315183.201175.228570@a75g2000cwd.googlegroups.com>

On Feb 1, 3:48 am, "PB0711" <hpben...@gmail.com> wrote:
> Hello all ,
>
> First THX. Second I have a program that looks through a file
> extracting data and then puts it into a database. I thought I had
> designed a nice check with the select * from table where name = $name
> and number = $num; . Then I had an if statement to see if that
> reported anything but of course it did cus sql gives the nothing found
> stuff.

Please produce a minimal but complete script illustrate what you mean
by "sql gives the nothing found stuff."

If you try to read one row and there isn't one then there wasn't one.

my $sth = $db->prepare('select * from table where name = ? and number
= ?');
$sth->execute($name,$num);
my $exists = !!$sth->fetchrow_arrayref;
$sth->finish;

unless ($exist) {
  #....
}

You can simplify this using DBI's convenience method
selectrow_arrayref

my $exists = !!$db->selectrow_arrayref('select * from table where name
= ? and number = ?',{},$name,$num);

Alternatively count the rows:

my $rows = $db->selectrow_array('select count(*) from table where name
= ? and number = ?',{},$name,$num);

> I know this is pretty easy but I cannot think of a way to do.

There are many more.



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

Date: Thu, 01 Feb 2007 00:10:52 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: How to start some file as Perl script argument?
Message-Id: <n062s2h3pc4pruaahuh68ha9a233u8kp1r@4ax.com>

On Wed, 31 Jan 2007 20:43:29 +0100, "max" <max@xxx.tovle.ct> wrote:

>No! I want start perl script with XLS document as argument.
>Start in CMD with typing perl script.pl someexcel.xls is normaly work,
>I want star it with click on Excel document, How to do it in Windows OS?

Sorry but I have some troubles parsing your English. Do you mean that
you want excel files -as defined by the .xls extension- to be opened
by some perl script of yours when you double click on them? This is
not meant as an offence to you. I'm not a native English speaker
either. Can you just confirm what I wrote? If so then your question
has *nothing* to do with Perl and *everything* with how to set up
associations in Windows. I may tell you how to do it, but the only
Windows (XP) version I have available is in Italian so I'm freely
translating into English and I don't know how reliable this would be.
However you should explore "your computer's resources" and select
"Folder Options" from some menu. From there, go to "File types": in
this dialog you can either modify existing associations or add new
ones (many of us do so for wperl.exe).


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: 1 Feb 2007 02:10:09 GMT
From: John Bokma <john@castleamber.com>
Subject: Re: How to start some file as Perl script argument?
Message-Id: <Xns98C9CD2BF9912castleamber@130.133.1.4>

Tad McClellan <tadmc@augustmail.com> wrote:

> You cannot "start" an Excel file because it is not executable.

start foo.xls

:-D

-- 
John                Experienced Perl programmer: http://castleamber.com/

          Perl help, tutorials, and examples: http://johnbokma.com/perl/


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

Date: Wed, 31 Jan 2007 08:48:14 -0800
From: sl123@netherlands.area
Subject: Re: HTML:Parser how to remove "//<![CDATA[ ... //]]>" ?
Message-Id: <00i1s2t208hnojivihnqou85ol3g8jjjne@4ax.com>

On 31 Jan 2007 12:51:21 GMT, anno4000@radom.zrz.tu-berlin.de wrote:

>Gerwin <g.h.vandoorn@gmail.com> wrote in comp.lang.perl.misc:
>> On Jan 31, 12:01 pm, "Gerwin" <g.h.vando...@gmail.com> wrote:
>> > Hi,
>> >
>> > I'm using HTML::Parser to strip HTML tags from my files. I noticed
>> > how //<![cdata[ ... //]]> and the javascript between that is not
>> > stripped. Any idea how to do this?
>> >
>> > -Gerwin
>> 
>> Well i made a regex to do it:
>> 
>> $content =~ s/(\/\/<!\[.*\/\/]]>)//;
>> 
>> Is this efficient? If not, what is?
>
>Why do you think efficiency matters?
>
>At this point you should be concerned with effectiveness: Does it
>match what it is supposed to match, no more and no less?  Since I
>don't know the variability of the pattern I can't tell.  The fact
>you are matching only one opening "[" but two closing "]" is a bit
>dubious.  Shouldn't the string "cdata" be checked somewhere?
>
>Worry about efficiency when your program turns out to be slow.
>If that happens, I dare say it won't be this regex that is
>responsible.
>
>Anno
Use ROBIC0's RxParse, it handles CDATA correctly


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

Date: Wed, 31 Jan 2007 23:50:30 -0500
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: HTML:Parser how to remove "//<![CDATA[ ... //]]>" ?
Message-Id: <x7fy9qo50p.fsf@mail.sysarch.com>

>>>>> "s" == sl123  <sl123@netherlands.area> writes:

  s> Use ROBIC0's RxParse, it handles CDATA correctly

you must either be kidding or an alter moron ego of his. his module is
horrible in too many ways to count.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: 1 Feb 2007 02:07:32 GMT
From: John Bokma <john@castleamber.com>
Subject: Re: Memory leak with XMLRPC and Perl...
Message-Id: <Xns98C9CCBA04FF1castleamber@130.133.1.4>

findingAri@gmail.com wrote:

> Hello,
> I wrote a program in Perl (using version 5.8.4), that using XMLRPC to
> communicate between the client and the server. The call from the
> client to the server sitting in a infinite loop that can be run in
> random inteval time.
> After sometime that the client is running the perl's proccess taking
> ~70MB of ram and ~100% of the CPU usage.
> May you know why?

My crystal ball just gave an out of memory. I am afraid it's a conspiracy.

-- 
John                Experienced Perl programmer: http://castleamber.com/

          Perl help, tutorials, and examples: http://johnbokma.com/perl/


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

Date: Thu, 1 Feb 2007 05:42:18 GMT
From: merlyn@stonehenge.com (Randal Schwartz)
Subject: new CPAN modules on Thu Feb  1 2007
Message-Id: <JCrruI.Gz2@zorch.sf-bay.org>

The following modules have recently been added to or updated in the
Comprehensive Perl Archive Network (CPAN).  You can install them using the
instructions in the 'perlmodinstall' page included with your Perl
distribution.

AI-MegaHAL-0.04_02
http://search.cpan.org/~chorny/AI-MegaHAL-0.04_02/
Perl interface to the MegaHAL natural language conversation simulator.
----
Bundle-PDF-0.01
http://search.cpan.org/~ski/Bundle-PDF-0.01/
A bundle to install PDF modules and dependencies
----
CGI-Ajax-0.701
http://search.cpan.org/~bct/CGI-Ajax-0.701/
a perl-specific system for writing Asynchronous web applications
----
CGI-Session-Plugin-Redirect-1.01
http://search.cpan.org/~makoto/CGI-Session-Plugin-Redirect-1.01/
----
CPAN-1.88_72
http://search.cpan.org/~andk/CPAN-1.88_72/
query, download and build perl modules from CPAN sites
----
Catalyst-Plugin-Session-0.14
http://search.cpan.org/~mramberg/Catalyst-Plugin-Session-0.14/
Generic Session plugin - ties together server side storage and client side state required to maintain session data.
----
Class-Workflow-0.01
http://search.cpan.org/~nuffin/Class-Workflow-0.01/
Light weight workflow system.
----
Class-Workflow-0.02
http://search.cpan.org/~nuffin/Class-Workflow-0.02/
Light weight workflow system.
----
Class-Workflow-0.03
http://search.cpan.org/~nuffin/Class-Workflow-0.03/
Light weight workflow system.
----
Class-Workflow-0.04
http://search.cpan.org/~nuffin/Class-Workflow-0.04/
Light weight workflow system.
----
Hook-Output-File-0.01
http://search.cpan.org/~schubiger/Hook-Output-File-0.01/
Redirect STDIN/STDOUT to a file
----
Hook-Output-File-0.02
http://search.cpan.org/~schubiger/Hook-Output-File-0.02/
Redirect STDOUT/STDERR to a file
----
MP3-PodcastFetch-1.02
http://search.cpan.org/~lds/MP3-PodcastFetch-1.02/
Fetch and manage a podcast subscription
----
MoCo-0.04
http://search.cpan.org/~jkondo/MoCo-0.04/
Light & Fast Model Component
----
Net-ARP-1.0
http://search.cpan.org/~crazydj/Net-ARP-1.0/
Perl extension for creating ARP packets
----
Net-Ping-External-0.12_02
http://search.cpan.org/~chorny/Net-Ping-External-0.12_02/
Cross-platform interface to ICMP "ping" utilities
----
Net-SIP-0.16
http://search.cpan.org/~sullr/Net-SIP-0.16/
Framework SIP (Voice Over IP, RFC3261)
----
POE-Component-IRC-5.19
http://search.cpan.org/~bingos/POE-Component-IRC-5.19/
a fully event-driven IRC client module.
----
POE-Component-IRC-5.20
http://search.cpan.org/~bingos/POE-Component-IRC-5.20/
a fully event-driven IRC client module.
----
Parse-Eyapp-1.06552
http://search.cpan.org/~casiano/Parse-Eyapp-1.06552/
Extensions for Parse::Yapp
----
Tripletail-0.23
http://search.cpan.org/~hio/Tripletail-0.23/
Tripletail, Framework for Japanese Web Application
----
XML-LibXML-Tools-1.00
http://search.cpan.org/~sinister/XML-LibXML-Tools-1.00/
An API for easy XML::LibXML DOM manipulation
----
XML-Parser-Lite-0.66
http://search.cpan.org/~hex/XML-Parser-Lite-0.66/
Lightweight regexp-based XML parser
----
XML-XPathScript-1.49
http://search.cpan.org/~yanick/XML-XPathScript-1.49/
a Perl framework for XML stylesheets
----
basis-0.03
http://search.cpan.org/~sknpp/basis-0.03/
use base with import call
----
onto-perl-0.26
http://search.cpan.org/~easr/onto-perl-0.26/


If you're an author of one of these modules, please submit a detailed
announcement to comp.lang.perl.announce, and we'll pass it along.

This message was generated by a Perl program described in my Linux
Magazine column, which can be found on-line (along with more than
200 other freely available past column articles) at
  http://www.stonehenge.com/merlyn/LinuxMag/col82.html

print "Just another Perl hacker," # the original

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


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

Date: Thu, 01 Feb 2007 00:24:42 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Passing results from sub class of html::parser
Message-Id: <d692s2deea4hlbj2t0f8hiupt1vqcv2k05@4ax.com>

On 31 Jan 2007 10:21:04 -0800, "Gerwin" <g.h.vandoorn@gmail.com>
wrote:

>Hmm well the "text" sub routine is called and i figured that if i
>stored the parsed text in an extra variable, you could retrieve it
>with that get sub routine. I do not know what the best method is for

Indeed you *could* retrieve it if you *did* store it in the "extra
variable", which is what the text() method is supposed to do. But...
maybe it's just me, but I plainly can't see where it is called,
contrary to your claim that it is. Can you re-paste your code adding a
comment to indicate where this call is supposed to happen?

>doing this. I just find it weird that assigning a string to "$vars-
>>{'text_output'} = "bladiebla";" will return "bladiebla" but assigning
>the value "$text" doesn't. Who can shine a light on this?

Well, it should, in the meantime I spotted another problem with your
code, that I had overlooked in the first place, and which I'm pointing
out in a separate thread.


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: Thu, 01 Feb 2007 00:24:44 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Passing results from sub class of html::parser
Message-Id: <p592s2l630o037pttgoia03qb8jofdd1va@4ax.com>

On 31 Jan 2007 06:06:35 -0800, "Gerwin" <g.h.vandoorn@gmail.com>
wrote:

>my %vars = (
>   text_output         => undef
>);

Here you have %vars, which is a *hash*.
>
>sub text {
>	my ($self, $text) = @_;
>	$vars->{'text_output'} = $text;

Here you have $vars, which is scalar, that you treat as a *hashref*.


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: Thu, 01 Feb 2007 00:25:32 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Passing results from sub class of html::parser
Message-Id: <ia92s2t7d2d24f0u45autkr70d26be4hbj@4ax.com>

On 31 Jan 2007 10:21:04 -0800, "Gerwin" <g.h.vandoorn@gmail.com>
wrote:

>Hmm well the "text" sub routine is called and i figured that if i
>stored the parsed text in an extra variable, you could retrieve it
>with that get sub routine. I do not know what the best method is for

Indeed you *could* retrieve it if you *did* store it in the "extra
variable", which is what the text() method is supposed to do. But...
maybe it's just me, but I plainly can't see where it is called,
contrary to your claim that it is. Can you re-paste your code adding a
comment to indicate where this call is supposed to happen?

>doing this. I just find it weird that assigning a string to "$vars-
>>{'text_output'} = "bladiebla";" will return "bladiebla" but assigning
>the value "$text" doesn't. Who can shine a light on this?

Well, it should, in the meantime I spotted another problem with your
code, that I had overlooked in the first place, and which I'm pointing
out in a separate thread.


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: Wed, 31 Jan 2007 23:56:31 -0600
From: "Mumia W. (NOSPAM)" <paduille.4060.mumia.w+nospam@earthlink.net>
Subject: Re: Passing results from sub class of html::parser
Message-Id: <eps0dt$rac$1@aioe.org>

On 01/31/2007 08:06 AM, Gerwin wrote:
> I'm trying to sub class HTML::Parser and store the parsed result in a
> variable so I can access it again from outside the sub class. In the
> code below the "text" method would print $text AND if I would say
> "$vars->{'text_output'} = "test text";"  it would also show "test
> text" when calling "print $parser->getTextOutput();" but it will not
> print the $text when saying "$vars->{'text_output'} = $text;"
> 
> What am I doing wrong here?
> 
> package MyParser;
> use base qw(HTML::Parser);
> 
> my %vars = (
>    text_output         => undef
> );
> 
> sub text {
> 	my ($self, $text) = @_;
> 	$vars->{'text_output'} = $text;
> }
> 
> sub getTextOutput {
> 	return $vars->{'text_output'};

This is the wrong syntax. "Vars" is not a hash reference; it's just a hash:
	return $vars{'text_output'};


> }
> [...]


HTH


-- 
Windows Vista and your freedom in conflict:
http://techdirt.com/articles/20061019/102225.shtml


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

Date: Wed, 31 Jan 2007 23:58:58 -0600
From: "Mumia W. (NOSPAM)" <paduille.4060.mumia.w+nospam@earthlink.net>
Subject: Re: Regular Expression help!
Message-Id: <eps0dv$rac$2@aioe.org>

On 01/31/2007 10:10 AM, Suk wrote:
> Sorry I meant  -
> 
> open CSVFILE, "data.csv" or die "failed to open changes $!";
> 
> while (<CSVFILE>) {
> 
>         print "$`$3/$2/$1$'" if (/(\d{2})\/(\d{2})\/(\d{4})/);
> 
> }
> 
> Which just alters the first date- I need to alter all of them
> 

It sounds like you want to use the substitution operator with its 
"global" option: s///g


HTH


-- 
Windows Vista and your freedom in conflict:
http://techdirt.com/articles/20061019/102225.shtml


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

Date: Thu, 01 Feb 2007 02:31:19 GMT
From: Lance Hoffmeyer <lance@augustmail.com>
Subject: round numbers with a caveat?
Message-Id: <Xhcwh.37889$uC6.12911@trnddc02>

Hey all,

I want to round numbers with one decimal place.
I know one can round in this manner:

sub round {
    my($value) = shift;
    return int($value + .5);
}


What I now need to add to this if that if a number is exactly
??.5 and is an even number then round down (i.e. 22.5 = 22)
and if a number is odd and ??.5 then round up (i.e. 21.5 = 22).

How can I test whether a number is even or odd?

sub round {
if ($value = "??.5 and even){ $value = $value - .1};
if ($value = "??.5 and odd"){ $value = $value + .1};

    my($value) = shift;
    return int($value + .5);
}


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

Date: 31 Jan 2007 18:45:24 -0800
From: "watsonbladd@gmail.com" <watsonbladd@gmail.com>
Subject: Re: round numbers with a caveat?
Message-Id: <1170297924.245881.201980@s48g2000cws.googlegroups.com>

On Jan 31, 9:31 pm, Lance Hoffmeyer <l...@augustmail.com> wrote:
> Hey all,
>
> I want to round numbers with one decimal place.
> I know one can round in this manner:
>
> sub round {
>     my($value) = shift;
>     return int($value + .5);
>
> }
>
> What I now need to add to this if that if a number is exactly
> ??.5 and is an even number then round down (i.e. 22.5 = 22)
> and if a number is odd and ??.5 then round up (i.e. 21.5 = 22).
>
> How can I test whether a number is even or odd?
>
> sub round {
> if ($value = "??.5 and even){ $value = $value - .1};
> if ($value = "??.5 and odd"){ $value = $value + .1};
>
>     my($value) = shift;
>     return int($value + .5);
>
> }

$int%2 is 0 for even, 1 for odd.



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

Date: Wed, 31 Jan 2007 18:49:56 -0800
From: Purl Gurl <purlgurl@purlgurl.net>
Subject: Re: round numbers with a caveat?
Message-Id: <45C15554.3090509@purlgurl.net>

Lance Hoffmeyer wrote:

> What I now need to add to this if that if a number is exactly
> ??.5 and is an even number then round down (i.e. 22.5 = 22)
> and if a number is odd and ??.5 then round up (i.e. 21.5 = 22).

> How can I test whether a number is even or odd?

if (int ($value) % 2 == 0)
  { print "even"; }
else
  { print "odd"; }

Purl Gurl



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

Date: 1 Feb 2007 06:59:13 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: round numbers with a caveat?
Message-Id: <52dhe1F1o9khrU1@mid.dfncis.de>

Lance Hoffmeyer  <lance@augustmail.com> wrote in comp.lang.perl.misc:
> Hey all,
> 
> I want to round numbers with one decimal place.
> I know one can round in this manner:
> 
> sub round {
>     my($value) = shift;
>     return int($value + .5);
> }

That works for positive values, but not for negative ones.
The normal way to round numbers in Perl is sprintf:

    sprintf '%.0f', $value;

rounds to an integer.  See also "perldoc -q round".

> What I now need to add to this if that if a number is exactly
> ??.5 and is an even number then round down (i.e. 22.5 = 22)
> and if a number is odd and ??.5 then round up (i.e. 21.5 = 22).

That is the standard and sprintf works this way.

    for my $x ( -1.5, -0.5, 0.5, 1.5, 2.5, 3.5 ) {
        my $y = sprintf'%.0f', $x;
        print "$x -> $y\n";
    }

> How can I test whether a number is even or odd?

You don't have to for the purpose of rounding.

Besides taking the number mod 2, which has been suggested, you
can use the binary representation and test the least significant
bit:

    my $parity = $y & 1 ? 'odd' : 'even';

Anno


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

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 V11 Issue 92
*************************************


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