[19107] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1302 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Jul 13 18:10:34 2001

Date: Fri, 13 Jul 2001 15:10:15 -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: <995062214-v10-i1302@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Fri, 13 Jul 2001     Volume: 10 Number: 1302

Today's topics:
    Re: Random Numbers... <rsherman@ce.gatech.edu>
        regexp question <emdee@DEMUNGEcwcom.net>
    Re: regexp question <mbudash@sonic.net>
    Re: regexp question (Craig Berry)
        Replacing a word in a file (BUCK NAKED1)
    Re: Replacing a word in a file <cpryce@pryce.net>
    Re: Replacing a word in a file <tore@extend.no>
    Re: Replacing a word in a file (Tad McClellan)
    Re: Replacing a word in a file <cpryce@pryce.net>
    Re: search and replace $ (Joe Smith)
    Re: search and replace $ <mdudley@execonn.com>
        Shouldn't -e return FALSE instead of UNDEFINED on non-e (pt)
    Re: Signal SEGV - What is it? <stumo@bigfoot.com>
    Re: Using long integer in PERL <tore@extend.no>
        warn() does not use a localized STDERR filehandle, how  (nico gianniotis)
    Re: Where is MD5 <krahnj@acm.org>
        Why is socket timeout not working? <haim.lichaa@intel.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 13 Jul 2001 14:02:40 +0500
From: Robert Sherman <rsherman@ce.gatech.edu>
Subject: Re: Random Numbers...
Message-Id: <3B4EB930.C6D89076@ce.gatech.edu>

TedWeb wrote:
> 
> Does perl have a random number generator? If so, how do I use it?
> 
> Many thanks,
> TedWeb


perldoc -f rand
perldoc -f srand


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

Date: Fri, 13 Jul 2001 18:37:11 GMT
From: Matt D <emdee@DEMUNGEcwcom.net>
Subject: regexp question
Message-Id: <chfukt4q0c9u5o157ob2ot20hv3vu23gj5@4ax.com>

there's probably a 'doh! of course!' answer to this, but it's eluding
me at the moment...

i want to capture a keyword and up to 3 words either side of it from a
block of text (keyword could potentially be at the beginning, end or
anywhere in the middle).

my block of text is a single space-delimited list of words.

i can do this:

$text =~ /(\S*\s*\S*\s*\S*\s*)($keyword)(\s*\S*\s*\S*\s*\S*)/i;
$match = "$1$2$3";

which kind of works but it can be very slow on big lists of words.
i'm sure there's a much more efficient way to do it.. can some kind
soul point me in the right direction please?

TIA,
m


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

Date: Fri, 13 Jul 2001 19:51:47 GMT
From: Michael Budash <mbudash@sonic.net>
Subject: Re: regexp question
Message-Id: <mbudash-887CF8.12515013072001@news.sonic.net>

In article <chfukt4q0c9u5o157ob2ot20hv3vu23gj5@4ax.com>, Matt D 
<emdee@DEMUNGEcwcom.net> wrote:

> there's probably a 'doh! of course!' answer to this, but it's eluding
> me at the moment...
> 
> i want to capture a keyword and up to 3 words either side of it from a
> block of text (keyword could potentially be at the beginning, end or
> anywhere in the middle).
> 
> my block of text is a single space-delimited list of words.
> 
> i can do this:
> 
> $text =~ /(\S*\s*\S*\s*\S*\s*)($keyword)(\s*\S*\s*\S*\s*\S*)/i;
> $match = "$1$2$3";
> 
> which kind of works but it can be very slow on big lists of words.
> i'm sure there's a much more efficient way to do it.. can some kind
> soul point me in the right direction please?

not sure about the speed issue, but this works for me:

undef $match;
if ($text =~ /(\S+\s){0,3}$keyword(\s\S+){0,3}/) {
  $match = $&;
}

hth-
-- 
Michael Budash ~~~~~~~~~~ mbudash@sonic.net


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

Date: Fri, 13 Jul 2001 21:16:11 -0000
From: cberry@cinenet.net (Craig Berry)
Subject: Re: regexp question
Message-Id: <tkup8rq30etpb0@corp.supernews.com>

Matt D (emdee@DEMUNGEcwcom.net) wrote:
: there's probably a 'doh! of course!' answer to this, but it's eluding
: me at the moment...
: 
: i want to capture a keyword and up to 3 words either side of it from a
: block of text (keyword could potentially be at the beginning, end or
: anywhere in the middle).
: 
: my block of text is a single space-delimited list of words.

If the block isn't big and exact spacing doesn't need to be maintained,
I'd do it like this:

  my $block = 'foo blah barg zneeb foo dribble foo glarb doh foo sneex';
  my $kw    = 'foo';
  my @words = $block =~ /(\w+)/g;

  for (my $i = 0; $i < @words; $i++) {
    if ($words[$i] eq $kw) {
      my $lo = $i - 3;
      my $hi = $i + 3;
      $lo = 0       if $lo < 0;
      $hi = $#words if $hi > $#words;
      print join(' ', @words[$lo..$hi]), "\n";
    }
  }

This grabs every instance of 'foo' and up to three words on either side of
it.  Note that overlapping ranges will work properly.

-- 
   |   Craig Berry - http://www.cinenet.net/~cberry/
 --*--  "Brute force done fast enough looks slick."
   |             - William Purves


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

Date: Fri, 13 Jul 2001 14:46:32 -0500 (CDT)
From: dennis100@webtv.net (BUCK NAKED1)
Subject: Replacing a word in a file
Message-Id: <14207-3B4F5018-87@storefull-248.iap.bryant.webtv.net>

Why doesn't the below work? I've tested this for hours, and get no
error; but it doesn't replace. I want to replace one word with another
in a file called $tmpfile. 
open(TFILE, "$tmpfile") or die "can't open $tmpfile: $!"; 
while (<$tmpfile>) {
      $tmpfile =~ s/$original/$replacement/ig or print "Cant 
replace: $!\n"; 
      };
close TFILE;

What am I doing wrong?


Thanks,
Dennis



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

Date: Fri, 13 Jul 2001 15:37:01 -0500
From: cp <cpryce@pryce.net>
Subject: Re: Replacing a word in a file
Message-Id: <B774C61D.87F4%cpryce@pryce.net>

in article 14207-3B4F5018-87@storefull-248.iap.bryant.webtv.net, BUCK NAKED1
at dennis100@webtv.net wrote on 07/13/2001 2:46 PM:

> Why doesn't the below work? I've tested this for hours, and get no
> error; but it doesn't replace. I want to replace one word with another
> in a file called $tmpfile.
> open(TFILE, "$tmpfile") or die "can't open $tmpfile: $!";

# filehandle is opened in read only mode.
> while (<$tmpfile>) {
# do you mean while (<TFILE>) {  ?

>     $tmpfile =~ s/$original/$replacement/ig or print "Cant
> replace: $!\n"; 

# if $tmpfile was your path, you are trying your search/replace on the SAME
value for each and every line of TFILE
>     };
> close TFILE;
> 
> What am I doing wrong?

You are not reading the FAQs. This is covered in perlfaq5 'How do I change
one line in a file/delete a line in a file/insert a line in the middle of a
file/append to the beginning of a file?'

It's also covered in chapter 7 of the Perl Cookbook.

cp



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

Date: Fri, 13 Jul 2001 22:41:18 +0200
From: Tore Aursand <tore@extend.no>
Subject: Re: Replacing a word in a file
Message-Id: <MPG.15b97b5dd6c78cd0989695@news.online.no>

In article <14207-3B4F5018-87@storefull-
248.iap.bryant.webtv.net>, dennis100@webtv.net says...
> Why doesn't the below work?

Because the syntax is _completely_ wrong. :)

> open(TFILE, "$tmpfile") or die "can't open $tmpfile: $!"; 

Good!  Not everyone remember (?) to include the die statement.

> while (<$tmpfile>) {
>       $tmpfile =~ s/$original/$replacement/ig or print "Cant 
> replace: $!\n"; 
>       };
> close TFILE;

You are using a scalar as a filehandle.  That doesn't work.  
This should work better;

  my $tmpfile = './file.temp';
  open(TMPFILE, $tmpfile) || die "$!\n";
  while (<TMPFILE>) {
      s/$original/$replacement/gi;
  }
  close(TMPFILE);


-- 
Tore Aursand - tore@extend.no - www.aursand.no


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

Date: Fri, 13 Jul 2001 15:57:45 -0400
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Replacing a word in a file
Message-Id: <slrn9kuklp.o8a.tadmc@tadmc26.august.net>

BUCK NAKED1 <dennis100@webtv.net> wrote:

>Why doesn't the below work? I've tested this for hours, 
                                              ^^^^^^^^^


You have wasted your time then. Don't do that. 

Start with some "good code" and modify it for your needs.


>and get no
>error; but it doesn't replace. 
>I want to replace one word with another
>in a file called $tmpfile. 
      ^^^^
      ^^^^ there's a nice search term...


   perldoc -q file

      "How do I change one line in a file/delete a line in a
       file/insert a line in the middle of a file/append to the
       beginning of a file?"


>open(TFILE, "$tmpfile") or die "can't open $tmpfile: $!"; 
             ^        ^
             ^        ^ useless double quotes

   open(TFILE, $tmpfile) or die "can't open $tmpfile: $!";


>while (<$tmpfile>) {


   while (<TFILE>) { # wants a filehandle, not a string


>      $tmpfile =~ s/$original/$replacement/ig or print "Cant 
>replace: $!\n"; 


You are substituting in the $tmpfile variable. What is in the
$tmpfile variable? A filename. Do you want to change the
filename or the file _contents_?

The line from the file is in $_, not in $tmpfile;

Why the "or print" there? it will complain for any line that
does not happen to have a $original in it. Are all data lines
required to match whatever that is?


>      };
>close TFILE;


Your program never made any output (because you do not use any
output operators). A program that makes no output is of very
limited value. You probably want a print() in there somewhere.


>What am I doing wrong?


Just about everything it would appear.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Fri, 13 Jul 2001 16:01:31 -0500
From: cp <cpryce@pryce.net>
Subject: Re: Replacing a word in a file
Message-Id: <B774CBDB.8805%cpryce@pryce.net>

in article MPG.15b97b5dd6c78cd0989695@news.online.no, Tore Aursand at
tore@extend.no wrote on 07/13/2001 3:41 PM:

>> while (<$tmpfile>) {
>>       $tmpfile =~ s/$original/$replacement/ig or print "Cant
>> replace: $!\n"; 
>>       };
>> close TFILE;
> 
> You are using a scalar as a filehandle.  That doesn't work.
> This should work better;
> 
> my $tmpfile = './file.temp';
> open(TMPFILE, $tmpfile) || die "$!\n";
> while (<TMPFILE>) {
>     s/$original/$replacement/gi;
> }
> close(TMPFILE);

While this is replacing the $_ string nicely, it's not doing anything with
it. Like printing the results to a file. Which it can't do to TMPFILE 'cause
it's opened in read mode.




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

Date: Fri, 13 Jul 2001 18:29:31 +0000 (UTC)
From: inwap@best.com (Joe Smith)
Subject: Re: search and replace $
Message-Id: <9inemb$14j5$1@nntp1.ba.best.com>

In article <3B4F2FE8.DCEAB8C4@execonn.com>,
Marshall Dudley  <mdudley@execonn.com> wrote:
>I don't know if this is a bug, or what, but I am finding it impossible
>to search for a dollar sign and replace it with something else.
>
>I am running perl verison 5.005_03
>
>For instance, with the following code:
>
>$text = "this product cost $5 to purchase\n";

 print "Before doing substitution, text='$text'\n";
 print "Oops. There is no dollar sign there.  Lets try again\n";
 $text = 'this product cost $5 to purchase' . "\n";

>$text =~ s/\$/ dollars /;
>print "$text\n";

>If I search for any other character than the "$" it works fine, it just
>messes up with the dollar sign.

The problem was that your example was using $5 as the name of a variable
because the dollarsign was between double quotes.  Go back to the
manual and look up "quote-like operators".

>What I am wanting to do in the end is to replace all "$" with "\$" in a
>string so I can use it for a regular expression.

There is a different solution for that.  Use qr() or \Q.
  s/\Q$string/$replacement/;

	-Joe

--
See http://www.inwap.com/ for PDP-10 and "ReBoot" pages.


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

Date: Fri, 13 Jul 2001 14:54:49 -0400
From: Marshall Dudley <mdudley@execonn.com>
Subject: Re: search and replace $
Message-Id: <3B4F43F9.34567DAF@execonn.com>

DUH.  Boy do I feel stupid.  The real code the text comes in from a post,
but I was trying to get it to work in a small test script and that is what I
screwed up. Turns out the real problem is that I cannot substute /\\$/ but
have to use /\\\$/ to get \$.

Working now.

Thanks,

Marshall

Joe Smith wrote:

> In article <3B4F2FE8.DCEAB8C4@execonn.com>,
> Marshall Dudley  <mdudley@execonn.com> wrote:
> >I don't know if this is a bug, or what, but I am finding it impossible
> >to search for a dollar sign and replace it with something else.
> >
> >I am running perl verison 5.005_03
> >
> >For instance, with the following code:
> >
> >$text = "this product cost $5 to purchase\n";
>
>  print "Before doing substitution, text='$text'\n";
>  print "Oops. There is no dollar sign there.  Lets try again\n";
>  $text = 'this product cost $5 to purchase' . "\n";
>
> >$text =~ s/\$/ dollars /;
> >print "$text\n";
>
> >If I search for any other character than the "$" it works fine, it just
> >messes up with the dollar sign.
>
> The problem was that your example was using $5 as the name of a variable
> because the dollarsign was between double quotes.  Go back to the
> manual and look up "quote-like operators".
>
> >What I am wanting to do in the end is to replace all "$" with "\$" in a
> >string so I can use it for a regular expression.
>
> There is a different solution for that.  Use qr() or \Q.
>   s/\Q$string/$replacement/;
>
>         -Joe
>
> --
> See http://www.inwap.com/ for PDP-10 and "ReBoot" pages.



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

Date: 13 Jul 2001 14:30:22 -0700
From: mnemotronic@yahoo.com (pt)
Subject: Shouldn't -e return FALSE instead of UNDEFINED on non-existant file?
Message-Id: <da662010.0107131330.6a7980e2@posting.google.com>

The generic description for the file test operators says they return 1
for TRUE and '' for FALSE.  The -e (file exists) does not return FALSE
if the file doesn't exist, it returns UNDEFINED, as do the other -X
operators.
   I know I'm picking at nano-nits while rowing upstream amd pissing
into the wind here, but it makes sense (to me) for this operator to
actually return TRUE or FALSE.
   Is it possible for -e to ever return the FALSE value ('') ???


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

Date: Fri, 13 Jul 2001 20:28:28 +0100
From: "Stuart Moore" <stumo@bigfoot.com>
Subject: Re: Signal SEGV - What is it?
Message-Id: <DYH37.26962$WS4.4095278@news6-win.server.ntlworld.com>

Dan Sugalski <dan@tuatha.sidhe.org> wrote in message
news:vVD37.158720$v5.12197549@news1.rdc1.ct.home.com...
> Stuart Moore <stumo@bigfoot.com> wrote:
>
> > Just for the record:
>
> > The error was solved when commenting out a particular line of a hash
definition
> > (as in %hash = (a=>b, ...)
> > The value of this key was from a variable, I changed the lines where this
> > variable value was obtained, so that exactly the same value was obtained in
a
> > slightly different manner (it had to de reference the output of a function
> > pointing to an array and take the first subscript) and it worked. There's
more
> > than one way to do it, but one of them causes a signal fault ;-)
>
> That's really, really bad, and it means something's wrong internally. If you
> can ship the entire program causing the problem off to activestate that
> woul dbe a good thing, since there's a bug in perl somewhere. (If you can
> duplicate the death on another OS that'd be even better)
>
> Did you fork() along the way by any chance?
>

No fork or anything complicated in my code; modules involved were DBI
(connecting to mysql) and CGI. Unfortunately I don't have that version any
longer, didn't think to keep it. If the same happens again, I'll keep a copy.

Stuart




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

Date: Fri, 13 Jul 2001 22:31:18 +0200
From: Tore Aursand <tore@extend.no>
Subject: Re: Using long integer in PERL
Message-Id: <MPG.15b979049fcb6fe5989694@news.online.no>

In article <3b4ef0c2@news>, 
benghua@NOSPAMMING.cyberworks.net.sg says...
> I have an array with big numbers, [...]

Take a look at the Math::* modules in the CPAN directory [1], 
especially Math::BigInt.

[1] <URL:http://www.cpan.org/>


-- 
Tore Aursand - tore@extend.no - www.aursand.no


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

Date: 13 Jul 2001 13:58:49 -0700
From: nico@arzoon.com (nico gianniotis)
Subject: warn() does not use a localized STDERR filehandle, how come?
Message-Id: <d71d1211.0107131258.586138e3@posting.google.com>

Hi,

Does anyone know why localizing STDERR, before reopening it, causes
unexpected results from warn()? For example,

    open(STDERR, ">/tmp/z");
    warn "this goes into /tmp/z\n";
    close(STDERR);

works as expected and desired.

But:

    {
        local *STDERR;
        open(STDERR, ">/tmp/z");
        warn "oops! this comes out the original stderr! why?\n";
	print STDERR "however, this goes into /tmp/z as expected\n";
	close(STDERR);
    }

It would appear that warn() might not use the dynamically scoped value
of the STDERR filehandle directly, but instead might use some cached
version of it?
Is there a way to make this work?

Thanks in advance for any explanations.
nico


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

Date: Fri, 13 Jul 2001 21:20:39 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Where is MD5
Message-Id: <3B4F6678.B101EF2@acm.org>

gnari wrote:
> 
> In article <1ewgfan.1uylxjh49fmeqN%otto.wyss@bluewin.ch>,
> Otto Wyss <otto.wyss@bluewin.ch> wrote:
> 
> [
> Otto: you are still skipping the attribution.
> the attribution is the bit that says 'In article blabla, suchandsuch says:'
> ]
> 
> >> >perldoc *MD5*
> >>
> >> personally, i often use lthe command locate for this purpose.
> >> for example:  locate MD5.pm
> >> on your system you may have something different, but most have
> >> some way of searching for files
> >>
> >That does not help if the name isn't exactly known. That is one of the
> >biggest problem a nonexpert has to face.
> 
> then there is the good old (on unix):
> find perl -e 'print "@INC"' -name '*MD5.pm' -print
> 
> or for ALL modules in @INC:
> find perl -e 'print "@INC"' -name '*.pm' -print

You forgot the back quotes.

find `perl -e 'print "@INC"'` -name '*MD5.pm' -print

find `perl -e 'print "@INC"'` -name '*.pm' -print



John
-- 
use Perl;
program
fulfillment


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

Date: Fri, 13 Jul 2001 23:59:15 +0300
From: "Haim Lichaa" <haim.lichaa@intel.com>
Subject: Why is socket timeout not working?
Message-Id: <9innfd$d4m@news.or.intel.com>

I'm using this perl snipit on a Solaris 2.51 box running Perl 5.00403

my($s)=IO::Socket::INET->new(
  PeerAddr        => $host,
  PeerPort        => $port,
  Proto           =>'tcp',
  Type            => SOCK_STREAM,
  Timeout         => $to);

and any $to value has no affect on the Timeout. It remains constat @ ~4mins.
Any body know why and how I can fix it?








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

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.  

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 1302
***************************************


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