[10686] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4278 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Nov 23 11:07:16 1998

Date: Mon, 23 Nov 98 08:00:24 -0800
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, 23 Nov 1998     Volume: 8 Number: 4278

Today's topics:
    Re: any more pattern matching (complex...) examples ? (Clay Irving)
        appending <richard_h@ttabconnectors.com>
    Re: Difficult Pattern matching problem in PERL <due@murray.fordham.edu>
    Re: Difficult Pattern matching problem in PERL <Allan@due.net>
    Re: Difficult Pattern matching problem in PERL (Tad McClellan)
    Re: Does (.*?) do what I think it does? (Matthew Bafford)
    Re: Does (.*?) do what I think it does? <ckc@dmi.dk>
        embedded perl mt safe? <gerhard@uni-x.net>
    Re: File handels to a variable (Steffen Beyer)
    Re: flock question (Andrew M. Langmead)
        HELP!  Newbie needs major help... <sabs45@hotmail.com>
        Help: Date Validation <pjb@nortel.com>
    Re: How do you pass parameters to Perl script from HTML (brian d foy)
        How does one get an SSL? (Diane McManus)
    Re: How does one get an SSL? (brian d foy)
    Re: HTTP_REFERER and Browser History <flavell@mail.cern.ch>
        Installing perl for win32 on MS Personal Web Server <alengua@virtual-orbis.com>
        mail server <"peter.herger"@swisslife.ch; peter.herger@bigfoot.com>
    Re: mail server (Clay Irving)
        My <!--#exec cgi="\scripts\script.pl"-->command will no <rlluhman@iastate.edu>
    Re: Passing parameters and returning values (Joergen W. Lang)
    Re: Perl 4 - Non-greedy regexp (Greg Ward)
        perl mods <fdm@internethomeschool.com>
    Re: perl mods (Joergen W. Lang)
    Re: Q: pod inside data structures (Don Blaheta)
    Re: Raw Sockets <ctookey@ihug.co.nz>
    Re: Round with Perl (Greg Ward)
    Re: Seeking Win32::DES.pm <carvdawg@patriot.net>
        sending patterns for substitutes as a parameter to a fu <aori@iil.intel.com>
        simple include <harrisp2@cf.ac.uk>
    Re: simple include (brian d foy)
        Strings instead of regular expressions paj@datcon.co.uk
    Re: Strings instead of regular expressions <rick.delaney@shaw.wave.ca>
    Re: Strings instead of regular expressions (Greg Ward)
    Re: Sum by group (Patrick Timmins)
        Special: Digest Administrivia (Last modified: 12 Mar 98 (Perl-Users-Digest Admin)

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

Date: 23 Nov 1998 09:16:35 -0500
From: clay@panix.com (Clay Irving)
Subject: Re: any more pattern matching (complex...) examples ?
Message-Id: <73bqo3$9pd@panix.com>

In <3659293C.7A55C3B@cadence.com> Han Kim <khan@cadence.com> writes:

>Is there any pattern matching examples I can get thru the internet?


Perl Reference: regular expressions
http://reference.perl.com/query.cgi?regexp

-- 
Clay Irving
clay@panix.com


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

Date: Mon, 23 Nov 1998 11:38:33 -0000
From: "Richard Hitchell" <richard_h@ttabconnectors.com>
Subject: appending
Message-Id: <911821115.11890.0.nnrp-04.9e98c956@news.demon.co.uk>

Hi- I this is a very simple question but I am new to all this so I apologise
in advance! I am writing my first perl program and want to append one file
to another without creating a totally new file, I also need to make sure
that no characters are added (ascii).

This is how I thought it might be done- but of course it just copies one
file to the other...

open ( FILE_TO_COPY, $a) || die "cannot open $a for reading: $!";
open ( MERGEFILE, ">>$b") || die "cannot open $b: $!";
while (<FILE_TO_COPY>) {
print MERGEFILE $_;
}
print "\nWeekly files merged!";
close(FILE_TO_COPY);
close(MERGEFILE);

Thanks...




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

Date: 23 Nov 1998 11:00:12 GMT
From: "Allan M. Due" <due@murray.fordham.edu>
Subject: Re: Difficult Pattern matching problem in PERL
Message-Id: <73bf7s$146$0@206.165.167.210>

Han Kim wrote in message <365928BE.216F3827@cadence.com>...
>Hi There,
>
>I am struggling to find out any easy way of eliminating any thing
>inbetween "/*" and "*/"
>....
>eg: If I have a line like:
>"/*Hi there*/ dont touch this /*please get this out*/  please"
>How can I use the substitution method in pearl to just get:
>"dont touch this please"
>Again, I want to use the substitution method...
>I tried the following but it erases the "dont touch this" portion....
> $_ =~ s/\/\*.+\*\///g;
>(The above writes out "please" only....)
>

Just make it a tad less greedy.  How about using your old friend ?:

$_=~s/\/\*.+?\*\///g;

AmD




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

Date: Mon, 23 Nov 1998 09:03:30 -0500
From: "AmD" <Allan@due.net>
Subject: Re: Difficult Pattern matching problem in PERL
Message-Id: <73bpmh$ibu$1@camel21.mindspring.com>


Han Kim wrote in message <365928BE.216F3827@cadence.com>...
>Hi There,
>
>I am struggling to find out any easy way of eliminating any thing
>inbetween "/*" and "*/"
>....
>
>eg: If I have a line like:
>
>"/*Hi there*/ dont touch this /*please get this out*/  please"
>
>How can I use the substitution method in pearl to just get:
>
>
>"dont touch this please"
>
>Again, I want to use the substitution method...
>I tried the following but it erases the "dont touch this" portion....
> $_ =~ s/\/\*.+\*\///g;
>
>(The above writes out "please" only....)
>

So close, just a little too greedy.

I might just make this less greedy by using ?.  Also, lets use | to get rid
of some of those toothpicks, and so / does not need to be escaped.

$_ = "/*Hi there*/ dont touch this /*please get this out*/  please";
s|/\*.+?\*/||g;
print;

gives:

 dont touch this   please

as requested.

AmD




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

Date: Mon, 23 Nov 1998 07:46:04 -0600
From: tadmc@flash.net (Tad McClellan)
Subject: Re: Difficult Pattern matching problem in PERL
Message-Id: <suob37.h2c.ln@flash.net>

Han Kim (khan@cadence.com) wrote:

: I am struggling to find out any easy way of eliminating any thing
: inbetween "/*" and "*/"
: .....


   Perl FAQ, part 6:

  "How do I use a regular expression to strip C style comments from a file?"


--
    Tad McClellan                          SGML Consulting
    tadmc@metronet.com                     Perl programming
    Fort Worth, Texas


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

Date: Mon, 23 Nov 1998 07:16:47 -0500
From: dragons@scescape.net (Matthew Bafford)
Subject: Re: Does (.*?) do what I think it does?
Message-Id: <MPG.10c31c3ecf1da9a098972c@news.scescape.net>

In article <36591C31.ED7D26CD@hsr.ch>, bnies@hsr.ch says...
=> Better this way:
=> 
=>   ($abc,$def,$ghi) = split(/,/,$yourstringhere);
=> 
=> .*  means a character apperars zero or more times (a comma
=>     is also a charachter)
=> .?  means a charachter may or may not match.
=> .*? useless

I think you need a trip back to perlre...

Especially the section on 'greedy' vs. 'non-greedy' matching.

Simply put, *? is not useless.  It is possible, however to craft a regex 
where *? is unnecessary.

=> Regards,

HTH,

=> Bernd

--Matthew


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

Date: Mon, 23 Nov 1998 13:29:40 +0100
From: Casper Kvan Clausen <ckc@dmi.dk>
Subject: Re: Does (.*?) do what I think it does?
Message-Id: <Pine.GS4.4.05.9811231318050.21446-100000@edb>

On Mon, 23 Nov 1998, Bernd Nies wrote:

> .*  means a character apperars zero or more times (a comma
>     is also a charachter)
> .?  means a charachter may or may not match.
> .*? useless
^^^^^^^^^^^^^

I beg to differ. You personal views of the usefulness of non-greedy
quantifiers notwithstanding, .*? (being a non-greedy version of .*) is
quite useful in many situations. Let's examine how it can help us:

__BEGIN__
#!/usr/local/bin/perl -w
#

$string="ab:cd:de";

$string =~ /^(.*):(.*)/;
print "Now we have:\t$1\t$2\n";

$string =~ /^(.*?):(.*)/;
print "Now we have:\t$1\t$2\n";
__END__

Kvan.
-- 
-------Casper Kvan Clausen------ | 'A *person* is smart. People are
----------<ckc@dmi.dk>---------- |  dumb, panicky, dangerous animals
           Lokal  544            |  and you know it.'
I do not speak for DMI, just me. |        - "K" in Men in Black.



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

Date: Mon, 23 Nov 1998 16:36:17 +0100
From: Gerhard Kutzelnigg <gerhard@uni-x.net>
Subject: embedded perl mt safe?
Message-Id: <365980F1.93944781@uni-x.net>

Hi,

I am using embedded perl in a multithread application, i.e. calling
perl_alloc () etc. in different threads. It seems to me that although I
am allocation a new PerlInterpreter object every time, embedded perl
still uses global variables or so. At least it doesn't work correctly.

When I fork () the process, there is no problem, it really occures when
using threads / lwps. Sorry to bother, but I am not talking about perl
threads, but posix threads using embedded perl.

Does anybody of you know if embedded perl is MT safe?

Thanks
gerry




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

Date: 23 Nov 1998 13:37:47 GMT
From: sb@engelschall.com (Steffen Beyer)
Subject: Re: File handels to a variable
Message-Id: <73bofb$m5j$1@en1.engelschall.com>

Josh Feinblum <toto@javanet.com> wrote:

> I asked a question earlier about a writing a filehandle to a variable.

> The response was to use the module IO::Scalar.  This worked, but had an
> undesired side effect.

> Part of the difficulty is that some of the print statements need to go to a
> file.  Is there anyway to re-direct a specific filehandle to a variable?

See "tie" in perlfunc(1).

The module "Tie::Handle" ties file handles to objects. 
The module "Data::Locations" ties file handles to files.

Maybe they do what you want?

> From my research I beleive that on solution would be setvbuf but I can't
> seem to get it to work right.

> Thanks in advance
> Josh

HTH.

Yours,
-- 
    Steffen Beyer <sb@engelschall.com>
    Free Perl and C Software for Download:
    http://www.engelschall.com/u/sb/download/
    New: Build'n'Play 2.1.0 (all-purpose Unix batch installation tool)


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

Date: Mon, 23 Nov 1998 14:47:16 GMT
From: aml@world.std.com (Andrew M. Langmead)
Subject: Re: flock question
Message-Id: <F2vqEs.K2o@world.std.com>

aajii@raaseri.ton.tut.fi (Antti-Jussi Korjonen) writes:

>How does perl behave, when trying to open a file locked with flock,
>does it try again or does it give up after one try?
>Is it possible to alter the number of tries not using any loops?

Unless given the LOCK_NB flag, the kernel puts it to sleep until the
lock is removed. (That is, it doesn't put it in the run queue, so it
never gets execution time.) If the LOCK_NB is set, it tries once and
returns with a value indicating success or failure.

-- 
Andrew Langmead


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

Date: Mon, 23 Nov 1998 14:58:12 GMT
From: "J.Oliver" <sabs45@hotmail.com>
Subject: HELP!  Newbie needs major help...
Message-Id: <8Ke62.1631$5n1.12807917@news.magma.ca>

Hi,

I'm just at the very beginning stages of learning PERL.

By that I mean I know PERL will do this, I just have no idea how to write
it. :)

If anyone is really bored and wants to show off their PERL skills, I would
really, really appreciate it.

Problem:  I need to run a script against a set of files.  Each file has a
different file name.  Each one contains a clear text record of all the mail
our company has sent or recieved. (in different directories.)  There is no
way to distinguish who it belongs to by the file name.

What I would like to do is first GREP the from: header for each file and
then move the file into a specific directory depending on who it belongs to.
The script would then move to the next file and repeat.

I then need to take each file and read the 'subject' and 'sent time' and
rename the file with that info in the file name.

I'll then use a mailer to put them all back in everyones mailbox.

This needs to be run from Windows NT.

I know it's 'uncooth' to ask this but if you can help me I will find a way
to show my appreciation.

Thanks very much in advance!

joliver@accelerix.com





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

Date: Mon, 23 Nov 1998 15:24:40 +0000
From: Peter J Bishop <pjb@nortel.com>
Subject: Help: Date Validation
Message-Id: <36597E38.87D3C430@nortel.com>

Is there a module available for validating a date? I have tried using
Time::ParseDate which should return 0 on an invalid date but it doesn't
seem to work.

For example, parsedate("40/11/1998", UK) returns a seconds value
relating to "10/12/1998".

Failing a module, has anyone written a piece of code to perform this
function?

Responses vie email would be appreciated.

Thanks.


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

Date: Mon, 23 Nov 1998 08:47:45 -0500
From: comdog@computerdog.com (brian d foy)
Subject: Re: How do you pass parameters to Perl script from HTML #exec statement?
Message-Id: <comdog-ya02408000R2311980847450001@news.panix.com>

In article <tamcgee-2111981759500001@cc1017583-a.union1.nj.home.com>, tamcgee@home.com (Tom McGee) posted:

> Why is this a problem?

this is answered in the documentation.

> In article <comdog-ya02408000R2111981559040001@news.panix.com>,
> comdog@computerdog.com (brian d foy) wrote:
> 
> >>In article <Pine.GSO.4.05.9811201103370.19882-100000@lance.netdoor.com>,
> Aaron Delong <delonad@netdoor.com> posted:
> >>
> >>> Have you tried adding your arguments like this:
> >>> 
> >>> <!--#exec cgi="/cgi-shell/my_script.pl?arg1=one&arg2=two&arg3=three"-->
> >>
> >>have you?  
> >>
> >>have you read the docs to understand why you can't do this? see the
> >>CGI Meta FAQ to discover how bad your advice is.
> >>
> >>-- 
> >>brian d foy                                  <comdog@computerdog.com>
> >>CGI Meta FAQ <URL:http://computerdog.com/CGI_MetaFAQ.html>
> >>hint: <!--#include virtual="....?parameters"-->

-- 
brian d foy                                  <comdog@computerdog.com>
CGI Meta FAQ <URL:http://computerdog.com/CGI_MetaFAQ.html>


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

Date: Mon, 23 Nov 1998 15:24:15 GMT
From: gback@teleport.com (Diane McManus)
Subject: How does one get an SSL?
Message-Id: <3659798c.120011541@news.teleport.com>

Hi,

>From this post you can probably tell I'm a newbie here  -- along with
the fact I don't spell that well..  :-)

I have a credit card merchant account, but no SSL secure site to
receive orders at.  I would like to set one up.  

Could anyone give me assistance by teelling me how to do this?  If you
know how one would go about doing this, or can answer any of the
questions below please reply.

If one needs to get an SSL (for processing credit cards, etc) how do
they get one?

Are there any free SSL's?

Are there places that let you use their SSL's?

Do any of these let you use theirs for free?

If not, what costs are involved in setting up one of these? (I ask as
I shudder with fear it will be a fortune.)  :-)

What would YOUrecomment?

Thank You Very Kindly,

-Diane

PS:  I'd  "love you to death"  :-)   if when you post you could also
cc a reply to 
Diane McManus <gback@teleport.com>



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

Date: Mon, 23 Nov 1998 11:04:25 -0500
From: comdog@computerdog.com (brian d foy)
Subject: Re: How does one get an SSL?
Message-Id: <comdog-ya02408000R2311981104250001@news.panix.com>

In article <3659798c.120011541@news.teleport.com>, gback@teleport.com (Diane McManus) posted:

> From this post you can probably tell I'm a newbie here  -- along with
> the fact I don't spell that well..  :-)
> 
> I have a credit card merchant account, but no SSL secure site to
> receive orders at.  I would like to set one up.  
> 
> Could anyone give me assistance by teelling me how to do this?

this is a web server sort of thing (absolutely nothing to do with
perl).  ask your local system administrator or check your server
documnetation.

-- 
brian d foy                                  <comdog@computerdog.com>
CGI Meta FAQ <URL:http://computerdog.com/CGI_MetaFAQ.html>


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

Date: Mon, 23 Nov 1998 11:36:52 +0100
From: "Alan J. Flavell" <flavell@mail.cern.ch>
Subject: Re: HTTP_REFERER and Browser History
Message-Id: <Pine.HPP.3.95a.981123113508.20364A-100000@hpplus01.cern.ch>

On Sun, 22 Nov 1998, Dan Brian wrote:

> I also prefaced my comments with "I don't know" ... so get off it.
 ..
> I'm always amused by petty criticisms of those who try to help by posting
> very *relevant* responses to questions.

Like "I don't know".  And on an inappropriate group, to boot.




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

Date: Mon, 23 Nov 1998 09:18:00 -0500
From: "Alejandro Lengua Vega" <alengua@virtual-orbis.com>
Subject: Installing perl for win32 on MS Personal Web Server
Message-Id: <73bq97$lj8$1@ucayali.unired.net.pe>

Hi!
I am running windows98 and MS PWS
I have already installed the perl for win32 (run the install that comes
with)
but what else do I need to do to be able to run
perl scripts from a homepage?

Thanks
Alejandro Lengua





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

Date: Mon, 23 Nov 1998 13:07:14 +0100
From: Herger Peter <"peter.herger"@swisslife.ch; peter.herger@bigfoot.com>
Subject: mail server
Message-Id: <73bj5i$4n41@su1010.swisslife.ch>

Hi all,

How can I send Mails from an Perl Script through(or to) a mailserver?
I've no idea how to specify the mail server in my perl script.
I have used Mail::Send

Thanks a lot

                      Peter Herger




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

Date: 23 Nov 1998 09:22:49 -0500
From: clay@panix.com (Clay Irving)
Subject: Re: mail server
Message-Id: <73br3p$ah7@panix.com>

In <73bj5i$4n41@su1010.swisslife.ch> Herger Peter <"peter.herger"@swisslife.ch; peter.herger@bigfoot.com> writes:

>How can I send Mails from an Perl Script through(or to) a mailserver?
>I've no idea how to specify the mail server in my perl script.
>I have used Mail::Send
  
Something like this:

  use Mail::Sendmail;
  
  %mail = (To      => "peter.herger\@bigfoot.com",
           From    => "clay\@panix.com",
           Subject => "Re: mail server",
           Message => "This is how you do it"
          );
  
  if (sendmail %mail) {
      print "Mail sent OK.\n";
  } else {
      print "Error sending mail: $Mail::Sendmail::error \n";
  }

-- 
Clay Irving
clay@panix.com


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

Date: Mon, 23 Nov 1998 08:45:25 -0600
From: "Rick Luhman" <rlluhman@iastate.edu>
Subject: My <!--#exec cgi="\scripts\script.pl"-->command will not work
Message-Id: <73bsdr$ni1$1@news.iastate.edu>

I have been trying to get #exec cgi to work with Microsoft-IIS/4.0 but to no
avail.  I keep getting the following error message.

HTTP/1.1 200 OK Date: Mon, 23 Nov 1998 12:38:00 GMT Server:
Microsoft-IIS/4.0 Content-type: text/html 'F:\InetPub\wwwroot\test.stm'
script produced no output.



Both directories (the hypertext directory and the script directory) have
execute permisions.  The perl scripts work fine when loaded directly (e.g.
http://mydomain/script/script.pl).





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

Date: Mon, 23 Nov 1998 16:14:02 +0100
From: jwl@worldmusic.de (Joergen W. Lang)
Subject: Re: Passing parameters and returning values
Message-Id: <1diyei2.47jid01ntcfswN@host040-210.seicom.net>

Antony <amcnulty@nortel.co.uk> wrote:

> Can this be done with the sub-routines in Perl ?
> 
> i.e the following situation....
>
> &mysub("hello world\n");
>
> sub mysub ($string) {
>     print STDOUT $string;
> }

Nearly. Parameteres passed to a subroutine arrive in the special array
@_. So if you wanted to print your string using a sub you could say:

sub mysub {
    print $_[0]; # print first element of @_ to STDOUT. 
}

hth,

Joergen
-- 
-------------------------------------------------------------------
   "Everything is possible - even sometimes the impossible"
             HOELDERLIN EXPRESS - "Touch the void"
-------------------------------------------------------------------


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

Date: 23 Nov 1998 13:51:23 GMT
From: gward@thrak.cnri.reston.va.us (Greg Ward)
Subject: Re: Perl 4 - Non-greedy regexp
Message-Id: <73bp8r$6ea$3@news0-alterdial.uu.net>

On Sun, 22 Nov 1998 17:09:49 -0000, Martin <minich@globalnet.co.uk> wrote:
>I have the regexp:
>
>s/abc(.*?)abc/$FOO{$1}/sg;
>
>which works fine under Perl 5 but Perl 4 won't accept the ? modifier to make
>the match .* non-greedy. I normally use Perl 5 so have never come across
>this
>problem before. How can regexps be made non-greedy in Perl 4?

They can't.  That's why ? was added in Perl 5.  (Although sometimes you
can go to great hairy lengths to come up with greedy regexes that act
like their non-greedy equivalents, the process is usually much more
bother than it's worth.  Don't go there.)

        Greg
-- 
Greg Ward - software developer                    gward@cnri.reston.va.us
Corporation for National Research Initiatives    
1895 Preston White Drive                      voice: +1-703-620-8990 x287
Reston, Virginia, USA  20191-5434               fax: +1-703-620-0913


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

Date: Sun, 22 Nov 1998 05:14:28 -0800
From: "Frank" <fdm@internethomeschool.com>
Subject: perl mods
Message-Id: <73bn61$anp$1@supernews.com>

what is a perl mod? I thought they only came from oysters?

--
fdm@internethomeschool.com
moody@internethomeschool.com
ihs@internethomeschool.com




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

Date: Mon, 23 Nov 1998 16:14:07 +0100
From: jwl@worldmusic.de (Joergen W. Lang)
Subject: Re: perl mods
Message-Id: <1diyewo.11azr4y3n8yg0N@host040-210.seicom.net>

Frank <fdm@internethomeschool.com> wrote:

> what is a perl mod? I thought they only came from oysters?

A Perl module ? Mod_perl ? Someone who likes to put loads of fancy
mirrors and decorations around the computer screen while using Perl ?
Hard to say.

Anyway - use Perl and the world _is_ your oyster. :-)

Joergen
-- 
-------------------------------------------------------------------
   "Everything is possible - even sometimes the impossible"
             HOELDERLIN EXPRESS - "Touch the void"
-------------------------------------------------------------------


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

Date: 20 Nov 1998 17:03:53 GMT
From: dpb@cs.brown.edu (Don Blaheta)
Subject: Re: Q: pod inside data structures
Message-Id: <7347dp$k91@cocoa.brown.edu>

Quoth Zenin:
> Uri Guttman <uri@sysarch.com> wrote:
> 	>snip<
> : a decent workaround and i will probably use it unless something better
> : comes along. but it is not a proper answer to why it fails. i was
> : drinking with randal tonight and he says it has to do with the parser
> : expecting an expression token next. 
> 
> 	Yep.  Personally, I think throwing the pod away in a preprocessing
> 	operation would be better then trying to have the parser deal with
> 	it inline, although that might make line number debug statements
> 	harder... 

Why would it affect line numbers?  Anything it throws away, it can just
leave whitespace in there.

-- 
-=-Don Blaheta-=-=-dpb@cs.brown.edu-=-=-<http://www.cs.brown.edu/~dpb/>-=-
"I believe we are on an irreversible trend toward more freedom and
democracy. But that could change."			--Dan Quayle


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

Date: Tue, 24 Nov 1998 01:26:09 +1300
From: "Hyper" <ctookey@ihug.co.nz>
Subject: Re: Raw Sockets
Message-Id: <73bkdd$b8$1@newsource.ihug.co.nz>

yes




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

Date: 23 Nov 1998 13:49:54 GMT
From: gward@thrak.cnri.reston.va.us (Greg Ward)
Subject: Re: Round with Perl
Message-Id: <73bp62$6ea$2@news0-alterdial.uu.net>

On 22 Nov 1998 16:51:31 GMT, Tuomas Angervuori <tumppi@nospam.icon.fi> wrote:
>I have a problem: for example when I divide 1 with 3, I get result 
>0.3333333333.... How can I tell Perl to count only for example two 
>desimals?

Try this one on for size:

=item round (VAL [, FACTOR [, DIR]])

Rounds VAL towards some multiple of FACTOR (which defaults to 1).  If DIR
is -1, it rounds down to the next lowest multiple of FACTOR; if DIR is 0,
it rounds to the nearest multiple of FACTOR, if DIR is +1, it rounds up to
the next highest multiple of FACTOR.  The default is to round to the
nearest multiple of FACTOR.

For example:

   round (3.25)        == 3
   round (3.25, 5)     == 5
   round (3.25, 5, -1) == 0
   round (-1.2, 2, +1) == 0
   round (-1.2, 2)     == -2

=cut

sub round
{
   my($value, $factor, $direction) = @_;
   $factor = 1 unless defined $factor;
   $direction = 0 unless defined $direction;

   $factor = abs ($factor);
   $value /= $factor;
   if ($direction == 0)
   {
      $value += ($value < 0.0) ? (-0.5) : (+0.5);
      $value = int($value) * $factor;
   }
   elsif ($direction == -1)
   {
      $value = floor ($value) * $factor;
   }
   elsif ($direction == +1)
   {
      $value = ceil ($value) * $factor;
   }
}

Use at your own risk.  Not guaranteed to be correct (but it works for
me).

For rounding to a certain number of decimals, you could either use my
code with FACTOR set to various powers of 10 (.01, .001, etc.) or do as
the FAQ suggests and just use sprintf.  I'm not sure which would be
faster, but don't count on either one for speed.

        Greg

-- 
Greg Ward - software developer                    gward@cnri.reston.va.us
Corporation for National Research Initiatives    
1895 Preston White Drive                      voice: +1-703-620-8990 x287
Reston, Virginia, USA  20191-5434               fax: +1-703-620-0913


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

Date: Mon, 23 Nov 1998 06:13:31 +0000
From: Marquis de Carvdawg <carvdawg@patriot.net>
Subject: Re: Seeking Win32::DES.pm
Message-Id: <3658FD0B.C67937C5@patriot.net>

Try here:

http://www.generation.net/~cybersky/Perl/perlmod.htm

Carv

Andrew Lee wrote:

> I know someone has written a perl module that implements DES in Win32,
> but I can not, for the life of me, find it!
>
> Any help is greatly appreciated!
>
> --
>
> Andrew F. Lee
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>  When I say, "Ignore the man behind the curtain."
>  That is because there is no man behind the curtain.





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

Date: Mon, 23 Nov 1998 12:06:20 +0200
From: Ori Aruj <aori@iil.intel.com>
Subject: sending patterns for substitutes as a parameter to a function
Message-Id: <3659339C.88101607@iil.intel.com>

Hi  !

I've had some problems sending patterns for the function s/// as
parameters.
the main problems are:

    the $1,...,$10 variables - the interpolating time is too early or to
late
    if I use the s///e option that evals the right side it causes
problems with other patterns as \.

I've been testing a lot of options like \1 instead of $1, the use of {
}, a lot of escaping.
if you have a working way please send it to me with an example.
take for example the following  simple lines:

s/\[(\d+)\]/_$1/

s/\[(\d)\-(\d*)\]/_$1_$2/

     Thanks Ori.




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

Date: Mon, 23 Nov 1998 15:09:59 -0000
From: "Phil Harris" <harrisp2@cf.ac.uk>
Subject: simple include
Message-Id: <365987b7.0@d032>

Hi all,

I hope this is not a FAQ but here goes anyway.

Is there a simple way to include a file, not a module just some init
strings, from the current directory.

TIA

Phil
phil@pharris.force9.co.uk





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

Date: Mon, 23 Nov 1998 11:05:07 -0500
From: comdog@computerdog.com (brian d foy)
Subject: Re: simple include
Message-Id: <comdog-ya02408000R2311981105070001@news.panix.com>

In article <365987b7.0@d032>, "Phil Harris" <harrisp2@cf.ac.uk> posted:

> Is there a simple way to include a file, not a module just some init
> strings, from the current directory.

perhaps using require()?

-- 
brian d foy                                  <comdog@computerdog.com>
CGI Meta FAQ <URL:http://computerdog.com/CGI_MetaFAQ.html>


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

Date: Mon, 23 Nov 1998 13:05:41 GMT
From: paj@datcon.co.uk
Subject: Strings instead of regular expressions
Message-Id: <73bmj3$iab$1@nnrp1.dejanews.com>

Hi,

In my Perl program I have a string, which I want to search for in another
string. Of course, if I do this using /$str/ then it is interpreted as a
regular expression, and so breaks on, for example, $str = "(-:". How do I
instruct perl to search to treat it as a string? I also need to use the
s/$str/$strb/g construct. I realise I could hack this by having code to put in
backslashes, but that seems a crazy solution.

Thanks,

Paul

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    


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

Date: Mon, 23 Nov 1998 13:36:04 GMT
From: Rick Delaney <rick.delaney@shaw.wave.ca>
Subject: Re: Strings instead of regular expressions
Message-Id: <36596675.46F19ED7@shaw.wave.ca>

[posted & mailed]

paj@datcon.co.uk wrote:
> 
> Hi,
> 
> In my Perl program I have a string, which I want to search for in 
> another string. Of course, if I do this using /$str/ then it is 
> interpreted as a regular expression, and so breaks on, for example, 
> $str = "(-:". How do I instruct perl to search to treat it as a 
> string? I also need to use the s/$str/$strb/g construct. I realise I 
> could hack this by having code to put in backslashes, but that seems a 
> crazy solution.

It certainly does when there is already a function that does it for you.

    perldoc -f quotemeta

    $str = "(-:";
    $qstr = quotemeta $str;
    print if $str =~ /$qstr/; 

Also check out \Q in perlre.

    print if $str =~ /\Q$str/;

-- 
Rick Delaney
rick.delaney@shaw.wave.ca


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

Date: 23 Nov 1998 14:00:27 GMT
From: gward@thrak.cnri.reston.va.us (Greg Ward)
Subject: Re: Strings instead of regular expressions
Message-Id: <73bppr$6ea$4@news0-alterdial.uu.net>

On Mon, 23 Nov 1998 13:05:41 GMT, paj@datcon.co.uk <paj@datcon.co.uk> wrote:
>In my Perl program I have a string, which I want to search for in another
>string. Of course, if I do this using /$str/ then it is interpreted as a
>regular expression, and so breaks on, for example, $str = "(-:". How do I
>instruct perl to search to treat it as a string? I also need to use the
>s/$str/$strb/g construct. I realise I could hack this by having code to put in
>backslashes, but that seems a crazy solution.

For just searching, use the 'index' function.  It's documented in the
'perlfunc' man page, and of course in the Camel Book.

For your second problem -- replacing an arbitrary string -- it might be
nice if Perl had a builtin 'replace', like Python's string module
does, analogous to 'index'... but it doesn't.  Instead, you have to use
s///.

However, quoting your $str is not hard -- just use the quotemeta
function -- also documented in the usual places.  (You can also use the
\Q operator inside a doubly-quoted string for the same effect.  It's
documented in 'perlop'.)  (The existence of quotemeta, of course, means
you can use m// to find $str rather than 'index'.  'index' should be
faster, though.

        Greg
-- 
Greg Ward - software developer                    gward@cnri.reston.va.us
Corporation for National Research Initiatives    
1895 Preston White Drive                      voice: +1-703-620-8990 x287
Reston, Virginia, USA  20191-5434               fax: +1-703-620-0913


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

Date: Mon, 23 Nov 1998 15:40:03 GMT
From: ptimmins@netserv.unmc.edu (Patrick Timmins)
Subject: Re: Sum by group
Message-Id: <73bvki$okh$1@nnrp1.dejanews.com>

In article <735dl9$u39$1@nnrp1.dejanews.com>,
  ptimmins@netserv.unmc.edu (Patrick Timmins) wrote:
[snip]
> Now that you've defined your input, and how you want the output to
> look, here is a variation of my earlier post that gives the output
> you desire ... I was too lazy to start over from scratch, so
> this may not be the most efficient way :)

Now the hubris in me has overtaken the laziness and impatience of
my earlier response in this thread, and I *must* post a more elegant
version of my original solution:

while (<DATA>) {
   @temp = split/\s+/;
   $group{$temp[0]}{n}++;
   for ($i=1; $i<@temp; $i++) {
      $group{$temp[0]}{$i}{sum} += $temp[$i];
   }
}

foreach $grp (sort keys %group) {
   print "$grp $group{$grp}{n}";
   foreach $clmn (sort keys %{ $group{$grp} } ) {
       print " $group{$grp}{$clmn}{sum}";
   }
   print "\n";
}
__DATA__
1 2 5 7 7
3 4 5 7 5
1 2 4 5 4
3 4 3 3 5
3 4 5 6 6
2 4 5 6 7
2 5 5 4 4

will print:

1 2 4 9 12 11
2 2 9 10 10 11
3 3 12 13 16 16

There. That feels better.

Patrick Timmins
$monger{Omaha}[0]

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    


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

Date: 12 Jul 98 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Special: Digest Administrivia (Last modified: 12 Mar 98)
Message-Id: <null>


Administrivia:

Special notice: in a few days, the new group comp.lang.perl.moderated
should be formed. I would rather not support two different groups, and I
know of no other plans to create a digested moderated group. This leaves
me with two options: 1) keep on with this group 2) change to the
moderated one.

If you have opinions on this, send them to
perl-users-request@ruby.oce.orst.edu. 


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

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