[10234] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3827 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Sep 25 17:07:21 1998

Date: Fri, 25 Sep 98 14:01:28 -0700
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Fri, 25 Sep 1998     Volume: 8 Number: 3827

Today's topics:
    Re: help with removing leading zeros with reg-exp Lee.Lindley@bigfoot.com
    Re: help with removing leading zeros with reg-exp droby@copyright.com
    Re: help with removing leading zeros with reg-exp droby@copyright.com
    Re: help with removing leading zeros with reg-exp (Larry Rosler)
    Re: help with removing leading zeros with reg-exp (Larry Rosler)
    Re: help with removing leading zeros with reg-exp (Matt Knecht)
        How to strip binary data from a file (SKa)
    Re: How to strip binary data from a file (Larry Rosler)
    Re: Learning Perl Advice (Ilya Zakharevich)
    Re: man pages for win32 perl <camerond@mail.uca.edu>
        Perl and ODBC? <topper@virginia.edu>
    Re: Poll: How Did You Learn Perl? (Patrick Timmins)
    Re: Redirection Question <zenin@bawdycaste.org>
        Regular Expression question... <shellymargolis@erols.com>
    Re: Regular Expression question... (Lloyd Zusman)
    Re: Regular Expression question... (Larry Rosler)
        Returning a "near" hash key. (Garrett Casey)
    Re: Returning a "near" hash key. (Karlon West)
    Re: Returning a "near" hash key. (Matt Knecht)
    Re: User inputted regexp. (Charles DeRykus)
    Re: Where to put cgi-lib.pl <uri@camel.fastserv.com>
        Special: Digest Administrivia (Last modified: 12 Mar 98 (Perl-Users-Digest Admin)

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

Date: Fri, 25 Sep 1998 19:20:56 GMT
From: Lee.Lindley@bigfoot.com
Subject: Re: help with removing leading zeros with reg-exp
Message-Id: <6ugqeo$jup$1@nnrp1.dejanews.com>

In article <6ugf6e$7gg@newsops.execpc.com>,
  Mark Stackhouse <stackhou@execpc.com> wrote:
> Could someone please clear up my misunderstanding of "tr"?
>
> I'm trying to remove leading zeros in the first six elements of file
> lines.  The closest code I can come up with is:
> [snip]
Try this:

my (@line) = qw/0001 0002 1000 0100 1111 extraarg6/;
map s/^0+//, @line[0..4];
print "@line \n";

# which results in the following output
1 2 1000 100 1111 extraarg6

--
// Lee.Lindley@Bigfoot.com
// Be nice.  It isn't that hard to do and it
// makes people happy.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum


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

Date: Fri, 25 Sep 1998 19:44:20 GMT
From: droby@copyright.com
Subject: Re: help with removing leading zeros with reg-exp
Message-Id: <6ugrqk$l8e$1@nnrp1.dejanews.com>

In article <sariuicw35o.fsf@camel.fastserv.com>,
  Uri Guttman <uri@camel.fastserv.com> wrote:
> >>>>> "MS" == Mark Stackhouse <stackhou@execpc.com> writes:
>
>   MS> Could someone please clear up my misunderstanding of "tr"?
>   MS> I'm trying to remove leading zeros in the first six elements of file
>   MS> lines.  The closest code I can come up with is:
>
>   MS> for ($k=0; $k<=5; $k++)	#check the first 6 elements
>   MS>  {
>   MS>    if ($line_element[$k] =~ /^0/) #match leading zeros
>   MS>     {
>   MS>      $line_element[$k] =~ tr/1-9//cd; #remove zeros
>
> try using s/^0+// instead of tr//
>
> hth,
>
> uri
>

Unfortunately, this will turn "000" into "" removing one zero more than
desired.

Try making it a number instead.

	$line_element[$k] =+ 0;

--
Don Roby

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum


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

Date: Fri, 25 Sep 1998 19:46:18 GMT
From: droby@copyright.com
Subject: Re: help with removing leading zeros with reg-exp
Message-Id: <6ugrua$ld8$1@nnrp1.dejanews.com>

In article <sariuicw35o.fsf@camel.fastserv.com>,
  Uri Guttman <uri@camel.fastserv.com> wrote:
> >>>>> "MS" == Mark Stackhouse <stackhou@execpc.com> writes:
>
>   MS> Could someone please clear up my misunderstanding of "tr"?
>   MS> I'm trying to remove leading zeros in the first six elements of file
>   MS> lines.  The closest code I can come up with is:
>
>   MS> for ($k=0; $k<=5; $k++)	#check the first 6 elements
>   MS>  {
>   MS>    if ($line_element[$k] =~ /^0/) #match leading zeros
>   MS>     {
>   MS>      $line_element[$k] =~ tr/1-9//cd; #remove zeros
>
> try using s/^0+// instead of tr//
>
> hth,
>
> uri
>

Unfortunately, this will turn "000" into "" removing one zero more than
desired.

Try making it a number instead.

	$line_element[$k] += 0;

--
Don Roby

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum


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

Date: Fri, 25 Sep 1998 13:01:51 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: help with removing leading zeros with reg-exp
Message-Id: <MPG.1075968b3b4271cb9897be@nntp.hpl.hp.com>

[Posted to comp.lang.perl.misc and a copy mailed.]

In article <BvRO1.532$7Q6.4688277@news2.voicenet.com> on Fri, 25 Sep 1998 
18:43:45 GMT, Matt Knecht <hex@voicenet.com> says...
 ...
> Mark Stackhouse <stackhou@execpc.com> wrote:
> >
> >I'm trying to remove leading zeros in the first six elements of file
> >lines.
> >
> >I want  "000" to be "0"
> >        "001" to be "1"
> 
> Everybody keeps forgetting "the first 6 elements".
> 
> s/^0{1,5}(?=\d)//  would work, as well as my original reply.

No, everybody is not forgetting "the first six elements".  You are.  See 
all the @line_element[0 .. 5] responses.

Your regex is working on up to 5 leading zeros, on every one of the 
elements of the array.

But several responders *did* forget about converting "000" to be "0" 
instead of "". :-)

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Fri, 25 Sep 1998 13:08:33 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: help with removing leading zeros with reg-exp
Message-Id: <MPG.1075981efc3596a59897bf@nntp.hpl.hp.com>

[Posted to comp.lang.perl.misc and a copy mailed.]

In article <6ugqeo$jup$1@nnrp1.dejanews.com> on Fri, 25 Sep 1998 19:20:56 
GMT, Lee.Lindley@bigfoot.com <Lee.Lindley@bigfoot.com> says...
 ...
> my (@line) = qw/0001 0002 1000 0100 1111 extraarg6/;
> map s/^0+//, @line[0..4];

1.  'map' in void context is wasteful -- slower than 'foreach'.
2.  '0..4' gets five elements, not six as requested (see below).
3.  "000" gets mapped to "", not "0" as requested (see below).

!!> I'm trying to remove leading zeros in the first six elements of file
!!> lines.
 ...
!!> I want 	"000" to be "0"

 ...
> --
> // Lee.Lindley@Bigfoot.com
> // Be nice.  It isn't that hard to do and it
> // makes people happy.

I'm trying to be nice, but that's a pretty high error rate for one line 
of code. :-)

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Fri, 25 Sep 1998 20:37:22 GMT
From: hex@voicenet.com (Matt Knecht)
Subject: Re: help with removing leading zeros with reg-exp
Message-Id: <6aTO1.537$7Q6.4747140@news2.voicenet.com>

Larry Rosler <lr@hpl.hp.com> wrote:
>> Mark Stackhouse <stackhou@execpc.com> wrote:
>> >
>> >I'm trying to remove leading zeros in the first six elements of file
>> >lines.
>
>No, everybody is not forgetting "the first six elements".  You are.  See 
>all the @line_element[0 .. 5] responses.

I misparsed his request not once, but twice.  For some reason I
interpreted what he was asking as "The first 6 bytes of every string",
not "The first 6 array elements".

Time to up my caffiene intake.

>Your regex is working on up to 5 leading zeros, on every one of the 
>elements of the array.

As was intended! :)

-- 
Matt Knecht - <hex@voicenet.com>


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

Date: Fri, 25 Sep 1998 19:09:50 GMT
From: stefank@wirehub.nl (SKa)
Subject: How to strip binary data from a file
Message-Id: <360be9ed.3523045@news.wirehub.nl>

Hi there,

Can anybody tell me how to remove (or ignore) lines of binary data
from a file? I am reading the file line by line, but when the program
encounters some binary data, it stops.

I hope anybody knows

SKa


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

Date: Fri, 25 Sep 1998 13:18:26 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: How to strip binary data from a file
Message-Id: <MPG.10759a6d552369919897c0@nntp.hpl.hp.com>

[Posted to comp.lang.perl.misc and a copy mailed.]

In article <360be9ed.3523045@news.wirehub.nl> on Fri, 25 Sep 1998 
19:09:50 GMT, SKa <stefank@wirehub.nl> says...
 ...
> Can anybody tell me how to remove (or ignore) lines of binary data
> from a file? I am reading the file line by line, but when the program
> encounters some binary data, it stops.

perlfaq4:  "How do I handle binary data correctly?"

You must be using a Windows/DOS system,  which has the unfortunate 
characteristic of distinguishing 'binary' data from 'text' data.  There 
must be a control-Z character in your data that is interrupting the 
'read'.

To fix this, see `perldoc -f binmode`.  Then the reads will continue, and 
you can deal with lines you don't like any way you wish.

How many times has this been this week???

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: 25 Sep 1998 20:57:20 GMT
From: ilya@math.ohio-state.edu (Ilya Zakharevich)
Subject: Re: Learning Perl Advice
Message-Id: <6uh03g$s7a$1@mathserv.mps.ohio-state.edu>

[A complimentary Cc of this posting was sent to Zenin 
<zenin@bawdycaste.org>],
who wrote in article <906730994.820841@thrush.omix.com>:
> 	$ perlindex socket
> 	1  2.121 lib/perl5/i386-freebsd/5.00404/IO/Socket.pm
> 	2  2.121 lib/perl5/site_perl/LWP/SecureSocket.pm
> 	3  2.121 lib/perl5/site_perl/LWP/Socket.pm
> 	4  2.121 lib/perl5/i386-freebsd/5.00401/IO/Socket.pm
> 	5  2.121 lib/perl5/IO/Socket.pm
> 	6  2.121 lib/perl5/pod/perlipc.pod
> 	7  1.061 lib/perl5/Socket.pm
> 	8  1.061 lib/perl5/i386-freebsd/5.00401/Socket.pm
> 	9  1.061 lib/perl5/i386-freebsd/5.00404/Socket.pm
> 	a  0.998 lib/perl5/ExtUtils/Embed.pm
> 	b  0.943 lib/perl5/site_perl/Net/DummyInetd.pm
> 	c  0.795 lib/perl5/site_perl/HTTP/Daemon.pm
> 	d  0.742 lib/perl5/IO/Select.pm
> 	e  0.707 lib/perl5/i386-freebsd/5.00401/IO.pm
> 	f  0.707 lib/perl5/i386-freebsd/5.00404/IO.pm
> 
> 	Enter Number or 'q'> 5
> 	Running pod2man /usr/local/lib/perl5/IO/Select.pm
> 		...man page for IO::Select is shown...

This may be marginally better than the IBM online-book system (which
does not sort by relevance - though I doubt these numbers mean much).
Perl docs are available in this form, there are viewers for OS/2
(it is the native documentation system there) and Win*.

In spring some people volunteered to write CGI which would present
Perl docs in similarly useful way, but I did not see any result coming
out of this...

Ilya


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

Date: Fri, 25 Sep 1998 15:49:54 -0500
From: Cameron Dorey <camerond@mail.uca.edu>
To: Peter Smith <psmith01@mindspring.com>
Subject: Re: man pages for win32 perl
Message-Id: <360C01F2.9DC568F8@mail.uca.edu>

[cc'd to ps]

Sorry for the delay, Peter, the newsserver's feed which we use is
seriously broken, and your note just showed up here along with
everything from 9/11 (2 weeks late). I don't have the 5.00502 version,
mainly because there seem to be a number of unresolved issues (yours
being one). It looks like ActiveState decided not to make perldoc
functional (going completely with HTML files?) in the new "unified"
version.

ActiveState is still apparently running things similarly to how they did
before, when there were two "tracks" for Perl in Window$. I hope that
they release a version which is analogous to GS's 5.00402, which worked
as nearly as possible to Standard Perl. That way we can use a lot of the
methods/tips/shortcuts discussed on clpm, et al, instead of having to
use "our own." (I saw a rumor that GS is going to be working for
ActiveState, I hope it's right if he will push them in this direction.)

Anyway, if you want to use perldoc, what I would suggest (although I
have NOT tried it because I do not have 5.00502) is to install GS's
5.004 (from your local CPAN mirror), use 5.005, and use perldoc from
5.004, you might have to do some mucking around with paths in order to
get it to find everything. Since the basic functionality should be the
same (5.005 mostly made things work "better," didn't it, and shouldn't
have completely changed syntax, etc., anyway the Win32 modules now all
have the 5.004 syntax, I think), the docs from 5.004 ought to be pretty
close for almost all things.

Cameron
camerond@mail.uca.edu

Peter Smith wrote:
> 
> I have the problem of 'perldoc -f <whatever>' returning this:
> 'No documentation found for "perlfunc".'
> 
> Seems like I may have a path problem or something.
> 
> I'm on Win95 4.00.950 B and my 'perl -v' is:
> 'This is perl, version 5.005_02 built for MSWin32-x86-object
> Copyright 1987-1998, Larry Wall
> Binary build 502 provided by ActiveState Tool Corp.
> http://www.ActiveState.com
> Built 10:59:41 Aug 11 1998...'
> 
> Anyone have an idea what that error message means and how to go about
> fixing??
> 
> Thanks.
> 
> Cameron Dorey wrote:
> 
> > Dan Nguyen wrote:
> > >
> > > [snip]
> > > I'm not sure if Windows people have perldoc.  But 'perldoc
> > > -f print' is the correct way of looking it up.
> >
> > GS port (5.004) has perldoc, AS port (5.003) does not. I don't know
> > about the new AS port (5.005).
> >
> > Perldoc can be a *lot* easier than running through the HTML pages when
> > you just want to look up something quick.
> >
> > Cameron
> 
> --
> --Peter--
> psmith01@mindspring.com


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

Date: Fri, 25 Sep 1998 16:06:36 -0400
From: "David J. Topper" <topper@virginia.edu>
Subject: Perl and ODBC?
Message-Id: <360BF7CC.D1A9DDE6@virginia.edu>

Hello there,

I'm trying to convince my boss to use perl instead of cold fusion.  The
snags are:

a)  he needs to use NT (ok ... there is perl for NT)
b)  he wants to talk with an MS Access database (preferrably with ODBC)

I've heard there are other mechanisms for talking with MS Access, but he
seems to really want ODBC stuff.  I don't even know what the hell the
difference is. We used to use sybperl back at Time Inc., then later some
special functions that opened connections with a Sybase database.

Any input would be appreciated.

Thanks,

Dave Topper
--
Technical Director, Virginia Center for Computer Music
Programmer / Analyst, Dean's Office (School of A&S)
http://www.panix.com/~topper
(804) 924-6887


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

Date: Fri, 25 Sep 1998 19:37:32 GMT
From: ptimmins@netserv.unmc.edu (Patrick Timmins)
Subject: Re: Poll: How Did You Learn Perl?
Message-Id: <6ugrds$ks8$1@nnrp1.dejanews.com>

In article <6udrue$fti$1@marina.cinenet.net>,
  cberry@cinenet.net (Craig Berry) wrote:
[snip]
> No, no, you don't understand.  The peanut butter itself *is* a compressed
> version of the latest Perl distribution, using a new molecular encoding
> scheme.  Only the chunky version includes the docs, however.

In addition to this, I've been receiving Perl patches encrypted on
the bottom of 'Snickers' candy bars for the past two years now. I
don't know how they figure out which actual Snickers I'll buy,
because my wife says she's never seen it on hers.

I've even tried buying them from vending machines in busy places,
and I still get an encrypted one everytime. It is a mystery to me,
but I guess the Truth is out there, and I'll just have to keep
checking more Snickers. But it *is* wreaking havoc on my formerly
fine-tuned line-backer of a physique.


> : Will it add much to the cost of Skippy?
>
> No, the added software is free-in-the-Jif-sense...that is, it doesn't cost
> anything.

Actually, Activestate's Perl is encoded in JIF peanut butter.
It's for "choosey mothers".

Patrick Timmins
$monger{Omaha}[0]

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum


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

Date: 25 Sep 1998 19:22:52 GMT
From: Zenin <zenin@bawdycaste.org>
Subject: Re: Redirection Question
Message-Id: <906751283.802672@thrush.omix.com>

Tom Turton <tturton@cowboys.anet-dfw.com> wrote:
	>snip<
: #!/usr/local/gnu/bin/perl -w

	Good.

: use diagnostics;

	Good.

	...

	Where is use strict?

: open STDERR, ">/home/tturton/PERL/error.log";

	Bad.

:     open (STDERR, ">-");      # ports it to STDOUT, instead of default STDERR

	Bad.

: open STDERR, ">&STDOUT";

	Bad.

	What happends if you add "or die $!" to the end of all those open()s?

-- 
-Zenin (zenin@archive.rhps.org)           From The Blue Camel we learn:
BSD:  A psychoactive drug, popular in the 80s, probably developed at UC
Berkeley or thereabouts.  Similar in many ways to the prescription-only
medication called "System V", but infinitely more useful. (Or, at least,
more fun.)  The full chemical name is "Berkeley Standard Distribution".


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

Date: Fri, 25 Sep 1998 15:32:49 -0400
From: Greg Feingold <shellymargolis@erols.com>
Subject: Regular Expression question...
Message-Id: <360BEFE1.95EFB7C2@erols.com>

Can someone tell me how to check a string to make sure it only contains
numbers and letters?

I thought it was:

if ($PARAMS('userid') != /^[A-Za-z0-9]+$/)
    print "Userid is invalid";

Greg



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

Date: 25 Sep 1998 20:30:29 GMT
From: ljz@asfast.com (Lloyd Zusman)
Subject: Re: Regular Expression question...
Message-Id: <slrn70nv9e.l1g.ljz@sunspot.tiac.net>

On Fri, 25 Sep 1998 15:32:49 -0400, Greg Feingold <shellymargolis@erols.com> wrote:
> Can someone tell me how to check a string to make sure it only contains
> numbers and letters?
> 
> I thought it was:
> 
> if ($PARAMS('userid') != /^[A-Za-z0-9]+$/)
>     print "Userid is invalid";
> 

When doing a regular expression match, it's incorrect to use the
numeric equality/inequality operators ('=' and '!=').  Instead, you
use the "match" and "doesn't match" operators: '=~' and '!~'. Also,
when dereferencing a hash (which is what '$PARAMS' is in your
example), you don't use parentheses, but rather, you use curly braces.
Therefore, this is one of the possible ways you can write your
statement:

  if ($PARAMS{'userid'} !~ /^[A-Za-z0-9]+$/)
      print "Userid is invalid";


-- 
 Lloyd Zusman
 ljz@asfast.com
 God bless you.


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

Date: Fri, 25 Sep 1998 13:45:23 -0700
From: lr@hpl.hp.com (Larry Rosler)
Subject: Re: Regular Expression question...
Message-Id: <MPG.1075a0bcf1ad8b009897c3@nntp.hpl.hp.com>

[Posted to comp.lang.perl.misc and a copy mailed.]

In article <360BEFE1.95EFB7C2@erols.com> on Fri, 25 Sep 1998 15:32:49 -
0400, Greg Feingold <shellymargolis@erols.com> says...
> Can someone tell me how to check a string to make sure it only contains
> numbers and letters?
> 
> I thought it was:
> 
> if ($PARAMS('userid') != /^[A-Za-z0-9]+$/)
>     print "Userid is invalid";

It looks as though you are a C programmer:  != and no braces. :-)

  if ($PARAMS{userid} !~ /^[A-Za-z0-9]+$/)
      { print "Userid is invalid"; }

or:

  unless ($PARAMS{userid}  =~ /^[A-Za-z0-9]+$/)
      { print "Userid is invalid"; }


or (without extra punctuation):

 print "Userid is invalid" unless $PARAMS{userid} =~ /^[A-Za-z0-9]+$/;

etc.

Of course, these ignore possible localizations (characters not in seven-
bit ASCII that the user considers to be letters).  To include them, 
change the regex to:

/^[^\W_]+$/

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Fri, 25 Sep 1998 19:23:27 GMT
From: nospamno_adms1@cts.com (Garrett Casey)
Subject: Returning a "near" hash key.
Message-Id: <360beab2.360976707@nntp.cts.com>

I haven't used Foxpro for DOS in a while, but you were able to create
an Index, set "NEAR ON" and if you seeked the index and the key wasn't
found, the record pointer would remain at the closest possible match.

Is there a way to do this with hash var?  If the key doesn't exist,
return the closest matching key (as if keys were in sort order). Is
there already a module available with hash utilities like this?  

My code goes like this:

Populate hash with many random generated strings as keys.
Generate random_search_string.

$f = getKey($hash{$random_search_string));

If $random_search_string is not a key, $f should equal the closest
find.  So, is there something like getKey() available?

Thank you.
Garrett Casey
nospamo_adms1@cts.com
Remove "nospamo_" To send me email.


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

Date: 25 Sep 1998 20:09:31 GMT
From: karlon@bnr.ca (Karlon West)
Subject: Re: Returning a "near" hash key.
Message-Id: <6ugt9r$aum@crchh14.us.nortel.com>

Garrett Casey (nospamno_adms1@cts.com) wrote:
> I haven't used Foxpro for DOS in a while, but you were able to create
> an Index, set "NEAR ON" and if you seeked the index and the key wasn't
> found, the record pointer would remain at the closest possible match.

> Is there a way to do this with hash var?  If the key doesn't exist,
> return the closest matching key (as if keys were in sort order). Is
> there already a module available with hash utilities like this?  

> My code goes like this:

> Populate hash with many random generated strings as keys.
> Generate random_search_string.

> $f = getKey($hash{$random_search_string));

> If $random_search_string is not a key, $f should equal the closest
> find.  So, is there something like getKey() available?

Here's something that works on Unix using Yellow Pages,
but I'm mainly posting to see the drastic improvements
that others will make. Also, though I'm prolly committing
a sin, I didn't scan the perl docs or CPAN to see if
this is already done :)

#!/usr/bin/perl -w

#generate hash of random strings
open(PW,"ypcat passwd|") or die "can't exec yppasswd";
while(<PW>)
{
   next if /^\s*$/;
   ($key)=split(/:/);
   $hash{$key}=$_;
}
close(PW);

# return match, or nearest match of hash
sub getKey
{
   my $key=shift;
   return $key if exists $hash{$key};
   my $last;
   my @tmp = sort keys %hash, $key;
   foreach (@tmp)
   {
      return $tmp[1] if $_ eq $key and !defined $last;
      return $last if $_ eq $key;
      $last=$_;
   }
   die "something wierd happened :(";
}

$f1 = getKey($ENV{'LOGNAME'});
$f2 = getKey("missing_key");

print "$ENV{'LOGNAME'} = $f1, missing_key = $f2\n";



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

Date: Fri, 25 Sep 1998 20:31:59 GMT
From: hex@voicenet.com (Matt Knecht)
Subject: Re: Returning a "near" hash key.
Message-Id: <35TO1.536$7Q6.4747100@news2.voicenet.com>

Garrett Casey <nospamno_adms1@cts.com> wrote:
>I haven't used Foxpro for DOS in a while

Congratulations! :)

>an Index, set "NEAR ON" and if you seeked the index and the key wasn't
>found, the record pointer would remain at the closest possible match.
>
>Is there a way to do this with hash var?  If the key doesn't exist,
>return the closest matching key (as if keys were in sort order). Is
>there already a module available with hash utilities like this?  

As far as I know, you'd have to grow you own solution.  Here's one
attempt (It's not very perly, though):

## UNTESTED
$f = near_on(\%some_hash, $key_to_look_for);

sub near_on
{
    my $hash = shift;
    my $key  = shift;

    return $key if exists $hash->{$key};

    my $last_key = $key;

    for (sort keys %$hash) {
        return $last_key if (($_ cmp $key) == 1);
        $last_key = $_;
    }

    $last_key;
}

You'd do well to build some sort of cache around this (Or use the
excellent Memoize module on CPAN by Mark-Jason Dominus).

-- 
Matt Knecht - <hex@voicenet.com>


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

Date: Fri, 25 Sep 1998 20:09:45 GMT
From: ced@bcstec.ca.boeing.com (Charles DeRykus)
Subject: Re: User inputted regexp.
Message-Id: <Ezuw09.MD7@news.boeing.com>

In article <36026D73.4F19B19E@nortel.com>,
Michael Bowler  <mkbowler@nortel.com> wrote:
>How can I verify a regular expression given by the user for validity?
>
>Code snippet.
>$exp = <STDIN>;
>chop $exp;
>if ($data =~ /$exp/) {
>   #Do something.
>}
>
>This works great until the user types something beginning with a single asterisk
>or something else invalid.  Is there a way for me to validate $exp (or at least
>gracefully catch the error)?
>

perldoc -f eval for complete details.   
 
For example, you could do something like this:

   chop($exp = <STDIN>);
   eval { 
      if ($data =~ /$exp/) {
           ....
      }
   };
   if ($@ =~ /regexp/) {    # regular expression error
      ...
   } elsif ($@) { die $@ }


But, note various things can go wrong, e.g.

  a problematic input like "foo/bar" for instance will go 
  silently undetected.

  even something really weird like "$^" which won't match 
  anything might be a remote concern. 

  
A string eval will catch the compile time error for the  
first worry noted. But, then security becomes a thorn. 
For example, a user could enter:

   @{[`\/usr\/bin\/rm \/somedir\/somefile`]}  


And suddenly, you're doing laundry. Ugh. 


hth,
--
Charles DeRykus


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

Date: 25 Sep 1998 16:24:30 -0400
From: Uri Guttman <uri@camel.fastserv.com>
Subject: Re: Where to put cgi-lib.pl
Message-Id: <sarbto4vt35.fsf@camel.fastserv.com>

>>>>> "EVZ" == Eric Von Zee <evonzee@tritechnet.com> writes:

  EVZ> In addition to previous message: Perl acts on data in a BEGIN
  EVZ> block at compile-time, not run-time.

  EVZ> For example: (taken straight from the manpages)

  EVZ>     use Module ();

  EVZ> is exactly equivalent to

  EVZ>     BEGIN { require Module }

and it calls import in the module too.

uri

-- 
Uri Guttman                  Fast Engines --  The Leader in Fast CGI Technology
uri@fastengines.com                                  http://www.fastengines.com


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

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

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