[8273] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1890 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Feb 13 14:07:57 1998

Date: Fri, 13 Feb 98 11:00:44 -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           Fri, 13 Feb 1998     Volume: 8 Number: 1890

Today's topics:
    Re: Constants in Perl? (Jonathan Feinberg)
    Re: convert Perl for UNIX to NT 4.0 (Jonathan Feinberg)
    Re: FAQless Forgays (Nathan V. Patwardhan)
    Re: How Do You Write an Empty While Statement? <cdkaiser@delete.these.four.words.concentric.net>
    Re: How Do You Write an Empty While Statement? <cdkaiser@delete.these.four.words.concentric.net>
    Re: How to tell if your CGI-related question belongs in (Joe McMahon)
    Re: HUMOR: Userspeak vs Hackerspeak (Earl Hood)
    Re: HUMOR: Userspeak vs Hackerspeak (Earl Hood)
    Re: Newbie Regexp question! (Jonathan Feinberg)
    Re: Newbie Regexp question! <dboorstein@shopcfn.com>
        Novice-written script (was Re: on reading FAQs and guru (Hunter Johnson)
    Re: opening files in subroutine (Jonathan Feinberg)
    Re: pattern Search problem (Jonathan Feinberg)
        perl code to find version number of a Windows DLL <rwijs@wirehub.nl>
    Re: portable gettimeofday or similar (NT AND UNIX) (Jonathan Feinberg)
    Re: Quickie: regexp for valid e-mail addresses <dennis@ironlight.com>
    Re: regexp: /^[800|888]/ vs. /^8[0{2}|8{2}]/ (Francois Trousset)
    Re: regexp: /^[800|888]/ vs. /^8[0{2}|8{2}]/ <dboorstein@shopcfn.com>
        Search string in log files advice (Jsgines)
    Re: sorting an array (Jonathan Feinberg)
    Re: sorting an array (Jonathan Feinberg)
    Re: time for comp.lang.perl.more-needed ? (Re: repetiti (Andrew M. Langmead)
    Re: Using flock? (Andrew M. Langmead)
    Re: Using list as value of hash (Jonathan Feinberg)
    Re: Using list as value of hash (Andrew M. Langmead)
    Re: Variable interpolation or reference? (Jonathan Feinberg)
    Re: Zero-width positive lookahead assertion Q (Ilya Zakharevich)
        Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)

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

Date: Fri, 13 Feb 1998 13:02:36 -0500
From: jdf@pobox.com (Jonathan Feinberg)
Subject: Re: Constants in Perl?
Message-Id: <MPG.f4e54c4f0ceaa5d9896bc@news.concentric.net>

jgoerzen@southwind.net said...
> Simple question (I hope).  How to I get a "constant" in Perl?  Eg, the 
> equivolent of the following C code:
> 
> const int x = 10;

Perlmod addresses this:
--------------------------------------------------------------------
Another use of symbol tables is for making "constant" scalars.

	*PI = \3.14159265358979;

Now you cannot alter $PI, which is probably a good thing all in all. 

This isnt the same as a constant subroutine (one prototyped to take no 
arguments and to return a constant expression), which is subject to
optimization at compile-time. This isn't. See perlsub for details on 
these.

-- 
Jonathan Feinberg    jdf@pobox.com    Sunny Brooklyn, NY


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

Date: Fri, 13 Feb 1998 13:09:21 -0500
From: jdf@pobox.com (Jonathan Feinberg)
Subject: Re: convert Perl for UNIX to NT 4.0
Message-Id: <MPG.f4e565c5c8f06269896bf@news.concentric.net>

podurgiel@goodnewsnet.net said...
> also, I was wondering how do I have a perl script display an image in an
> html page just by using an img tag?  IE..
> <img src="http://www.mysite.com/cgi-bin/my.pl">
> is there anything special i need to do?

You'll want to ask again in a newsgroup that deals with HTML and/or CGI.  
This group is for the discussion of perl language issues.

-- 
Jonathan Feinberg    jdf@pobox.com    Sunny Brooklyn, NY


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

Date: 13 Feb 1998 18:22:02 GMT
From: nvp@shore.net (Nathan V. Patwardhan)
Subject: Re: FAQless Forgays
Message-Id: <6c230a$68r@fridge.shore.net>

Chip Salzenberg (chip@mail.atlantic.net) wrote:

: Where is Linus Torvalds?  

Linus was recently debating BSD/GPL in a FreeBSD NG.  I do understand
your point, tho.

--
Nathan V. Patwardhan
please don't send spam to president@whitehouse.gov


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

Date: 13 Feb 1998 15:14:09 GMT
From: Cameron Kaiser <cdkaiser@delete.these.four.words.concentric.net>
Subject: Re: How Do You Write an Empty While Statement?
Message-Id: <6c1o01$5os@examiner.concentric.net>

Justin Vallon <vallon@pearl.fi.bear.com> writes:

>  sleep 1 while ! -e 'Hmm.Hmm';

Whoops, you suggested that too :-)

--
Cameron Kaiser
cdkaiser at concentric dot net (it hasn't helped the spam yet though)
*** visit the Spectre Server at www.sserv.com
*** C64 software lives! www.computerworkshops.home.ml.org


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

Date: 13 Feb 1998 15:13:25 GMT
From: Cameron Kaiser <cdkaiser@delete.these.four.words.concentric.net>
Subject: Re: How Do You Write an Empty While Statement?
Message-Id: <6c1nul$5mc@examiner.concentric.net>

Justin Vallon <vallon@pearl.fi.bear.com> writes:

>Probably:
>  while (`ls Hmm.Hmm | wc -l` < 1) {}

You could also say:

1 while (`ls ...` < 1);

<offtopic>
Why use backticks? Why not:

1 while (! -e 'Hmm.Hmm');

which saves a subprocess and untold CPU cycles.
Just a suggestion. :-)
</offtopic>

--
Cameron Kaiser
cdkaiser at concentric dot net (it hasn't helped the spam yet though)
*** visit the Spectre Server at www.sserv.com
*** C64 software lives! www.computerworkshops.home.ml.org


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

Date: Fri, 13 Feb 1998 13:07:10 -0500
From: joe.mcmahon@gsfc.nasa.gov (Joe McMahon)
Subject: Re: How to tell if your CGI-related question belongs in comp.lang.perl.misc
Message-Id: <joe.mcmahon-1302981307100001@prtims.stx.com>

Thanks for the edits, additions, and corrections, everyone. I'll make 
edits as appropriate (epecially blowing the CGI newsgroup name - d'oh!)
and post again next week.

 --- Joe M.

-- 
 --- Joe M.


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

Date: 13 Feb 1998 18:33:45 GMT
From: ehood@medusa.acs.uci.edu (Earl Hood)
Subject: Re: HUMOR: Userspeak vs Hackerspeak
Message-Id: <6c23m9$due@news.service.uci.edu>

In article <34E31B1F.1D99@see_signature.com>,
NerveGas  <NerveGas@see_signature.com> wrote:
>Tom Christiansen wrote:
>{snip}
>>     getting help                paying a consultant
>
>  What?  The mighty "Real Programmer" Tom Christiansen paying others to
>do his work?  Tell me it isn't so! : )

The list maps out translation of phrases and not actions (if the
phrase implies an action).  I.e.  When a user says "getting help",
what is really meant is "paying a consultant".

	--ewh
-- 
             Earl Hood              | University of California: Irvine
      ehood@medusa.acs.uci.edu      |      Electronic Loiterer
http://www.oac.uci.edu/indiv/ehood/ | Dabbler of SGML/WWW/Perl/MIME


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

Date: 13 Feb 1998 18:35:28 GMT
From: ehood@medusa.acs.uci.edu (Earl Hood)
Subject: Re: HUMOR: Userspeak vs Hackerspeak
Message-Id: <6c23pg$e5k@news.service.uci.edu>

In article <m3lnvgftw9.fsf@tower.bofh.org.uk>,
Piers Cawley  <pdcawley@bofh.org.uk> wrote:
>12 Feb 1998 01:18:14 GMT Tom Christiansen <tchrist@mox.perl.com> comp.lang.perl.misc
>>         C:\                          root# 
>                                       ^^^^^
>Oops, Tom just failed the sysadmin test. YM, '$', HTH, HAND.

Depends on shell and how the root account is configured.

	--ewh
-- 
             Earl Hood              | University of California: Irvine
      ehood@medusa.acs.uci.edu      |      Electronic Loiterer
http://www.oac.uci.edu/indiv/ehood/ | Dabbler of SGML/WWW/Perl/MIME


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

Date: Fri, 13 Feb 1998 13:07:40 -0500
From: jdf@pobox.com (Jonathan Feinberg)
Subject: Re: Newbie Regexp question!
Message-Id: <MPG.f4e55f4f372f6889896be@news.concentric.net>

kmartino1@aol.com said...
> I need to replace null chars (ASCII=0) in a string with spaces and can't quite
> figure out how to do it.

In general, you may specify any octal or hex character in double-quotish 
context (such as regexes) very easily.  I refer you to the section on 
"Quote and Quote-like Operators" in "perlop," followed by "perlre."

-- 
Jonathan Feinberg    jdf@pobox.com    Sunny Brooklyn, NY


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

Date: Fri, 13 Feb 1998 13:15:24 -0500
From: Dan Boorstein <dboorstein@shopcfn.com>
To: KMartino1 <kmartino1@aol.com>
Subject: Re: Newbie Regexp question!
Message-Id: <34E48DBC.18CD16E3@shopcfn.com>

KMartino1 wrote:
> 
> Hi,
> I need to replace null chars (ASCII=0) in a string with spaces and can't quite
> figure out how to do it. I believe I need to use a regexp, but I can't figure
> out the syntax to find a null and do the replace.

no regex needed. if all you want to do is swap one character for
another then 'tr' is a great tool. though often found among the
regex pages, 'tr' is really a different beast and considerably
faster when it comes to simple character substitutions.

the null character is represented by \0 in perl, so just swap \0 for
a space and your on your way.

$nulls = "\0monkey\0potato";
($spaces = $nulls) =~ tr/\0/ /;
print "$nulls\n";
print "$spaces\n";

cheers,

-- 
dan boorstein <dboorstein@shopcfn.com>


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

Date: 13 Feb 1998 18:06:03 GMT
From: jhunterj@lexis-nexis.com (Hunter Johnson)
Subject: Novice-written script (was Re: on reading FAQs and gurus answering questions)
Message-Id: <6c222b$110@mailgate.lexis-nexis.com>

In article <8clnvf7wlf.fsf@gadget.cscaper.com>,
Randal Schwartz  <merlyn@stonehenge.com> wrote:

> In the old days, we used to get chunks of source shared as well: "hey
> look at this nifty recursive directory tree walker[2] I
> wrote". *There's* community for you.

> Think "potluck".  I'm rather pleased with myself on the analogy there.
> I'm just looking for a bigger potluck, where *everyone* realizes that
> it's not a gravy train.  And people *bring* stuff when they can... not
> just take -- take -- take.

Here's a little perl script, one of my first.  I needed to cart big
files home from my UNIX system to my DOS system, so this would split
the big file into diskette-sized chunks (by default) with "bsplit
sample.exe", which I could then "copy /b
sample.eaa+sample.eab+samble.eac sample.exe" at home.

It was written under perl 4.0 and doubtless missed many optimization
opportunities.

#!/usr/local/bin/perl

# bsplit - split file into new files of a specified size (default 1.44M)

$size = 1457664;
$tail = "aa";

foreach $i (0..$#ARGV)
{
    if ($ARGV[$i] =~ /^-[bkKmM]/)
    {
        $switch = substr ($ARGV[$i], 1, 1);
    
        $size = substr($ARGV[$i], 2);
        ($size eq "") && ($size = $ARGV[++$i]);
        
      SWITCH:
        {
            ($switch eq "k") && ($size *= 1000, last SWITCH);
            ($switch eq "m") && ($size *= 1000000, last SWITCH);
            ($switch eq "K") && ($size *= 1024, last SWITCH);
            ($switch eq "M") && ($size *= 1048576, last SWITCH);
        }
    }
    elsif ($ARGV[$i] =~ /^-o/)
    {
        $base = substr($ARGV[$i], 2);
        ($base eq "") && ($base = $ARGV[++$i]);
    }
    elsif ($ARGV[$i] =~ /^-t/)
    {
        $tail = substr($ARGV[$i], 2);
        ($tail eq "") && ($tail = $ARGV[++$i]);
    }
    else
    {
        (defined($beenhere++)) && die "$0: specify only 1 file to be split\n";
        
        open (FROM, $ARGV[$i]) || die "$0: can't open $ARGV[$i]\n";
        if (! defined($base))
        {
            $base = $ARGV[$i];
            substr($base, -2) = '';
        }
    }
}                               
                                 
defined($base) || ($base = "x");
eof(FROM) && open (FROM, "-");

while (read(FROM, $buf, $size)) 
{                               
    $file = join("", ">",$base,$tail);
    open (TO, $file);
    print TO $buf || die "$0: can't write to ", $base, $tail, "\n";
    $tail++;
}  


******

Hunter
--
J. Hunter Johnson        |  "I know that I came face-to-face with God's    
jhunterj@lexis-nexis.com |    love at my worst, not my best, and that      
(937) 865-6800 x5385     |     amazing grace saved a wretch like me."      
Lexis-Nexis, Dayton, OH  | Philip Yancey, _What's So Amazing About Grace?_ 


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

Date: Fri, 13 Feb 1998 13:33:39 -0500
From: jdf@pobox.com (Jonathan Feinberg)
Subject: Re: opening files in subroutine
Message-Id: <MPG.f4e5c133e4136009896c0@news.concentric.net>

xkn14@dial.pipex.com said...

Jon - First, you'll want to familiarize yourself with the difference 
between lexical and dynamic scoping by reading "perlsub" with special 
atention to the section on "Creating provate variables via my()."  
Second, you don't actually call your subroutine anywhere in your sample 
code, so it's hard to evaluate your thinking.  I think you mean to call 
the routine twice, once for each file.

-- 
Jonathan Feinberg    jdf@pobox.com    Sunny Brooklyn, NY


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

Date: Fri, 13 Feb 1998 12:59:23 -0500
From: jdf@pobox.com (Jonathan Feinberg)
Subject: Re: pattern Search problem
Message-Id: <MPG.f4e540537c178239896ba@news.concentric.net>

bmaillet@cisco.com said...
>     After I enter this pattern  search and replace:  $_=~s/\.\&nbsp\;
> [^.]/\.\&nbsp\; [^A-Z]/g;
> The output prints This is a test. [A-Z]his is only a test.  [A-Z]o not
> attempt to adjust your television.
> 
> How can I get the case to change here?  My output is printing [A-Z]
> instead of the letter I would like change case on.

In order to guide you towards learning the use of Perl regular 
expressions (and, in this case, the substitution operator), I'll point 
out the following things:

You do not capture a specific part of your match, and therefore have no 
means of specifying what it is you wish to convert to uppercase.

The brackets denote a character class.  The expression [^.] matches a 
any single character except for a literal dot.

The brackets don't mean anything special in the substitution part of the 
s// operator.

You may convert a string to uppercase by using the "uc" builtin or by 
using the "\U" double-quotish operator.

You do not need to escape the semicolons in your regex.

You need to read "perlre" followed by "perlfaq6".

-- 
Jonathan Feinberg    jdf@pobox.com    Sunny Brooklyn, NY


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

Date: 13 Feb 1998 18:47:29 GMT
From: "Rob Wijs" <rwijs@wirehub.nl>
Subject: perl code to find version number of a Windows DLL
Message-Id: <01bd38af$d03972a0$bd5fa5c2@galaxy>


Hello,

Does anyone know of any Perl code which opens a Windows95 DLL in
binary mode and gets the Version number?


best regards,
Peter



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

Date: Fri, 13 Feb 1998 13:41:31 -0500
From: jdf@pobox.com (Jonathan Feinberg)
Subject: Re: portable gettimeofday or similar (NT AND UNIX)
Message-Id: <MPG.f4e5de3eed76fb39896c1@news.concentric.net>

Frederic.Garzon@sycomore.fr said...
I have to write a cross-OS benchmark perl script, with a resolution
> of some milli-secs. I saw Time::Hires on CPAN but it is not usable
> on NT :
> Did any code some kind of similar module, available for both NT and
> Unix ?

The standard Perl distribution includes Benchmark.pm, which works fine 
on my NT box:

#!/usr/bin/perl -w

use Benchmark;

sub outer_my
{
   my $i;
   for ($i=0; $i<50000; $i++){}
}

sub inner_my
{
   for (my $i=0; $i<50000; $i++){}
}
timethese(100, {
   'outer my' => \&outer_my,
   'inner my' => \&inner_my,
});

-- 
Jonathan Feinberg    jdf@pobox.com    Sunny Brooklyn, NY


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

Date: Fri, 13 Feb 1998 09:47:59 -0800
From: Dennis Whitney <dennis@ironlight.com>
Subject: Re: Quickie: regexp for valid e-mail addresses
Message-Id: <34E4874F.62B98C21@ironlight.com>

Hello Bart,
    I had this same problem a while back and the only real thing you can do
is a check is make sure that the address is in at leat the right format.
After that, it's all up to sendmail (or what ever you use).
    The check I currently use looks something like:
if (($email =~ /\w+@[\w\.]+(com|gov|net|edu|mil|us|org|int)$/)&&
 ($email !~ /(!#$%^&*\(\))+/)) {
        ... Your code here ...
}

the first expression checks for valid formating and the 7 TLDs (+ int),
while the second looks for chars that sendmail cannot (or will not) deal
with.

Hope this helps...

-later,
    dennis

Bart Lateur wrote:

> cberry@cinenet.net (Craig Berry) wrote:
>
> >Dan Sanderson (dsanders@u.washington.edu) wrote:
> >: What's a good regexp that will only match valid e-mail addresses
>
> >Short answer:
> >
> >  Don't bother.  There isn't a good one.  Give up.
> >
> >Longer answer:
> >
> >  Buy Freidl's _Mastering Regular Expressions_.  Read his brilliant
> >  discussion on this very topic.  Marvel at his 4,000+ character
> >  valid-email-matching regex which still doesn't handle every possible
> >  case.  Give up.
>
> My question: how do mailer programs process them?
>
> My thoughts: If a mailer program can process them correctly, it must be
> possible to write a Perl script that does the same.
>
> My afterthought: *IF*
>
>         Bart.






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

Date: 13 Feb 1998 17:54:21 GMT
From: trs@eerie.fr (Francois Trousset)
Subject: Re: regexp: /^[800|888]/ vs. /^8[0{2}|8{2}]/
Message-Id: <6c21cd$fcq$1@anais.eerie.fr>

In article <34e45fe8.8296270@cnews.newsguy.com>,
	fritz.knack@nospam.POPULUS.net (Fritz Knack) writes:
> For a report I've written, I'm trying to determine if a given phone
> number is an 800-class number, i.e., it begins with 800 or 888.
> 
> Anyway, below are two snippets of code. The first one is what works
> the way I want it to. The second pulls too much, e.g., area code 813
> (Houston? Dallas?) "counts" as an 800-class number.
> 
> Can anyone tell me what's going on? I would have though the two were
> classic TMTOWTDI. (If it matters: perl 5.0003_07, Win32 build 311.)
> 
> # Snippet 1:
>   	if ($phone =~ /^8[0{2}|8{2}]/) {
> #		do stuff
> 	}
> 
> #	Snippet 2:
> 	if ($phone =~ /^[800|888]/) {
> #		do stuff
> 	}

	Hi,
	Both are wrong, the first one will also accept 80x 82a ...
	Hey no, ... won't fit ;-)
	I think the good solution is /^800|888/

	In fact [...] means any caracter in the enclosed brackets and
	not grouping...

		Francois Trousset



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

Date: Fri, 13 Feb 1998 13:03:14 -0500
From: Dan Boorstein <dboorstein@shopcfn.com>
To: fritz.knack@POPULUS.net
Subject: Re: regexp: /^[800|888]/ vs. /^8[0{2}|8{2}]/
Message-Id: <34E48AE2.75DE2480@shopcfn.com>

Fritz Knack wrote:
> Anyway, below are two snippets of code. The first one is what works
> the way I want it to. The second pulls too much, e.g., area code 813
> (Houston? Dallas?) "counts" as an 800-class number.
> # Snippet 1:
>         if ($phone =~ /^8[0{2}|8{2}]/) {
> #               do stuff
>         }
> 
> #       Snippet 2:
>         if ($phone =~ /^[800|888]/) {
> #               do stuff
>         }

i don't think either of these is what you want really. and
i think your confusion is over the square brackets. these
designate character classes (or collections), so in #1
you match an 8 at the beginning of the line and then one
of any of the following characters:

  028}{|

$phone = '8{' will match.

in #2 there is a similar problem. you are saying i want any of
the characters, 08|, at the beginning of the string. i believe
changing the square brackets to parentheses in #2 should give
you what you want.

good luck,

-- 
dan boorstein <dboorstein@shopcfn.com>


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

Date: 13 Feb 1998 18:35:26 GMT
From: jsgines@aol.com (Jsgines)
Subject: Search string in log files advice
Message-Id: <19980213183501.NAA05502@ladder02.news.aol.com>

I am newbie to the PERL language (im a network systems guy who is facing the
reality that he will have to code to fix something, 'nuff said) but I am
clueful enough to stumble and make things work if I can get some advice. I am
currently using PERL 5.003 for Win32 (ActiveState distribution) and am
programming in an NT workstation environment.  

I would like to write a script that will accept input from the keyboard, and
use that input as a reg. expression to search through several log files in a
certain order.  I was using the $variable = <STDIN> as regexp to be used in the
search, but I amunsuccessful.  The log files I need searched are in this
format:

[DATE] [TIME]
980210 183647 (1866), User "jsgines" logged in
980210 183647 (1866), Server: P2 Enabled. MTU = 9976
980210 183647 (1866), Coversheet List Start
980210 183647 (1866), Coversheet List Done
980210 183647 (1866), User Defaults Get
980210 183851 (1866), Submit Fax Start
980210 183851 (1866), (T)Submit Title: HFC->6:40 test 021098
980210 183853 (1866), Submit via local queue "ispout"
980210 183853 (1866), Fax is up, up and away!!!
980210 183853 (1866), Daemon "ispout" has been tickled

The regular expression is located in the fifth column (ie, "jsgines"), and I
would like to capture the Date, Time, and the regular expression that is found
on that line of the log file.  I would then keep reading the logfile to search
for the "Submit Fax: string.  If that string is found, I would need to capture
the line following it.  I would then use the DATE, TIME, and User as a
parameters to search through another log file.

the logic I am following is this:
>Enter user to search for:  jsgines (from keyboard)
   Open $LogFileName || die "Can't open: $LogFileName";

   If this user is found in the 5th column of $LogFileName,
   print the DATE, TIME and user (ie.,  jsgines)
   keep searching the log file for a "Submit Title" string.
   if the "Submit Title" string is found
   print the DATE, TIME, and the value following the "Submit Title" string
   keep reading the log file to find "Submit via local queue '<queuename>'"
Close $LogFileName
  else
   print some message

Open <queuename>
start search process all over again but starting at the DATE TIME and looking
for string user
print that line
if search fails,
  search for any error messages, but starting at the DATE TIME
  print that line
Close <queuename>

I apologize that above pseudocode is terrible and sloppy I know, sorry.  But if
there are any PERL gurus that have faced a similiar task in solving or can at
least point me in the right direction (I have the "Learning Perl on Win32
systems book" ) at what pages or chapters to look over, this newbie would be
greatful.  

Jonathan Gines
Sr. Engineer
UUNET Technologies






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

Date: Fri, 13 Feb 1998 12:46:40 -0500
From: jdf@pobox.com (Jonathan Feinberg)
Subject: Re: sorting an array
Message-Id: <MPG.f4e510d14bd81579896b7@news.concentric.net>

> I have an array of keys with this patern: xxxxYYYY where xxxx is a fixed
> char and YYYY are numbers. I want to sort the array by numeric valor of
> YYYY.
> Something like:
> sub bynumber { "ubppp".$a <=> "ubppp".$b; }
> @sort_keys = sort bynumber @keys;

In your sub, you are appending a string to the comparisands, making them 
non-numeric.  You want to extract the numeric portions of $a and $b.

#!/usr/bin/perl -w

sub my_cmp { 
   $a_num = substr($a,5,length($a)-5);
   $b_num = substr($b,5,length($b)-5);
   $a_num <=> $b_num
}

chomp (@sorted = sort my_cmp <DATA>);
print "@sorted\n";

__DATA__
abcde45
abcde112
abcde3
abcde87
abcde42

-- 
Jonathan Feinberg    jdf@pobox.com    Sunny Brooklyn, NY


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

Date: Fri, 13 Feb 1998 12:48:22 -0500
From: jdf@pobox.com (Jonathan Feinberg)
Subject: Re: sorting an array
Message-Id: <MPG.f4e5170e2f2c5b29896b8@news.concentric.net>

Oops.

The substr functions can be spelled

substr($a,5)

-- 
Jonathan Feinberg    jdf@pobox.com    Sunny Brooklyn, NY


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

Date: Fri, 13 Feb 1998 17:49:49 GMT
From: aml@world.std.com (Andrew M. Langmead)
Subject: Re: time for comp.lang.perl.more-needed ? (Re: repetitive FAQ posts)
Message-Id: <EoBw72.KLt@world.std.com>

Remove xx to reply <xxTony.Curtis@vcpc.univie.ac.at> writes:

>Maybe the clp hierarchy should be extended?

>clp.www or clp.cgi

>might trap most of the posts that turn out to be WWW server
>related.

Unfortunately, it makes no sense. There are few questions in which CGI
and perl overlap.

If you have trouble getting or decoding input to your CGI script, it
is a CGI question. There is little perl related about it.

If your script isn't emitting valid output from your CGI script, it is
a CGI question. Again, unless someone has problems with the print()
operator, it isn't perl related. If they are having problems using the
print operator, their question is probably answered in some book or
other documentation they have.

If their CGI script written in perl gets its input correctly, does the
wrong thing with it, and then correctly outputs the wrong results
according to the CGI spec, then it is a perl question.

If the novice does not understand the difference between CGI and perl,
creating an environment that implies there is little if any difference
is not going to teach them. It may be a hard step for them to learn,
but leading them in the wrong direction is just going to cause them to
have to unlearn it after it is more deeply entrenched.

-- 
Andrew Langmead


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

Date: Fri, 13 Feb 1998 17:55:20 GMT
From: aml@world.std.com (Andrew M. Langmead)
Subject: Re: Using flock?
Message-Id: <EoBwGB.1K3@world.std.com>

Jochen Wiedmann <joe@ispsoft.de> writes:


>Question arises: What does "flock FILE,8" do? Or, in other words:
>Is this even usable?

It is useful in certain situations, its just that most programs are in
that situation.

Imagine a long running program, like a unix daemon. It may want
occationally access a file. It may need to lock it for some reason,
but it doesn't want to lock it for the entire programs duration (which
may be months or years. I wonder what the record for the longest up
time on a unix system is? I'm sure it isn't Software Tool and Die's
current machine.)

So in this case, the program make lock the file, or portions of the
file, read or write to it as needed, flush its buffers if needed, then
release the lock. 
-- 
Andrew Langmead


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

Date: Fri, 13 Feb 1998 12:51:54 -0500
From: jdf@pobox.com (Jonathan Feinberg)
Subject: Re: Using list as value of hash
Message-Id: <MPG.f4e524514183dfe9896b9@news.concentric.net>

epa@datcon.co.uk said...
> I couldn't find anything on this in the FAQ or "Learning Perl", please
> help.

Please consult the "perllol" doc which comes with Perl.  If you still 
have questions, I'll be glad to help you out.

Also, the question is indeed addressed in perlfaq4:

"How can I make the Perl equivalent of a C structure/C++ class/hash or 
array of hashes or arrays?"

-- 
Jonathan Feinberg    jdf@pobox.com    Sunny Brooklyn, NY


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

Date: Fri, 13 Feb 1998 18:02:49 GMT
From: aml@world.std.com (Andrew M. Langmead)
Subject: Re: Using list as value of hash
Message-Id: <EoBwss.79u@world.std.com>

epa@datcon.co.uk (Ed Avis) writes:

>Here the ID "2" is associated with the names "apple", "pear" and
"banana".  The ID "3" is associated with "goat", and "4" with "bear".


One suggestion would be to use a multidimensional data structure, like
a hash of arrays.

open IF, 'filename' or die "Can't open filename: $!\n";

while(<IF>) {
  ($word, $id) = split;
  push @{$name{$id}}, $word;
}

Then you can look get something by $id and find all the associated
names.

foreach $id (keys %name) {
  print "$id\n";
  foreach $word (@{$name{$id}}
     print "   $word\n";
  }
}


There is more information about multidimensional data structures in
the perldsc man page.

<URL:http://www.perl.com/CPAN/doc/manual/html/pod/perldsc.html>


-- 
Andrew Langmead


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

Date: Fri, 13 Feb 1998 13:04:27 -0500
From: jdf@pobox.com (Jonathan Feinberg)
Subject: Re: Variable interpolation or reference?
Message-Id: <MPG.f4e553226eae9ef9896bd@news.concentric.net>

mark666769@aol.com said...
> In the following program:
> #!/usr/local/bin/perl
> 
> $var = "test";
> ${$var} = "data";		# creates a new variable "$test"
> 
> print "\$var is: $var\n";
> print "\$test is: $test\n";
> 
> 
> I see that ${$var} creates a new variable $test,
> but I'm not quite sure why.

Then "perlref" is going to quench your thirst.

$ perldoc perlref

-- 
Jonathan Feinberg    jdf@pobox.com    Sunny Brooklyn, NY


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

Date: 13 Feb 1998 18:30:17 GMT
From: ilya@math.ohio-state.edu (Ilya Zakharevich)
Subject: Re: Zero-width positive lookahead assertion Q
Message-Id: <6c23fp$6e5$1@mathserv.mps.ohio-state.edu>

[A complimentary Cc of this posting was sent to Sitaram Chamarty
<sitaram@diac.com>],
who wrote in article <slrn6e7trm.er5.sitaram@ltusitaram.diac.com>:
> On Thu, 12 Feb 1998 16:26:36 GMT, David Griffin <griffin@i33.com> wrote:
> >I'm trying to add commas to long numbers, eg, 1234123545 becomes
> >1,234,123,545.  It seems to me that this should do the trick:
> >
> >$num =~ s/(\d{1,3})(\d{3})(?=\d{3})*$/\1,\2/g;
> 
> I'm not really sure the "*" (match >= 0 times) quantifier is
> applicable to lookahead assertions...that's prolly your trouble.

Of course it is applicable!  I *think* most Perl's will not even
segfault in such a situation ;-).  Newer Perls will even emit a
warning that you wrote not what you wanted.

  >perl -we "/(?=\d)*/"
  (?=\d)* matches null string many times at -e line 1.
  Strange *+?{} on zero-length expression at -e line 1.
  Use of uninitialized value at -e line 1.

(Hmm, the first warning is not wanted any more, should be considered a
bug).

The above problem is the first one on which I understood a use of
lookahead.  My solution was something like

	    s/(\G\d{1,3})(?=(\d{3})*$)/\1,/g;

Ilya


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

Date: 8 Mar 97 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 8 Mar 97)
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.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 1890
**************************************

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