[18095] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 255 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Feb 9 09:05:40 2001

Date: Fri, 9 Feb 2001 06:05:11 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <981727511-v10-i255@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Fri, 9 Feb 2001     Volume: 10 Number: 255

Today's topics:
    Re: *perl newbie* asking for hash-of-hashes basics <none@nowhere.noland.etc>
    Re: Can perl define vars dynamically? <joe+usenet@sunstarsys.com>
    Re: Editing a file if line doesn't exist already <Jerome.Abela@free.fr>
    Re: flock unavailable on many platforms <johnlin@chttl.com.tw>
        fperl:ile -newer file2 <daniel.heiserer@bmw.de>
    Re: killing a cgi nicely (Michel Dalle)
    Re: killing a cgi nicely <emelin@my-deja.com>
    Re: non repeating random numbers <jbroz@transarc.ibm.com>
    Re: Problems with Perl on Apache? <dk_sz@hotmail.com>
    Re: Problems with Perl on Apache? <dk_sz@hotmail.com>
    Re: put undef as place holder in an array <johnlin@chttl.com.tw>
    Re: sort of about sort (Mark Jason Dominus)
    Re: sort of about sort (Eric Bohlman)
    Re: sorting an array (Bernard El-Hagin)
    Re: sorting an array (J.C.Posey)
    Re: sorting an array (Abigail)
    Re: sorting an array (J.C.Posey)
    Re: sorting an array (Abigail)
    Re: sorting an array (J.C.Posey)
    Re: sorting an array (Abigail)
    Re: Specifying the length of regular expression <iboreham@my-deja.com>
    Re: split a path and keep the slashes? <stephenk@cc.gatech.edu>
    Re: split a path and keep the slashes? (Abigail)
    Re: Using regex to match numbers (not digits)? <iboreham@my-deja.com>
    Re: Using regex to match numbers (not digits)? nukes666@my-deja.com
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

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

Date: Fri, 09 Feb 2001 12:11:20 +0100
From: Perique des Palottes <none@nowhere.noland.etc>
Subject: Re: *perl newbie* asking for hash-of-hashes basics
Message-Id: <3A83D058.CD561DB2@nowhere.noland.etc>

Perique des Palottes wrote:
> ...
>    foreach $key1 (keys $assoc) {
>      foreach $key2 (keys $assoc{$key1}){
>        print $key1." ".$key2." ".$assoc{$key1}{$key2}."\n";
>      }
>    }

Mmm, experimenting, it seems now to make sense to the interpreter,
and its result makes sense to me...

foreach $key1 (keys %assoc) {
  foreach $key2 (keys %{$assoc{$key1}}){
    print $key1." ".$key2." ".$assoc{$key1}{$key2}."\n";
  }
}

--
  All true believers shall break their eggs at the convenient end.


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

Date: 09 Feb 2001 09:02:08 -0500
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: Can perl define vars dynamically?
Message-Id: <m3g0hoosyn.fsf@mumonkan.sunstarsys.com>

"John W. Krahn" <krahnj@acm.org> writes:

> Joe Schaefer wrote:
> > 
> >   my $FS = '|';         # (used in a regexp) why is this here?
> >   my @sepf;             # desired array (what's it good for?)
> > 
> >   while (<>) {
> >     chomp;              # get rid of potential teminating newline
> >     my (undef, $field) = split /$FS/o, $_, 3; # field 2 into my $field
> 
> $ perl -e '$FS = q{|}; $x = "ab|cd|ef|gh"; @y = split /$FS/o, $x; print
> ">$_< " for @y;'
> >a< >b< >|< >c< >d< >|< >e< >f< >|< >g< >h<

Oops!  Good catch. I should have looked more carefully at my own
comments :)

Should be something like

    my $FS = qr/[|]/;     # why is this here?

or just

    my $FS = '\|';        # why is this here?

but *not*

    my $FS = "\|";        # bzzt! needs /\Q$FS/ in the split regexp


Sorry for the error, and thanks again for the spot.

-- 
Joe Schaefer    "Few things are harder to put up with than the annoyance of a
                                        good example."
                                                --Mark Twain


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

Date: Fri, 09 Feb 2001 11:12:46 GMT
From: Jerome Abela <Jerome.Abela@free.fr>
Subject: Re: Editing a file if line doesn't exist already
Message-Id: <3A83CFA3.52FEFFEE@free.fr>

jstanley@mmm.com a écrit :
> I am having trouble thinking of a way to edit a file only if a particular
> line doesn't exist.

What is the size of this file ? It it's short, you can read it as a
single block:

undef $/;
open(LAST, ...);
$text = <LAST>;
if($text !~ /text to match/i) {
   add something
}

If it's bigger, use a sub, which makes the flow control easier:

sub fileMatches {
  my ($file, $regexp) = @_;
  open(FILE, "<$file");
  while(<FILE>) { if(/$regexp/i) { return 1; } }
  return 0;
}


Jerome.


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

Date: Fri, 9 Feb 2001 08:38:27 +0800
From: "John Lin" <johnlin@chttl.com.tw>
Subject: Re: flock unavailable on many platforms
Message-Id: <95ve97$s90@netnews.hinet.net>

"Darren Dunham" wrote
> John Lin wrote:
>
> >     open F,'file.lock' or die "please create 'file.lock' first\n";
> >     flock F,LOCK_EX or die "can't lock\n";
>
> You're asking for an exclusive lock on a file that you haven't opened
> with write access.
>
> Try to only ask for exclusive locks on files you have opened for
> writing.
>

OK, now I open for read-write but don't really write anything.

open F,'+< file.lock' or die "please create 'file.lock' first\n";

Great!!!  It works!!!  Thank you very much!!!

John Lin





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

Date: Fri, 09 Feb 2001 13:36:57 +0100
From: Daniel Heiserer <daniel.heiserer@bmw.de>
Subject: fperl:ile -newer file2
Message-Id: <3A83E469.477890AF@bmw.de>

This is a multi-part message in MIME format.
--------------33B168F0BD475C6062170705
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hi,
how can I check in perl if one file is newer then
another one.
I only have seen unary operators for files.

thanks daniel
--------------33B168F0BD475C6062170705
Content-Type: text/x-vcard; charset=us-ascii;
 name="daniel.heiserer.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Daniel Heiserer
Content-Disposition: attachment;
 filename="daniel.heiserer.vcf"

begin:vcard 
n:Daniel;Heiserer,
tel;fax:41696
tel;home:1409782
tel;work:21187
x-mozilla-html:FALSE
adr:;;;;;;
version:2.1
email;internet:daniel.heiserer@bmw.de
x-mozilla-cpt:;-3136
fn:Heiserer, Daniel
end:vcard

--------------33B168F0BD475C6062170705--



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

Date: Fri, 09 Feb 2001 12:47:36 GMT
From: michel.dalle@usa.net (Michel Dalle)
Subject: Re: killing a cgi nicely
Message-Id: <960p02$t2k$1@news.mch.sbs.de>

In article <960fcd$i4v$1@nnrp1.deja.com>, emelin <emelin@my-deja.com> wrote:
[snip]
>Okay... how do I send my error msg to the sub?? (sorry, i AM a newbie)
>
>There is no way to do this with built-in die??

Add the following line near the top of your script :
use CGI::Carp qw(fatalsToBrowser);

You can then use the normal ... die "This went wrong : $!\n";
code in your script, and it will show up in the browser.

HTH,


Michel.


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

Date: Fri, 09 Feb 2001 13:50:02 GMT
From: emelin <emelin@my-deja.com>
Subject: Re: killing a cgi nicely
Message-Id: <960si8$s6u$1@nnrp1.deja.com>


>
> Add the following line near the top of your script :
> use CGI::Carp qw(fatalsToBrowser);
>
> You can then use the normal ... die "This went wrong : $!\n";
> code in your script, and it will show up in the browser.

but this will print out lots of stuff, and that's what i was trying to
avoid...


Sent via Deja.com
http://www.deja.com/


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

Date: Fri, 09 Feb 2001 12:51:42 +0000
From: jb <jbroz@transarc.ibm.com>
Subject: Re: non repeating random numbers
Message-Id: <3A83E7DE.E488E1@transarc.ibm.com>

"Godzilla!" wrote:
> 
[...]

> Your Perl Cookbook is amongst the worst of books on Perl.
> Trust nothing you read in this book; it is riddled with
> errors throughout its content and index.

It would have been more helpful if you could point out some of the errors.
I have the book and find it to be a valuable resource. On the contrary,
there are probably thousands of crap books on perl and probably less than
20 that would rate as 'good' or better. The cookbook is one of the latter
but understand the audience for which it was written. It's a set of
recipes. I think the reader is expected to understand the concepts behind
the solutions. In my opinion, it's not meant for a novice perl programmer.

> Typical for Perl documentation, you will find examples but
> will not find any discussion of how this method works. I am
> quite convinced those whose write Perl documentation don't
> have a clue how 'stuff' works, they only know it does.

Incorrect. The documentation is meant to describe the language and is not
designed to show the internals of what is being discussed. I read many of
the posts in this group and it seems to me that those who slag off the perl
documentation just haven't invested the time to read and understand it (or
fail to do so for some other reason). Typically, people want to be told how
to solve their particular problem, they do not want to try to figure it out
for themselves. When they (not you, in this case) don't find the solution
in the written documentation they complain about the quality of it.

> I am equally convinced those who write Perl documentation,
> have never taken a class in English. Personally, I suspect
> they learned how to write by reading stall walls in a
> gas station men's restroom.

Absolute crap. Stupid assertions such as this are meaningless. In addition,
many people who produce perl code and documentation are not native speakers
of English.


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

Date: Fri, 9 Feb 2001 12:18:57 +0100
From: "Thomas Schulz" <dk_sz@hotmail.com>
Subject: Re: Problems with Perl on Apache?
Message-Id: <960juc$aq7$1@news.inet.tele.dk>

> This question probably belongs in
comp.infosystems.www.servers.ms-windows... :-o
Also posted there - follow ups should probably go there (as we're in a
fairly large thread there) as you suggest.


> 1.  Add "Includes" to your "Options".
> 2.  Add (or uncomment) "AddType text/html .shtml".
> 3.  Add (or uncomment) "AddHandler server-parsed .shtml".

Already done.

> <html>
> <!--#include virtual="/cgi-bin/helloworld.pl"-->
> </html>

I have been told (and sorta read) that it should be: <!--#exec
cgi="/cgi-bin/helloworld.pl"-->
is correct. This should only "include" the file: <!--#include
virtual="/cgi-bin/helloworld.pl"--> AFAIK.
I have tryed what you said though.


> Good luck!
yeah.. well.. it didn't work..  But thx anyway

Regards
Thomas Schulz




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

Date: Fri, 9 Feb 2001 12:23:36 +0100
From: "Thomas Schulz" <dk_sz@hotmail.com>
Subject: Re: Problems with Perl on Apache?
Message-Id: <960jud$aq7$2@news.inet.tele.dk>

> When you start asking about web server configs, it's time to start
> asking in a web server group.

Yeah - sorry, I've done that too now.

> I'm not an expert on embedding Perl into web pages with SSI, but I'd
> say you may not need to print headers if you're in the middle of the
> page.

I have tryed not to as well :-(.

> Try bringing up a regular CGI in Perl, like this:
>
> $ cat helloweb.pl
> print <<EOF;
> Content-type: text/html
>
> <html>
> <body>
> Hello, world!
> </body>
> </html>
> EOF

Will try


Thanks
Thomas Schulz





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

Date: Fri, 9 Feb 2001 08:24:45 +0800
From: "John Lin" <johnlin@chttl.com.tw>
Subject: Re: put undef as place holder in an array
Message-Id: <95vdfe$ree@netnews.hinet.net>

"Bart Lateur" wrote
> Set the upper bound for the dimension of the subarrays.

Great!!!  I think this question can be closed.

my @a = ('a b','d e','h');
my @len = (3,4,5);
my @result = map { my @b = split(' ',$a[$_]); $#b = $len[$_]-1; @b } 0..$#a;
print map { defined()? $_: '?' } @result;

Thank you very much.

John Lin





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

Date: Fri, 09 Feb 2001 11:40:57 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: sort of about sort
Message-Id: <3a83d748.5c8$18e@news.op.net>
Keywords: confrontation, cottonmouth, ecosystem, utility


In article <3A83B20A.34805389@coventry.ac.uk>,
JMT  <ccx138@coventry.ac.uk> wrote:
>So what does TMTOWTDI stand for again. ;)


You're confused.  Just because there's more than one way to do it
doesn't mean that the Perl core must necessarily support every
possible way of doing everything.  
        
        Although the Perl Slogan is There's More Than One Way to Do
        It, I hesitate to make 10 ways to do something. :-)

             --Larry Wall in <9695@jpl-devvax.JPL.NASA.GOV>


>I was just thinking that it could (have) be(en) implemented so if you
>want to sort by another way you could define your own or by default
>it does alpha or with a switch it does numeric.

Right.  And I was just pointing out that if you want that, you can
already get it (so there *is* more than one way to do it, including
yours) and then you can pay the cost for it if you like, and nobody
else has to.

>I was only asking because perl started on UNIX and I'm sure Larry
>often used sort -nr.

Yes, and being familiar with unix he must have realized that the
qsort(3) functionality was much more useful and appropriate than the
sort(1) functionality.
-- 
@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: 9 Feb 2001 11:56:38 GMT
From: ebohlman@omsdev.com (Eric Bohlman)
Subject: Re: sort of about sort
Message-Id: <960ltm$krd$1@bob.news.rcn.net>

JMT <ccx138@coventry.ac.uk> wrote:
> Mark Jason Dominus wrote:

>> It could, but it won't, because it multiplies entities unnecessarily.
>> There is already a perfectly good way to do a numeric reversed sort
>> (as you know, having already read the manual entry for 'sort') and
>> your suggestion only adds another way to do exactly the same thing.
>>

> So what does TMTOWTDI stand for again. ;)
> I was just thinking that it could (have) be(en) implemented so if you want to
> sort by another way you could define your own or by default it does alpha or with
> a switch it does numeric.

> I was only asking because perl started on UNIX and I'm sure Larry often used sort
> -nr.  I have no problem with doing it the way it is now I was just curious as to
> why it was set up the way it was.

In this case, there really is, IMHO, justification for OMWTDI: default 
sorts are significantly faster (by a constant factor of course; the time 
complexity doesn't change) than sorts using a comparison block (which is 
the whole basis for the Guttman/Rosler transformation).  Thus there's a 
time penalty for doing a numeric sort compared to a lexical sort, and 
there's a time penalty for a descending sort compared to an ascending 
sort.



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

Date: Fri, 9 Feb 2001 11:05:14 +0000 (UTC)
From: bernard.el-hagin@lido-tech.net (Bernard El-Hagin)
Subject: Re: sorting an array
Message-Id: <slrn987jna.2uq.bernard.el-hagin@gdndev25.lido-tech>

On 09 Feb 2001 11:01:54 +0000, J.C.Posey <jcp@myrtle.ukc.ac.uk> wrote:
>I'm a novice Perl programmer, and may be missing something simple.
>
>I converted an awk script using a2p, and want to add a little functionality.
>
>I would like to sort my output, then print.  Currently,
>
>---code fragment---
>
>foreach $item (keys %daily_total) {
>    print $item, $daily_total{$item};
>}
>
>---end----

Change the foreach to:

foreach $item (sort keys %daily_total){
	print $item, $daily_total{$item};
}

Cheers,
Bernard
--
#requires 5.6.0
perl -le'* = =[[`JAPH`]=>[q[Just another Perl hacker,]]];print @ { @ = [$ ?] }'


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

Date: 09 Feb 2001 11:20:06 +0000
From: jcp@myrtle.ukc.ac.uk (J.C.Posey)
Subject: Re: sorting an array
Message-Id: <jkopugsje6x.fsf@myrtle.ukc.ac.uk>

bernard.el-hagin@lido-tech.net (Bernard El-Hagin) writes:


> 
> Change the foreach to:
> 
> foreach $item (sort keys %daily_total){
> 	print $item, $daily_total{$item};
> }
> 
> Cheers,
> Bernard
> --

Ahhh, of course simple, one word.  Many thanks.

-- 
Jake C. Posey                    | +44 (0)1227 827692
Junior Technical Support Officer | http://www.mau.ac.uk/
MAU/TAU                          | http://www.tau.ac.uk/
University of Kent at Canterbury | http://www.jisc.ac.uk/


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

Date: 9 Feb 2001 11:54:33 GMT
From: abigail@foad.org (Abigail)
Subject: Re: sorting an array
Message-Id: <slrn987mjp.73i.abigail@tsathoggua.rlyeh.net>

J.C.Posey (jcp@myrtle.ukc.ac.uk) wrote on MMDCCXIX September MCMXCIII in
<URL:news:jkou264jf19.fsf@myrtle.ukc.ac.uk>:
:: I'm a novice Perl programmer, and may be missing something simple.
:: 
:: I converted an awk script using a2p, and want to add a little functionality.
:: 
:: I would like to sort my output, then print.  Currently,
:: 
:: ---code fragment---
:: 
:: foreach $item (keys %daily_total) {
::     print $item, $daily_total{$item};
:: }
:: 
:: ---end----
:: 
:: Which simply prints to standard out, and then I sort in my shell.  I would
:: like to sort prior to output.
:: 
:: I know that if it was an array I could simply sort @daily_total, but this
:: is in error here. Or is it?


Really? What did you try and what was the error you got?



Abigail
-- 
perl -le 's[$,][join$,,(split$,,($!=85))[(q[0006143730380126152532042307].
          q[41342211132019313505])=~m[..]g]]e and y[yIbp][HJkP] and print'


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

Date: 09 Feb 2001 12:24:43 +0000
From: jcp@myrtle.ukc.ac.uk (J.C.Posey)
Subject: Re: sorting an array
Message-Id: <jkon1bwjb78.fsf@myrtle.ukc.ac.uk>

abigail@foad.org (Abigail) writes:


> :: 
> :: ---code fragment---
> :: 
> :: foreach $item (keys %daily_total) {
> ::     print $item, $daily_total{$item};
> :: }

> :: 
> :: I know that if it was an array I could simply sort @daily_total, but this
> :: is in error here. Or is it?
> 
> 
> Really? What did you try and what was the error you got?

> 
> Abigail

I tried print sort @daily_totals;

Then it would give me "syntax error at line XX, near print".


-- 
Jake C. Posey                    | +44 (0)1227 827692
Junior Technical Support Officer | http://www.mau.ac.uk/
MAU/TAU                          | http://www.tau.ac.uk/
University of Kent at Canterbury | http://www.jisc.ac.uk/


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

Date: 9 Feb 2001 12:47:38 GMT
From: abigail@foad.org (Abigail)
Subject: Re: sorting an array
Message-Id: <slrn987pna.73i.abigail@tsathoggua.rlyeh.net>

J.C.Posey (jcp@myrtle.ukc.ac.uk) wrote on MMDCCXIX September MCMXCIII in
<URL:news:jkon1bwjb78.fsf@myrtle.ukc.ac.uk>:
\\ abigail@foad.org (Abigail) writes:
\\ 
\\ 
\\ > :: 
\\ > :: ---code fragment---
\\ > :: 
\\ > :: foreach $item (keys %daily_total) {
\\ > ::     print $item, $daily_total{$item};
\\ > :: }
\\ 
\\ > :: 
\\ > :: I know that if it was an array I could simply sort @daily_total, but this
\\ > :: is in error here. Or is it?
\\ > 
\\ > 
\\ > Really? What did you try and what was the error you got?
\\ 
\\ > 
\\ > Abigail
\\ 
\\ I tried print sort @daily_totals;
\\ 
\\ Then it would give me "syntax error at line XX, near print".


That is odd:

    $ perl -wce 'print sort @daily_totals;'
    Name "main::daily_totals" used only once: possible typo at -e line 1.
    -e syntax OK
    $


No syntax error here!



Abigail
-- 
@_=map{[$!++,$_^$/]}split$²,"\@\x7Fy~*kde~box*Zoxf*Bkiaox";$\="\r";
$|=++$*;do{($#,$=)=(rand@_,rand@_);@_[$#,$=]=@_[$=,$#]}for($*..@_);
for$:($|..@_-$|){for($|..@_-$:){@_[$_-$|,$_]=@_[$_=>$_-$*]if$_[$_][
$¼]<$_[$_-$*][$®];print+map{$_->[$|]}@_;select$·,$°,$½,0.1}}print$/


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

Date: 09 Feb 2001 12:51:18 +0000
From: jcp@myrtle.ukc.ac.uk (J.C.Posey)
Subject: Re: sorting an array
Message-Id: <jkoitmkj9yx.fsf@myrtle.ukc.ac.uk>

abigail@foad.org (Abigail) writes:

>  
> No syntax error here!
> 

Could it be because of an older version of Perl? Perl 5.005_03 built 
for sun4-solaris

-- 
Jake C. Posey                    | +44 (0)1227 827692
Junior Technical Support Officer | http://www.mau.ac.uk/
MAU/TAU                          | http://www.tau.ac.uk/
University of Kent at Canterbury | http://www.jisc.ac.uk/


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

Date: 9 Feb 2001 13:29:35 GMT
From: abigail@foad.org (Abigail)
Subject: Re: sorting an array
Message-Id: <slrn987s5v.73i.abigail@tsathoggua.rlyeh.net>

J.C.Posey (jcp@myrtle.ukc.ac.uk) wrote on MMDCCXIX September MCMXCIII in
<URL:news:jkoitmkj9yx.fsf@myrtle.ukc.ac.uk>:
)) abigail@foad.org (Abigail) writes:
)) 
)) >  
)) > No syntax error here!
)) > 
)) 
)) Could it be because of an older version of Perl? Perl 5.005_03 built 
)) for sun4-solaris


No, it could not. 



Abigail
-- 
package Just_another_Perl_Hacker; sub print {($_=$_[0])=~ s/_/ /g;
                                      print } sub __PACKAGE__ { &
                                      print (     __PACKAGE__)} &
                                                  __PACKAGE__
                                            (                )


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

Date: Fri, 09 Feb 2001 11:16:27 GMT
From: Ian Boreham <iboreham@my-deja.com>
Subject: Re: Specifying the length of regular expression
Message-Id: <960ji8$l4t$1@nnrp1.deja.com>

In article <960h6i$jeo$1@nnrp1.deja.com>,
  Ian Boreham <iboreham@my-deja.com> wrote:

> If you really need to do it with a single regex, try:
>
>   ^(?=[ABC]{10,})A+B*C+$
>
> or some variation to taste.

Here I go replying to my own post:

You don't need the comma after the "10".

Regards,


Ian


Sent via Deja.com
http://www.deja.com/


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

Date: Fri, 09 Feb 2001 06:19:40 -0500
From: Stephen Kloder <stephenk@cc.gatech.edu>
Subject: Re: split a path and keep the slashes?
Message-Id: <3A83D24C.88714558@cc.gatech.edu>

tebrusca@my-deja.com wrote:

> Given a path: "//A/B/" or "/A/B" or "/A//B//" etc
> Return a list: "/A", "/B"
>
> That is, ignore multiple slashes and give items with leading slash
>
> Here's what I came up with:
>
> my $string = "/A/B///";
> my @list =  map {"/$_"}( map {/^(.+)/}(split( /\//, $string )));
>
> Am I breaking the "Dont modify $_" rule with my first ( actually
> second) map?

Neither of your map statements actually modify the strings in the
array.   I find it interesting that you use a map statement to do the
job of a grep statement:
my @list =  map {"/$_"} (grep length, split( /\//, $string ));
does the same thing, and is more readable.  In fact, why bother with the
grep at all?  Splitting on m#/+# (i.e. multiple slashes) will remove its
necessity.

Also, why do you yank all the slashes out, only to reinsert them?


> Any comments or better ideas ... golfers?
>
> Note: The number of items may vary so /()()/ wont work for me.
>

my @list = ($string =~ m#/?[^/]+#g);

The '?' is for the case of no leading slashes.

Hope this helps.
perldoc perlre
perldoc perlop

--
Stephen Kloder               |   "I say what it occurs to me to say.
stephenk@cc.gatech.edu       |      More I cannot say."
Phone 404-874-6584           |   -- The Man in the Shack
ICQ #65153895                |            be :- think.




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

Date: 9 Feb 2001 12:04:28 GMT
From: abigail@foad.org (Abigail)
Subject: Re: split a path and keep the slashes?
Message-Id: <slrn987n6c.73i.abigail@tsathoggua.rlyeh.net>

Stephen Kloder (stephenk@cc.gatech.edu) wrote on MMDCCXIX September
MCMXCIII in <URL:news:3A83D24C.88714558@cc.gatech.edu>:
,, tebrusca@my-deja.com wrote:
,, 
,, > Given a path: "//A/B/" or "/A/B" or "/A//B//" etc
,, > Return a list: "/A", "/B"
,, >
,, > That is, ignore multiple slashes and give items with leading slash
,, >
,, > Here's what I came up with:
,, >
,, > my $string = "/A/B///";
,, > my @list =  map {"/$_"}( map {/^(.+)/}(split( /\//, $string )));
,, >
,, > Am I breaking the "Dont modify $_" rule with my first ( actually
,, > second) map?
,, 
,, Neither of your map statements actually modify the strings in the
,, array.   I find it interesting that you use a map statement to do the
,, job of a grep statement:
,, my @list =  map {"/$_"} (grep length, split( /\//, $string ));
,, does the same thing, and is more readable.  In fact, why bother with the
,, grep at all?  Splitting on m#/+# (i.e. multiple slashes) will remove its
,, necessity.

No, the grep also removes the "" which will be there when $string
starts with one of more slashes.

,, Also, why do you yank all the slashes out, only to reinsert them?

The above collapses slashes, and adds a leading one of none exists.
But you could write it as:

    @list = "/$string" =~ m{/*(/[^/]+)}g;

,, > Any comments or better ideas ... golfers?
,, >
,, > Note: The number of items may vary so /()()/ wont work for me.
,, >
,, 
,, my @list = ($string =~ m#/?[^/]+#g);
,, 
,, The '?' is for the case of no leading slashes.


    #!/opt/perl/bin/perl -w

    my $string = "foo//bar////baz//";

    my @list1 = map {"/$_"} (grep length, split( /\//, $string ));
    my @list2 = "/$string" =~ m{/*(/[^/]+)}g;
    my @list3 = ($string =~ m#/?[^/]+#g);

    print "@list1\n";
    print "@list2\n";
    print "@list3\n";
    __END__
    /foo /bar /baz
    /foo /bar /baz
    foo /bar /baz


Looks your solution is incorrect.


Abigail
-- 
perl5.004 -wMMath::BigInt -e'$^V=Math::BigInt->new(qq]$^F$^W783$[$%9889$^F47]
 .qq]$|88768$^W596577669$%$^W5$^F3364$[$^W$^F$|838747$[8889739$%$|$^F673$%$^W]
 .qq]98$^F76777$=56]);$^U=substr($]=>$|=>5)*(q.25..($^W=@^V))=>do{print+chr$^V
%$^U;$^V/=$^U}while$^V!=$^W'


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

Date: Fri, 09 Feb 2001 11:00:39 GMT
From: Ian Boreham <iboreham@my-deja.com>
Subject: Re: Using regex to match numbers (not digits)?
Message-Id: <960ikl$kkt$1@nnrp1.deja.com>

In article <95v6n0$hud$1@nnrp1.deja.com>,
  nukes666@my-deja.com wrote:
> I need to search a file which has, let's say, 2000 numbers in it
> between 1 & 2000, each on it's own line. I want to only print numbers
> 700 through 900 and 1200 through 1500.
> <snip>
> Apparently, though, this returns any lines based on digit matches, and
> not numbers. Could someone suggest the appropriate regex to return
> ranges of numbers?

Most other posters have rightly told you that regexes are not the best
way to do this, but for those odd occasions when you don't have any
choice about using one, you can do this sort of thing:

#! /usr/bin/perl -w
use strict;

foreach (<DATA>)
{
    print if /^([78]\d\d|900|1[234]\d\d|1500)$/;
}

__DATA__
1
23
112
699
700
701
701 spurious text
750
899
900
901
1000
1199
1200
1201spurious
1201
1350
spurious1499
1499
1500
1501
15000



However, it gets much ickier if the number ranges aren't so nice.

Regards,


Ian


Sent via Deja.com
http://www.deja.com/


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

Date: Fri, 09 Feb 2001 13:10:09 GMT
From: nukes666@my-deja.com
Subject: Re: Using regex to match numbers (not digits)?
Message-Id: <960q7e$q92$1@nnrp1.deja.com>

Thank you to everyone who replied to my question. The answers were all
excellent, now I just have to pick which one to use and plug it into my
script. Again, thanks!


Sent via Deja.com
http://www.deja.com/


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

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

| NOTE: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.

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


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