[18796] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 964 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed May 23 03:13:10 2001

Date: Wed, 23 May 2001 00:10:16 -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: <990601816-v10-i964@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Wed, 23 May 2001     Volume: 10 Number: 964

Today's topics:
    Re: Redirecting STDERR to sendmail <pne-news-20010523@newton.digitalspace.net>
        Scripting help (Flipper)
    Re: Scripting help <krahnj@acm.org>
    Re: Selectively output records from delimited file? <godzilla@stomp.stomp.tokyo>
    Re: Selectively output records from delimited file? <godzilla@stomp.stomp.tokyo>
    Re: SET-UP (free) (Bob Moose)
    Re: SET-UP (free) <uri@sysarch.com>
    Re: setting priority from perl on linux <dball@bnb-lp.com>
    Re: Simple sorting problem <uri@sysarch.com>
    Re: Sorting data file on numerical field <vmurphy@Cisco.Com>
    Re: Test for integer? <goldbb2@earthlink.net>
    Re: Time validation <pne-news-20010523@newton.digitalspace.net>
    Re: url parsing (Alan Barclay)
        Using Open3 to read/write to program in Win32 <anomie@my-deja.com>
    Re: Warning/Danger -- Don't use Perl for CGI <comdog@panix.com>
        why doesn't this regex do what I think it should? <nospam@cfl.rr.com>
    Re: why doesn't this regex do what I think it should? (E.Chang)
    Re: why doesn't this regex do what I think it should? (Mark Jason Dominus)
    Re: why doesn't this regex do what I think it should? (E.Chang)
    Re: why doesn't this regex do what I think it should? <godzilla@stomp.stomp.tokyo>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 23 May 2001 07:35:18 +0200
From: Philip Newton <pne-news-20010523@newton.digitalspace.net>
Subject: Re: Redirecting STDERR to sendmail
Message-Id: <voimgtgeobv096qhfbf425shpd4v2ufgms@4ax.com>

On 22 May 2001 18:41:24 +0100, nobull@mail.com wrote:

> You probably should unbuffer STDERR too.

Why? It's usually unbuffered by default.

Cheers,
Philip
-- 
Philip Newton <nospam.newton@gmx.li>
That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.


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

Date: Wed, 23 May 2001 06:13:55 GMT
From: twoflippers@icqmail.com.spamfilter (Flipper)
Subject: Scripting help
Message-Id: <3b0b54dc.13974487@news.uwa.edu.au>

G'day all

I am pretty new to Perl and i found a script ont he internet which
acts as a fake PHF server

can someone deciher this for me please?

if ($ENV{"QUERY_STRING"} =~ /(/|%2f)passwd/i) 

what is the string the script is looking for and how is it executed?

Thanks in advance
David


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

Date: Wed, 23 May 2001 06:36:55 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Scripting help
Message-Id: <3B0B5A87.BDE3131F@acm.org>

Flipper wrote:
> 
> G'day all
> 
> I am pretty new to Perl and i found a script ont he internet which
> acts as a fake PHF server
> 
> can someone deciher this for me please?
> 
> if ($ENV{"QUERY_STRING"} =~ /(/|%2f)passwd/i)
> 
> what is the string the script is looking for?

/(/

> how is it executed?

It doesn't, it's a syntax error.


John
-- 
use Perl;
program
fulfillment


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

Date: Tue, 22 May 2001 21:12:02 -0700
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Selectively output records from delimited file?
Message-Id: <3B0B3892.A134073@stomp.stomp.tokyo>

Chris Ashley wrote:

(snipped)
 
> I'm in the middle of developing a CGI quiz application at the moment.

> a comma delimited text file like so:
 
> name,email,result
> newname,newemail,newresult
 
> This works fine for me, but I'm now looking to do a high score table
> of sorts - I need to read that file with perl, and only output the top
> 10 results fields along with the other details. 

> where do I start?


You may start by rethinking what you ask.

There is a very good chance many quiz takers
will attain an identical score.

Searching for the top ten, the top four, searching
for whatever, won't work, semantically. How will
you sort the literal top four if eight people have
identical high scores?

"Who scored in the top four percent?"

"Who scored seven or better?"

Get the idea?

In my test script following my signature, I have
used $top_scores = 4; to represent user input
inquiring about those scoring within the top
four percent; scores of seven or better.

My @Top_Scores = (0 .. 10); simply sets a score
range of zero to ten, inclusively. Obviously,
the top four percent would be a score of seven
or better.

Concept is to establish a minimum score and work
from there. My script simply references seven
as the fourth element reading right to left.

You don't need an array to do this. A user could
ask for scores of seven or better:

"Who scored seven or better?"

$top_scores = 7;

My while loop would be changed to find scores
of seven or better, with no need for an array.

if (substr ($_, rindex ($_, ",") + 1) >= $top_scores)

There are many ways to establish a baseline score.
This is a matter of personal perference and how
you would like your wording of user inquiry.

What is important here, instead of reading the entire
contents of my database into an array, then going
from there, I am using an efficient while loop.
This is much faster for larger databases. For smallish
reads, efficiency differences are diddly-squat.

Another good feature is use of substring to address
your scores; no need to split into variables. This
is very quick and very efficient.

This use of @Results of mine, is to exemplify how
you can push your results into an array for later
sorting. With this array, you can offer your user
sorting by name, by email or by score. Easy enough
to sort on any one of your three criteria, then
split out your results.

Give some consideration to while looping your data
rather than slurping it. Slurpies are unhealthy
unless you only take a small sip.



Godzilla!
--

#!perl

$top_scores = 4;

@Top_Scores = (0 .. 10);

$limit = $Top_Scores[-$top_scores];

open (DATABASE, "database.txt");

while (<DATABASE>)
 {
  if (substr ($_, rindex ($_, ",") + 1) >= $limit)
   { push (@Results, $_); }
 }

close (DATABASE);

print " @Results";


exit;

CONTENTS OF database.txt:
_________________________

Billyray,email,9
Rubymae,email,7
Kiralynne,email,10
Velmajean,email,6
Lindal,email,6
Weldon,email,4
Tracilynne,email,8
Loriann,email,7
Nancyann,email,8
Frank,email,0


PRINTED RESULTS:
________________

 Billyray,email,9
 Rubymae,email,7
 Kiralynne,email,10
 Tracilynne,email,8
 Loriann,email,7
 Nancyann,email,8


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

Date: Tue, 22 May 2001 22:07:00 -0700
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Selectively output records from delimited file?
Message-Id: <3B0B4574.EDBA7A3@stomp.stomp.tokyo>

Godzilla! wrote:
 
> Chris Ashley wrote:
 
> (snipped)

(ditto)

> inquiring about those scoring within the top
> four percent; scores of seven or better.
 
> the top four percent would be a score of seven
> or better.

FORE! Forty percent, ahem.

Godzilla!


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

Date: 22 May 2001 18:28:12 -0700
From: yogibearxx@bobmoose.com (Bob Moose)
Subject: Re: SET-UP (free)
Message-Id: <4ec914b1.0105221728.fa611b4@posting.google.com>

Uri Guttman <uri@sysarch.com> wrote in message news:<x7elti6nuv.fsf@home.sysarch.com>...
> >>>>> "BM" == Bob Moose <yogibearxx@bobmoose.com> writes:
> 
>   BM> "flash" <bop@mypad.com> wrote in message news:<RlVN6.4790$eF6.368361@news20.bellglobal.com>...
>   >> No one will do that for you.
>   >> 
>   >> You should learn to do stuff on your own.
>   >> "Bob Moose" <yogibearxx@bobmoose.com> wrote in message
>   >> news:4ec914b1.0105200757.2302ca2c@posting.google.com...
>   >> > Can somebody set up http://www.kastle.net/products/URLredirect/
>   >> > For FREE,
>   >> > set up at http://www.bobmoose.com,
>   >> > when done send it to http://bobmoose.com/publicftpspace,
>   >> > then i will chmod it and put it in the CGI-BIN
> 
>   BM> Well, I'll ask again. Can somebody?
> 
> i will (for my definition of free which may not be yours. :)
> 
> uri



Uri Guttma, Please install the script at
http://bobmoose.com/publicftpspace (with MSIE's folderview)
And what's your defenition of free?


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

Date: Wed, 23 May 2001 01:39:39 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: SET-UP (free)
Message-Id: <x7itis6dyd.fsf@home.sysarch.com>

>>>>> "BM" == Bob Moose <yogibearxx@bobmoose.com> writes:

  BM> Uri Guttman <uri@sysarch.com> wrote in message news:<x7elti6nuv.fsf@home.sysarch.com>...
  BM> Well, I'll ask again. Can somebody?
  >> 
  >> i will (for my definition of free which may not be yours. :)

  BM> And what's your defenition of free?

not yours.

uri

-- 
Uri Guttman  ---------  uri@sysarch.com  ----------  http://www.sysarch.com
SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Learn Advanced Object Oriented Perl from Damian Conway - Boston, July 10-11
Class and Registration info:     http://www.sysarch.com/perl/OOP_class.html


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

Date: Tue, 22 May 2001 18:06:50 -0700
From: David Ball <dball@bnb-lp.com>
Subject: Re: setting priority from perl on linux
Message-Id: <h53mgtk7tpkcr8baq78mpl8r8qpuj3hcap@4ax.com>

On 22 May 2001 19:37:51 -0500, Tony Curtis <tony_curtis32@yahoo.com>
wrote:

[snip]
>You just have to be nice!
>
>    perldoc POSIX
>
>       nice

Thanks!
>
>Building some kind of database index would be a better way
>of doing this, otherwise you'd have at least linear search
>time for *every* access.

Do you know of any scripts that will build an index of html pages
and then search it from a CGI ?

-- David



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

Date: Wed, 23 May 2001 03:47:15 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Simple sorting problem
Message-Id: <x7bsok681o.fsf@home.sysarch.com>

>>>>> "AT" == Abe Timmerman <abe@ztreet.demon.nl> writes:

  AT> On Wed, 16 May 2001 15:26:35 -0400, Young Chi-Yeung Fan
  AT> <yf32@cornell.edu> wrote:

  >> 1, 2, 3, ..., 14, 15, 16, A, B
  >> 
  >> I get this sorted result:
  >> B, A, 1, 2, ..., 15, 16

  >> What I want is this:
  >> 1, 2, 3, ..., 14, 15, 16, A, B

  AT> So, my first thought was to use GRT to sort the keys for the general
  AT> case where (natural) numbers come before (other) strings.

  AT> This was the point where I went back to reading appendix B of "A Fresh
  AT> Look at Efficient Perl Sorting"[1] and usually find an answer as to
  AT> how-to pack/unpack the data for GRT.

  AT> Although I found a solution for this example data, I didn't find a
  AT> answer for the general case I stated above.

i contacted my co-author, larry rosler (who doesn't hang out here
anymore) and he came up with this solution:

------------------
my @s_keys = map { substr $_, 4 } sort
	map { pack( 'N' => /^(\d+)$/ ? $1 : -1 ) . $_ }
	keys %data;

The idea of the solution is essentially correct, though encoded
inefficiently (unnecessary copying of the alphanumeric strings).  The
main point is that -1 packed 'N' produces the *largest* value
("\xFFFF"), so the alphabetics sort themselves high relative to the
sorted numerics.
------------------

uri

-- 
Uri Guttman  ---------  uri@sysarch.com  ----------  http://www.sysarch.com
SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Learn Advanced Object Oriented Perl from Damian Conway - Boston, July 10-11
Class and Registration info:     http://www.sysarch.com/perl/OOP_class.html


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

Date: 22 May 2001 21:26:28 -0400
From: Vinny Murphy <vmurphy@Cisco.Com>
Subject: Re: Sorting data file on numerical field
Message-Id: <m3heycn9dn.fsf@vpnrel.cisco.com>

usenet@cox99.screaming.net (Graham Cox) writes:

> I have a data file that I am trying to display as an HTML table from a
> CGI script. What I want to do is to sort the data file on the 6th
> column before displaying it. The problem is that what little code I've
> been able to put together seems to insist on sorting is as if the 6th
> column was text, when I need to sort it as a number. Help :)
> 
> The data file I'm trying to sort is as follows:
> 
> a|1|2|3|4|5
> bob|5|4|3|2|1
> c|3|5|8|1|8
> d|99|99|99|99|99
> e|42|24|42|24|62

You need to use the spaceship operator.  See perldoc -f sort for more
details.

#!/usr/bin/perl -w
my @r = ();
push(@r,[ split '\|' ]) while <DATA>;
print join('|', @$_) for (sort{$a->[5]<=>$b->[5]}@r);
  
__DATA__
a|1|2|3|4|5
bob|5|4|3|2|1
c|3|5|8|1|8
d|99|99|99|99|99
e|42|24|42|24|62


Output:
bob|5|4|3|2|1
a|1|2|3|4|5
c|3|5|8|1|8
e|42|24|42|24|62
d|99|99|99|99|99

-- 
Vinny


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

Date: Wed, 23 May 2001 03:03:56 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Test for integer?
Message-Id: <3B0B60DC.61ABD6B3@earthlink.net>

Rob wrote:
> 
> Hi all!
> 
> Is there a way for me to test if a scalar holds an integer value
> (positive or negative)?
> 
> Thanks!
> 
> Rob

sub int_test {
	my $x = shift;
	my $notint = 0;
	local ($^W, $SIG{__WARN__}) = (1, sub{$notint = 1;});
	return $x != int $x or $notint;
}

This untested sub should say that "123", "123.0" and "123foo" are
integers, and "foo" and "12.3" are not integers.

-- 
Customer: "I would like to try on that suit in the window."
Salesman: "Sorry sir, you will have to use the dressing room."


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

Date: Wed, 23 May 2001 07:35:17 +0200
From: Philip Newton <pne-news-20010523@newton.digitalspace.net>
Subject: Re: Time validation
Message-Id: <jdimgtkjlgfkv98i941hg0og2coko1cp74@4ax.com>

On Tue, 22 May 2001 18:28:38 +0200, Per Kistler <kistler@gmx.net> wrote:

> Daryl Shute wrote:
> > I have a form that needs to be submitted only during the hours of 0600 -
> > 1515 Monday through Friday, and not on Holidays.
> 
> ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime(time); 

Ick. Use a slice.

    my($min, $hour, $day, $mon, $year, $dayofweek) = (localtime)[1..6];

You don't need seconds, day-of-year, or is-DST. At first I thought you
didn't need day-of-month, month, or year, either, but you'd need that
for holidays. But rather than calculating things such as Easter (movable
feast!) yourself, you may want to use one of the Date:: modules.

Cheers,
Philip
-- 
Philip Newton <nospam.newton@gmx.li>
That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.


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

Date: 23 May 2001 00:44:21 GMT
From: gorilla@elaine.furryape.com (Alan Barclay)
Subject: Re: url parsing
Message-Id: <990578629.29590@elaine.furryape.com>

In article <jPzO6.22278$BN6.931952@newsread1.prod.itd.earthlink.net>,
Patrick Joyce <joycefive@earthlink.net> wrote:
>Hey, my name is patrick joyce and im a newbe to perl.
>I need a program that takes a url and strips out everything but the domain
>this would normally not be as hard but i need this to work for such urls as
>".com.au, .co.jp" and "foo.me.com, hi.test.you.com" so if my url was
>http://www.tiac/net/users/candvr

Do you want it to give the assigned part? so www.something.co.uk would be
something.co.uk, but www.something.part.com would be part.com?

This is very hard to do. Some domains have a fixed length, so for
example in .com you know it's always the second last part, but
other domains have variable allocations. In Canada, that could be
something.ca, something.on.ca, or something.toronto.on.ca.

Unless you have a list of every city & province in canada, then you
can't tell a city domain from an assigned domain.

There are similar problems in other domains around the world.


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

Date: Tue, 22 May 2001 18:28:38 -0700
From: "Jordan Reed" <anomie@my-deja.com>
Subject: Using Open3 to read/write to program in Win32
Message-Id: <3b0b1231@monitor.lanset.com>

I'm playing around with reading/writing to a program (in this case, PGP
version 6.5.8) on a WinNT 4.0 machine.
I've never fiddled with IPC:Open3 before, and wondering if I'm doing
something wrong.

Here's snippit from what I'm executing.  The problem is that no output I
send to the writer appears to make it to the program, though I can read the
input just fine.  I tried using Open3 with the "pause" program instead, and
it worked fine.  This makes me think that PGP is handling input in some
weird way.  Are there any tips/tricks about how else I might try writing to
the program to get it to see my output?

my $pid = open3(\*WRITER, \*READER, \*ERROR, "pgp -ka ac.asc");
sysread(READER, $line, 2048);
syswrite(STDOUT, "Got: $line\n");
syswrite(WRITER, "\n");
waitpid $pid, 0;

-jdr




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

Date: Wed, 23 May 2001 02:06:27 -0400
From: brian d foy <comdog@panix.com>
Subject: Re: Warning/Danger -- Don't use Perl for CGI
Message-Id: <comdog-4B3263.02062723052001@news.panix.com>

In article <%zCO6.57911$I5.12567590@news1.rdc1.tn.home.com>, "Todd 
Smith" <todd@designsouth.net> wrote:

> > In general, it's a very bad idea to use the Perl interpreter as a CGI.

> Did they say why?

that would allow you to execute arbitrary code just like passing
perl STDIN.

$ perl
print "Hello!\n";
^D
Hello!

-- 
brian d foy <comdog@panix.com>
CGI Meta FAQ - http://www.perl.org/CGI_MetaFAQ.html
Troubleshooting CGI scripts - http://www.perl.org/troubleshooting_CGI.html



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

Date: Wed, 23 May 2001 01:57:53 GMT
From: tuxy <nospam@cfl.rr.com>
Subject: why doesn't this regex do what I think it should?
Message-Id: <3B0B1AA4.3E9BB9D5@cfl.rr.com>


I have a scalar with multiple lines. I want to "split it" on a certain
line and delete ALL lines thereafter. In this example, I want to split
it on the "man manline" line, and delete thereafter. Seems like the
regex I'm employing should do just that, but instead it only deletes the
LAST line. Why the LAST line? I tried /g also.


use strict;
my $x='cat feline
man manline
dog canine
fish fishline
';

print "\n\n**********\n$x\n\n**********\n";

$x =~ s/(man.+\n).+$/$1/s;
print "$x\n\n";

[tux@tuxxe ~]$ ./x.pl


**********
cat feline
man manline
dog canine
fish fishline


**********
cat feline
man manline
dog canine


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

Date: Wed, 23 May 2001 02:46:59 GMT
From: echang@netstorm.net (E.Chang)
Subject: Re: why doesn't this regex do what I think it should?
Message-Id: <Xns90A9E833AEF27echangnetstormnet@207.106.93.86>

tuxy <nospam@cfl.rr.com> wrote in <3B0B1AA4.3E9BB9D5@cfl.rr.com>:

> 
> I have a scalar with multiple lines. I want to "split it" on a
> certain line and delete ALL lines thereafter. In this example, I
> want to split it on the "man manline" line, and delete thereafter.
> Seems like the regex I'm employing should do just that, but instead
> it only deletes the LAST line. Why the LAST line? I tried /g also.
> 
> 
> use strict;
> my $x='cat feline
> man manline
> dog canine
> fish fishline
> ';
> 
> print "\n\n**********\n$x\n\n**********\n";
> 
> $x =~ s/(man.+\n).+$/$1/s;
> print "$x\n\n";
> 
[snip]

The quantifier * does a "greedy" match, finding the largest pattern 
that matches.  To stop it at the first (smallest) match, follow the * 
with a ?.

$x =~ s/(man.+?\n).+$/$1/s;

See perldoc -q greedy for a more thorough explanation.

-- 
EBC


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

Date: Wed, 23 May 2001 03:21:46 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: why doesn't this regex do what I think it should?
Message-Id: <3b0b2cca.42f9$164@news.op.net>

In article <Xns90A9E833AEF27echangnetstormnet@207.106.93.86>,
E.Chang <echang@netstorm.net> wrote:
>$x =~ s/(man.+?\n).+$/$1/s;

        $x =~ s/(man[^\n]+\n).*$/$1/s;

will probably be a lot more efficient there.

-- 
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print


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

Date: Wed, 23 May 2001 03:52:02 GMT
From: echang@netstorm.net (E.Chang)
Subject: Re: why doesn't this regex do what I think it should?
Message-Id: <Xns90A9F33B9CB87echangnetstormnet@207.106.93.86>

mjd@plover.com (Mark Jason Dominus) wrote in 
<3b0b2cca.42f9$164@news.op.net>:

>In article <Xns90A9E833AEF27echangnetstormnet@207.106.93.86>,
>E.Chang <echang@netstorm.net> wrote:
>> $x =~ s/(man.+?\n).+$/$1/s;
>
>        $x =~ s/(man[^\n]+\n).*$/$1/s;
>
>will probably be a lot more efficient there.


Please disregard earlier version if it slipped past cancellation.

Doesn't look too different, or am I messing up something else? :)

use Benchmark qw(cmpthese);
cmpthese (3_000_000, {
   'ebc' => sub {my $x='cat feline
man manline
dog canine
fish fishline
'; $x =~ s/(man.+?\n).+$/$1/s;
},
   'mjd' => sub{ my $x='cat feline
man manline
dog canine
fish fishline
'; $x =~ s/(man[^\n]+\n).*$/$1/s;}
});

Benchmark: timing 2000000 iterations of ebc, mjd...
   ebc: 14 wallclock secs (13.07 usr +  0.00 sys = 13.07 CPU) @ 
153022.19/s
   mjd: 14 wallclock secs (13.45 usr +  0.00 sys = 13.45 CPU) @ 
148698.88/s


-- 
EBC


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

Date: Tue, 22 May 2001 23:31:06 -0700
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: why doesn't this regex do what I think it should?
Message-Id: <3B0B592A.6E69243C@stomp.stomp.tokyo>

tuxy wrote:

(snippage)
 
> I have a scalar with multiple lines. I want to "split it" on a certain
> line and delete ALL lines thereafter. In this example, I want to split
> it on the "man manline" line, and delete thereafter. 


> my $x='cat feline
> man manline
> dog canine
> fish fishline
> ';

> $x =~ s/(man.+\n).+$/$1/s;


You will discover use of index and substring to be nearly
infinitely more efficient for this task than a regex method.

My test script exemplifies two methods. First is a simple
search for your keyterm, manline. Second method shows how
to search backwards for the same keyterm and move forward
to the end of the line containing that keyterm.

This second method of mine makes use of rindex to show how
you can move backwards through a string and find a keyterm.
It will work the same moving forward with use of index.
Should you have two lines containing your keyterm, use of
rindex will find the last line containing your keyterm
where index will find the first line with your keyterm.
My intent here is to add a bit of flexibility, if needed.

This second method will pick up all terms within a key line,
regardless of what position your keyterm holds. An example:

man manline mankind mania

My second method would find "manline" then move to the
end of the line by indexing for "\n", catching all 
contained terms within that line. This method of
indexing for a newline could be used in my first
method, with easy modifications.

You could use "man" for a keyterm if you know your
data does not contain other words which include
man, such as our word human. Use of rindex for this
would be very effective by locating the last use
of "man" within a line. Exercise caution however.
An example of caution would be to index for,

" man"

This is man with a leading space. Easy huh?
You no longer have to worry about human concerns
unless a human comes first in line, thus caution
is always in order, even when you think not. Still
there is a fix for this if you look for a newline
without a human first in line, know what I mean?

A disadvantage of index and rindex, is inherent
case sensitivity. Spelling must include lowercase
and uppercase as needed. There are methods to work
around this, discovery of which, I will leave to you.

Use of index and substring is very powerful, very
efficient. Consider using them when you can. Sure
beats a regex method for most circumstances, easily.


Godzilla!
--

#!perl

$x='cat feline
man manline
dog canine
fish fishline
';

$key_term = "manline";
$x = substr ($x, 0, index ($x, "$key_term") + length ($key_term));

print "$x\n\n";

$x='cat feline
man manline
dog canine
fish fishline
';

$key_term = rindex ($x, "manline");
$x = substr ($x, 0, index ($x, "\n", $key_term));

print "$x\n\n";

exit;


PRINTED RESULTS:
________________

cat feline
man manline

cat feline
man manline


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

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


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