[24439] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 6623 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri May 28 18:05:55 2004

Date: Fri, 28 May 2004 15:05:06 -0700 (PDT)
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, 28 May 2004     Volume: 10 Number: 6623

Today's topics:
    Re: "Commutative" regexps? (Anno Siegel)
    Re: "Commutative" regexps? <nospam-abuse@ilyaz.org>
    Re: "Commutative" regexps? (Greg Bacon)
        Another Dereference Post by me <mikeflan@earthlink.net>
    Re: Another Dereference Post by me <uri@stemsystems.com>
    Re: Another Dereference Post by me <ittyspam@yahoo.com>
    Re: Another Dereference Post by me <dwall@fastmail.fm>
    Re: create directory ... BreadWithSpam@fractious.net
        DNS::Zoneparse (Jeff Kunzelman)
    Re: DNS::Zoneparse <noreply@gunnar.cc>
    Re: Extracting a data from a string <tore@aursand.no>
    Re: Handling Environmental variables (Satish)
    Re: MyPAN? <vetro@online.no>
    Re: On "for (@foo)" <ittyspam@yahoo.com>
    Re: On "for (@foo)" <nobull@mail.com>
    Re: On "for (@foo)" <usenet@morrow.me.uk>
        Two quick questions about Perl6 <bik.mido@tiscalinet.it>
    Re: Unwanted double-interpolation in string passed to b (Anno Siegel)
    Re: Unwanted double-interpolation in string passed to b (Anno Siegel)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 28 May 2004 20:03:23 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: "Commutative" regexps?
Message-Id: <c985ub$6eu$1@mamenchi.zrz.TU-Berlin.DE>

J Krugman  <jkrugman345@yahbitoo.com> wrote in comp.lang.perl.misc:
> 
> 
> 
> Suppose that I have three different regexps, for example:
> 
>   my $r1 = qr/foo/;
>   my $r2 = qr/bar/;
>   my $r3 = qr/baz/;
> 
> (although not necessarily this simple).  If I wanted to find all
> the documents in an array of documents that matched the three
> regexps, I'd just do:
> 
>   for my $doc (@docs) {
>     if (/$r1/s && /$r2/s && /$r3/s) {
>       go_to_town($_);
>     }
>   }
> 
> But what if I wanted to impose a constraint on how far any two
> regexps can be from each other (e.g. for implementing a NEAR query
> operator).  In that case I'd need expressions
> 
>   /$r1(.*?)$r2/s
> 
> and test for the length of $1.  The problem is that now I'd need
> 6 tests like this like.  If instead of 3 regexps we had N, the
> number of such tests required would be N*(N-1)/2, i.e. quadratic
> growth in N.  But this would be a very inefficient solution.  After
> all, after /($r1|$r2|$r3)/s matched, and say the match was $r2, we
> know that we are interested only in the distance between this match
> and the next match of /($r1|$r3)/s.  And once this matched, say to
> $r3, we are only interested in the distance between this match and
> the next match to /$r1/s.  This approach is linear in N.  The
> problem is that I can't figure out how to code it in a way that
> preserves this linear property.  The first obstacle is how to
> determine which of the regexps in a disjunction (e.g. /($r1|$r2|$r3)/)
> actually matched, without having to perform O(N) tests (which would
> put me back in O(N^2) territory for the whole algorithm).
> 
> Of course, if there were a way to tell Perl to look for lines that
> matched $r1, $r2, and $r3 in *any* order, coding this would be
> relatively simple (though the results not necessarily faster).  Is
> there any way to coax the regexp engine to handle such "commutative
> regexps"?

No, and-matches are notoriously hard to code in a single regex.

I'd find all matches of any of the regexes and then see if the substring
of given length starting there contains the others.  Something like this
(tested, but not debugged):

    sub match_all_near {
        for ( shift ) { # document text in $_
            my ( $width, @regex) = @_;
            my $any = join '|', @regex;
            while ( /$any/g ) {
                my $where = $-[ 0];
                for ( substr( $_, $where, $width) ) { # substring in $_
                    my $match = 1;
                    for my $re ( @regex ) {
                        $match &&= /$re/;
                        last unless $match;
                    }
                    print "match at $where:\n$_\n" if $match;
                }
            }
        }
    }

The inner loop I simply checks all regexes again, even though it is
known that one already matched.  It's not worth the hassle to skip it.

The way it is written it reports all matches, which includes overlapping
ones.  If only (the first) success must be reported, that is no problem.
Otherwise, pos() could be set to the end of the substring after each
match.

It's basically linear in n (of regexes).  For large n, the alternation
in /$any/ may dominate run time -- I'm not sure how alternations are
handled.

Anno


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

Date: Fri, 28 May 2004 20:25:53 +0000 (UTC)
From:  Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: "Commutative" regexps?
Message-Id: <c9878h$1dp8$1@agate.berkeley.edu>

[A complimentary Cc of this posting was sent to
J Krugman 
<jkrugman345@yahbitoo.com>], who wrote in article <c97ttr$nu0$1@reader2.panix.com>:
>   /$r1(.*?)$r2/s
> 
> and test for the length of $1.  The problem is that now I'd need
> 6 tests like this like.  If instead of 3 regexps we had N, the
> number of such tests required would be N*(N-1)/2, i.e. quadratic
> growth in N.

If you allow overlapping, then you can just AND the expressions:

  '\A(?:' . join( '|', map ".*?$_", @rex ) . ')'

(untested; qr/./ may need to be //s'ed).

> But this would be a very inefficient solution.  After
> all, after /($r1|$r2|$r3)/s matched, and say the match was $r2, we
> know that we are interested only in the distance between this match
> and the next match of /($r1|$r3)/s.  And once this matched, say to
> $r3, we are only interested in the distance between this match and
> the next match to /$r1/s.

If you want to match all them at a small distance (and allow
overlapping), you can have a "restricted AND" (drop \A, and restrict .*? ):

  '(?:' . join( '|', map ".{0,64}?$_", @rex ) . ')'

(untested).  I'm not sure whether this is sufficiently efficient for
your needs - but you should be able to calculate complexity yourselves...

Hope this helps,
Ilya


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

Date: Fri, 28 May 2004 21:04:39 -0000
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: "Commutative" regexps?
Message-Id: <10bfab7q1ehhb84@corp.supernews.com>

In article <c97ttr$nu0$1@reader2.panix.com>,
    J Krugman  <jkrugman345@yahbitoo.com> wrote:

: [...]
: Of course, if there were a way to tell Perl to look for lines that
: matched $r1, $r2, and $r3 in *any* order, coding this would be
: relatively simple (though the results not necessarily faster).  Is
: there any way to coax the regexp engine to handle such "commutative
: regexps"?

You can match in any order using positive lookahead assertions:

    if (/(?=.*$r1)(?=.*$r2)(?=.*$r3)/) { ... }

What if you knew the starting and ending indices of a match?

    sub startend {
        my $pat = shift;
        my $str = shift;

        return unless $str =~ /$pat/gc;

        # compute length($&) with $+[0] - $-[0] (see perlvar)
        # to avoid even mentioning $& and to avoid adding
        # parentheses that might throw off $pat's backrefs
        (pos($str) - $+[0] + $-[0], pos($str) - 1);
    }

I don't know if it's possible to find where a lookahead match started
and stopped.

Hope this helps,
Greg
-- 
Government never furthered any enterprise but by the alacrity with which
it got out of its way.
    -- Henry David Thoreau


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

Date: Fri, 28 May 2004 19:05:42 GMT
From: Mike Flannigan <mikeflan@earthlink.net>
Subject: Another Dereference Post by me
Message-Id: <40B78D79.FCDA6DBA@earthlink.net>


I've learned quite a bit messing with this script over the last
couple hours.  The first part of the script below to find
duplicates works fine.  Now I'm trying to sort the resulting
list.

I think the problem with this code is that the long @sorted
statement near the bottom does not dereference the values.
I know - I have posted about this kind of thing many times
before, but I am still very rusty in this area.  Could somebody
show me how to get it to properly sort the data?

Then what I really want is to sort by the 5th column that looks
like "381715N0930417W", keeping in mind that there are
duplicate values in that column.  I suspect I can get this done
myself with a hash or two, but first I'd like to learn this
dereference thing.


Thanks for your help,

Mike Flannigan


use strict;
use warnings;

use constant COL1 => 0;
use constant COL2 => 1;
use constant COL3 => 2;
use constant COL4 => 3;
use constant COL5 => 4;
use constant COL6 => 5;


my @data = map [ split /\|/, $_, 5 ], <DATA>;

# Identify duplicate lat/long's
my %saw;
foreach my $line (@data) {
    print "$line->[ COL1 ]\n";
    if ($saw{ $line->[ COL5 ] }++) {
        print "found dup\n";
        $line->[ COL1 ] .= "+++++";
        }
    }
print "\n";

my @sorted =
  map $_->[0],
  sort { $b->[1] cmp $a->[1] }
  map [$_, /(.*)/ ? $1 : die "$_"],
  @data;

#print the sorted list
foreach my $line (@sorted) {
    print join("|" => @$line);
    }

# I do this just to look at the reference name
foreach my $line (@sorted) {
    print join("|", $line);
    }

__DATA__
Camdenton Medical Center|Green Bay
Terrace|Camden|hospital|380141N0924604W|MO
Carpenter Memorial State Wildlife Area|Boylers
Mill|Morgan|park|381715N0930417W|MO|||||0
Mansfield Forest State Wildlife Management Area|Lake
Ozark|Camden|park|380823N0924256W|MO|||||0
Brockman Island|Bagnell|Miller|area|380141N0924604W|MO
Hawaiian Island|Lake Ozark|Miller|island|381317N0924115W|MO|||||0



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

Date: Fri, 28 May 2004 19:22:51 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Another Dereference Post by me
Message-Id: <x77juwe4qc.fsf@mail.sysarch.com>

>>>>> "MF" == Mike Flannigan <mikeflan@earthlink.net> writes:

  MF> use constant COL1 => 0;
  MF> use constant COL2 => 1;
  MF> use constant COL3 => 2;
  MF> use constant COL4 => 3;
  MF> use constant COL5 => 4;
  MF> use constant COL6 => 5;

what does adding COL do?

  MF> my @data = map [ split /\|/, $_, 5 ], <DATA>;

  MF> # Identify duplicate lat/long's
  MF> my %saw;
  MF> foreach my $line (@data) {
  MF>     print "$line->[ COL1 ]\n";
  MF>     if ($saw{ $line->[ COL5 ] }++) {
  MF>         print "found dup\n";
  MF>         $line->[ COL1 ] .= "+++++";
  MF>         }
  MF>     }

that is a very odd indent for the close }. try putting it under the line
that opened it. this is the 'approved' perl indent style and most
hackers use it. it is also the gnu style.


  MF> print "\n";

  MF> my @sorted =
  MF>   map $_->[0],
  MF>   sort { $b->[1] cmp $a->[1] }
  MF>   map [$_, /(.*)/ ? $1 : die "$_"],

@data is a array of anon arrays. so map will set $_ to an array ref each
time. and /(.*)/ is matching against that array ref (NOT the array data)
which is meaningless.

  MF>   @data;

good idea would be to describe the sort operation.

  MF> #print the sorted list
  MF> foreach my $line (@sorted) {
  MF>     print join("|" => @$line);

where is the useful trailing newline?

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: Fri, 28 May 2004 15:24:21 -0400
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: Another Dereference Post by me
Message-Id: <20040528151339.C18263@dishwasher.cs.rpi.edu>

On Fri, 28 May 2004, Mike Flannigan wrote:

> I think the problem with this code is that the long @sorted
> statement near the bottom does not dereference the values.
> I know - I have posted about this kind of thing many times
> before, but I am still very rusty in this area.  Could somebody
> show me how to get it to properly sort the data?
>
> Then what I really want is to sort by the 5th column that looks
> like "381715N0930417W", keeping in mind that there are
> duplicate values in that column.  I suspect I can get this done
> myself with a hash or two, but first I'd like to learn this
> dereference thing.
>
> use strict;
> use warnings;
>
> my @data = map [ split /\|/, $_, 5 ], <DATA>;
>
> my @sorted =
>   map $_->[0],
>   sort { $b->[1] cmp $a->[1] }
>   map [$_, /(.*)/ ? $1 : die "$_"],
>   @data;

What the hey is this?  Looking at your (bottom) map statement, foreach
element of @data, you're creating an anonymous array reference.  The first
element of this array is the current element of @data.  The second element
of this array is the grouped subpattern of /(.*)/ if that pattern match
succeeded.  There are a number of very bizzare issues here:

1) /(.*)/  cannot fail.  Ever.  Why are you adding code to check for this?
2) .* will match as much of the string as it can.   Since there are no
newlines in your data, and .* isn't being anchored to any other part of a
match, $1 will therefore contain the entire string - that is, it will be
identical to $_
3) So now you've created a reference to an array containing [$_, $_].
Why?  It looks like you've decided to use a Schwartzian transform without
understanding why you'd do such a thing.

In your script, @data is an array of array references, each containing at
most 5 elements.  You said you want to sort by the fifth column.  Looking
at your data, however, it looks like there are some lines that do not have
a fifth column.  What do you want to do with those rows?

Ignoring that detail for the moment, the sort you say you want to perform
is far easier than you tried to make it:

my @sorted = sort { $a->[4] cmp $b->[4] } @data;

This works because within the sort subroutine, $a and $b will be two
elements of @data.  Each element of @data is an array reference.  To get
at the elements of an array when you have it's reference, you can
dereference it using the arrow operator as I've done above.

For more info on referencing and dereferencing, consult perldoc perlreftut


If I haven't understood your issue, consider making your goal more
explicit.

Paul Lalli


> __DATA__
> Camdenton Medical Center|Green Bay
> Terrace|Camden|hospital|380141N0924604W|MO
> Carpenter Memorial State Wildlife Area|Boylers
> Mill|Morgan|park|381715N0930417W|MO|||||0
> Mansfield Forest State Wildlife Management Area|Lake
> Ozark|Camden|park|380823N0924256W|MO|||||0
> Brockman Island|Bagnell|Miller|area|380141N0924604W|MO
> Hawaiian Island|Lake Ozark|Miller|island|381317N0924115W|MO|||||0
>
>


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

Date: Fri, 28 May 2004 20:39:56 -0000
From: "David K. Wall" <dwall@fastmail.fm>
Subject: Re: Another Dereference Post by me
Message-Id: <Xns94F7A987AA7D1dkwwashere@216.168.3.30>

Uri Guttman <uri@stemsystems.com> wrote:

>>>>>> "MF" == Mike Flannigan <mikeflan@earthlink.net> writes:

>   MF> # Identify duplicate lat/long's
>   MF> my %saw;
>   MF> foreach my $line (@data) {
>   MF>     print "$line->[ COL1 ]\n";
>   MF>     if ($saw{ $line->[ COL5 ] }++) {
>   MF>         print "found dup\n";
>   MF>         $line->[ COL1 ] .= "+++++";
>   MF>         }
>   MF>     }
> 
> that is a very odd indent for the close }. try putting it under
> the line that opened it. this is the 'approved' perl indent style
> and most hackers use it. it is also the gnu style.

The indenting makes perfect sense -- if you're writing Python code. :-)



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

Date: 28 May 2004 15:25:48 -0400
From: BreadWithSpam@fractious.net
Subject: Re: create directory ...
Message-Id: <yobiseguzer.fsf@panix1.panix.com>

Martien Verbruggen <mgjv@tradingpost.com.au> writes:

> What's wrong with the builtin function mkdir?

FWIW, I usually use File::Path's mkpath as well.  Because:

DESCRIPTION
       The "mkpath" function provides a convenient way to create
       directories, even if your "mkdir" kernel call won't create more
       than one level of directory at a time.  "mkpath" takes three
       arguments:

No need for me to write code to walk the tree and create
elements as I go - someone else has already done it for me.

-- 
Plain Bread alone for e-mail, thanks.  The rest gets trashed.
No HTML in E-Mail! --    http://www.expita.com/nomime.html
Are you posting responses that are easy for others to follow?
   http://www.greenend.org.uk/rjk/2000/06/14/quoting


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

Date: 28 May 2004 13:33:31 -0700
From: jeff@arcosanti.org (Jeff Kunzelman)
Subject: DNS::Zoneparse
Message-Id: <27415bc9.0405281233.2a6877c8@posting.google.com>

I'm having trouble getting the Zoneparse module to update SOA records.
Can someone see what I'm doing wrong? There's a beer in if for you if
you can.

use DNS::ZoneParse;
    
    my $zoneTemplateFile = DNS::ZoneParse->new("fwdTest.soa");
    
    # Get a reference to the MX records
    my $templateMx = $zoneTemplateFile->mx;  
    my %templateSOA = $zoneTemplateFile->soa;

    #read in file of DNS informaton. Then repeat for every record in
the file

    # begin zone file creation
    # Change zone file attributes

    %templateSOA = ( 'origin' => 'us-lbt-sn01-ice-rt01',
                      'primary' => 'primary',
                      'email' => 'email'
                         );

    $templateMx->[0] = { host => 'mail.localhost.com',
                 priority => 10,
                 name => '@' };
    
    # update the serial number
    $zoneTemplateFile->new_serial();

    print  $templateSOA{'ttl'};



    #write the new zone file to disk 
    open NEWZONE, ">zonefile2.db" or die "error"; 

    print NEWZONE $zoneTemplateFile->output();
    close NEWZONE;

    #end  zone file creation


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

Date: Fri, 28 May 2004 22:47:53 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: DNS::Zoneparse
Message-Id: <2hpn24Ffeug4U1@uni-berlin.de>

Jeff Kunzelman wrote:
> I'm having trouble getting the Zoneparse module to update SOA
> records.

I have seen this question posted to three newsgroups in three
different messages. It's called multi-posting, and is considered rude.
Maybe you should give it a thought why that is the case...

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl



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

Date: Fri, 28 May 2004 23:55:36 +0200
From: Tore Aursand <tore@aursand.no>
Subject: Re: Extracting a data from a string
Message-Id: <pan.2004.05.28.21.49.09.980760@aursand.no>

On Fri, 28 May 2004 19:38:05 +0400, Dan wrote:
> <img src="http://mywebsite_address/mypath/-picture.gif"></a><br>
> 
> How can I extract "-picture.gif" from this string ?

perldoc HTML::LinkExtor
perldoc perlretut


-- 
Tore Aursand <tore@aursand.no>
"Scientists are complaining that the new "Dinosaur" movie shows
 dinosaurs with lemurs, who didn't evolve for another million years.
 They're afraid the movie will give kids a mistaken impression. What
 about the fact that the dinosaurs are singing and dancing?" (Jay Leno)


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

Date: 28 May 2004 14:12:30 -0700
From: satishsanthanam@yahoo.com (Satish)
Subject: Re: Handling Environmental variables
Message-Id: <d04d15e8.0405281312.1de1f0a6@posting.google.com>

"J. Gleixner" <glex_nospam@qwest.invalid> wrote in message news:<9brtc.131$Ri6.70556@news.uswest.net>...
> Satish wrote:
> > Hi,
> > 
> > How do I set/unset unix environmental variables from a perl script?
> 
> It's 'environment' variables. You operated on %ENV.
> 
> $ENV{'PATH'} .= ':/some/other/path';
> 
> foreach my $var (keys %ENV)
> {
> 	print $var, ':', $ENV{$var}, "\n";
> }
Thanks this helped.
Satish


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

Date: Sat, 29 May 2004 01:46:14 +0200
From: "Vetle Roeim" <vetro@online.no>
Subject: Re: MyPAN?
Message-Id: <opr8qhncfp3hk3cf@localhost>

On Fri, 28 May 2004 16:38:11 GMT, Bill Segraves  
<segraves_f13@mindspring.com> wrote:

> "Vetle Roeim" <vetro@online.no> wrote in message
> news:opr8pdzqvo3hk3cf@quickfix.opera.com...
>>    Hi folks. CPAN is really great. It is in fact so great, that we'd
> really
>>    like to set up our own CPAN-ish server where I work, to make it  
>> easier
> to
>>    distribute and install our own modules.
>
> Randal L. Schwartz' articles at www.stonehenge.com may be helpful to you.
>
> http://www.stonehenge.com/merlyn/LinuxMag/col42.html
> http://www.stonehenge.com/merlyn/LinuxMag/col43.html

   I did find these articles, but they did not provide the silver bullet
   I was looking for. However, they will be useful if I need to roll my
   own solution.

   Guess I'll have to get my hands dirty, then... ;) I'll take a closer
   look at CPAN::Site first, though.


> Randal's articles would fall into the "If You Like" category of this
> newsgroup's posting guidelines. IMO, you'll save yourself a lot of time  
> "If You Like" this important resource.
>
> Many thanks, Randal.

   Indeed, but I refuse to believe he's only one man... The name is
   obviously a pseudonym for a large group of genious Perl-hackers churning
   out articles and books! ;)


-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/


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

Date: Fri, 28 May 2004 14:07:43 -0400
From: Paul Lalli <ittyspam@yahoo.com>
Subject: Re: On "for (@foo)"
Message-Id: <20040528140443.P18263@dishwasher.cs.rpi.edu>

On Fri, 28 May 2004, Jim Gibson wrote:

> In article <c96vkq$kkk$1@wisteria.csv.warwick.ac.uk>, Ben Morrow
> <usenet@morrow.me.uk> wrote:
>
> > You never need C-style loops:
> >
> > for my $i (0..$#X) {
> >     $X[$i] = 0;
> > }
> >
> > Ben
>
> You do if you want to skip some elements:
>
> Jim 53% cat loops.pl
> #!/usr/local/bin/perl
> use strict;
> use warnings;
>
> my @array = ( 0..4 );
>
> print "C-style:\n";
> for( my $i = 0; $i < @array; $i++ ) {
>   print "$i. $array[$i]\n";
>   $i++ if $i == 2;
> }
>
> print "Perlish:\n";
> for my $i ( 0 .. $#array ) {
>   print "$i. $array[$i]\n";
>   $i++ if $i == 2;
> }

Ew.  Why would you ever do that?  In either of those loops?  Incrementing
the count variable within a Cstyle loop is just terrible.

for (my $i=0; $i<@array; $i++){
   next if $i == 2;
   print "$i, $array[$i]\n";
}

for my $i (0..$#array){
   next if $i == 2;
   print "$i, $array[$i]\n";
}


Paul Lalli


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

Date: 28 May 2004 19:26:11 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: On "for (@foo)"
Message-Id: <u9pt8ov264.fsf@wcl-l.bham.ac.uk>

Paul Lalli <ittyspam@yahoo.com> writes:

> On Fri, 28 May 2004, Jim Gibson wrote:

> > for( my $i = 0; $i < @array; $i++ ) {
> >   print "$i. $array[$i]\n";
> >   $i++ if $i == 2;
> > }

> Ew.  Why would you ever do that?  In either of those loops?  Incrementing
> the count variable within a Cstyle loop is just terrible.

Yeah verily!  The c-style loop is just a shorthand for a while loop,
you you are going to be manipulating the loop control variable
anywhere other than in the for(;;) it's better IMNSHO to write out the
while loop long hand.

>    next if $i == 2;

It is also common in a c-style loop to decrement the counter in order
to redo the current element but Perl has an explicit redo command.

my %redo = ( 2 => 2 );
for my $i ( 0 .. 10 ) {
  print "$i ";
  if ( $redo{$i} ) {
     $redo{$i}--;
     redo;
  }
}
__END__
0 1 2 2 2 3 4 5 6 7 8 9 10 


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

Date: Fri, 28 May 2004 18:40:26 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: On "for (@foo)"
Message-Id: <c9812q$gq4$1@wisteria.csv.warwick.ac.uk>


Quoth Jim Gibson <jgibson@mail.arc.nasa.gov>:
> In article <c96vkq$kkk$1@wisteria.csv.warwick.ac.uk>, Ben Morrow
> <usenet@morrow.me.uk> wrote:
> 
> > Quoth "Andrew Palmer" <atp5470 at fsu.edu>:
> > > > OK, now, in an expression like this
> > > >
> > > >   for my $x (@X) {
> > > >     # etc.
> > > >   }
> > > >
> > > > I assume that Perl keeps track of the index on @X correponding to
> > > > the current element.  Is there a way to access this "current index"
> > > > from within a Perl program?
> > > 
> > > I think the only way to do what you want is like
> > > 
> > > for(my $i=0;$i<$#X;++$i)
> > > {
> > >   $X[$i]=0;
> > > }
> > 
> > You never need C-style loops:
> > 
> > for my $i (0..$#X) {
> >     $X[$i] = 0;
> > }
> > 
> > Ben
> 
> You do if you want to skip some elements:
> 
> Jim 53% cat loops.pl
> #!/usr/local/bin/perl
> use strict;
> use warnings;
> 
> my @array = ( 0..4 );
> 
> print "C-style:\n";
> for( my $i = 0; $i < @array; $i++ ) {
>   print "$i. $array[$i]\n";
>   $i++ if $i == 2;
> }
> 
> print "Perlish:\n";
> for my $i ( 0 .. $#array ) {

next if $i == 3;

>   print "$i. $array[$i]\n";
>   $i++ if $i == 2;
> }

Ben

-- 
"The Earth is degenerating these days. Bribery and corruption abound.
Children no longer mind their parents, every man wants to write a book,
and it is evident that the end of the world is fast approaching."
     -Assyrian stone tablet, c.2800 BC                         ben@morrow.me.uk


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

Date: Fri, 28 May 2004 23:40:54 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Two quick questions about Perl6
Message-Id: <mdmeb093kvir851r8i18f65bt6gh3f1e5p@4ax.com>

I've been reading the first few chapters of "Perl6 Essentials" and I
have two quick (generic) questions:

(1) Don't you think that whereas the new language will indeed be more
consistent and robust, it will also be *slightly* less magic?

(2) Perl is known for being inspired from a mixture of many other
languages/programs. It seems evident that the new OO features of Perl6
resemble a lot those of some widely used OO languages, but are there
any other languages that specifically influenced some choices about
its syntax and semantic?


Michele
-- 
you'll see that it shouldn't be so. AND, the writting as usuall is
fantastic incompetent. To illustrate, i quote:
- Xah Lee trolling on clpmisc,
  "perl bug File::Basename and Perl's nature"


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

Date: 28 May 2004 18:39:38 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Unwanted double-interpolation in string passed to backticks
Message-Id: <c9811a$334$1@mamenchi.zrz.TU-Berlin.DE>

Paul Lalli  <ittyspam@yahoo.com> wrote in comp.lang.perl.misc:
> On Fri, 28 May 2004, Anno Siegel wrote:
> 
> > > However, Perl gives you a way to do this:
> > >
> > > $return = `ls -m \Q$filename\E`;
> > > or
> > > $filename = quotemeta $filename
> > > $return = `ls -m $filename`;
> > >
> > > Read about quotemeta in perldoc -f quotemeta, and \Q  in perldoc perlop
> > > under "Quote and Quote-like Operators"
> >
> > With quotemeta() you'll also escape "/", which is unwanted in file
> > names.
> 
> Unneeded, perhaps.  But it has no ill effect, at least not with the shell
> I'm using (I believe it's bash).
> 
> ls -al foo\/bar
> has the same effect as
> ls -al foo/bar
> 
> Are there shells out there that would throw an error at this?

I don't know, but the fact that the question even arises makes a case
against using quotemeta to quote strings for a shell.  It may work,
but it's not the right tool for the task.

Anno


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

Date: 28 May 2004 19:02:22 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Unwanted double-interpolation in string passed to backticks
Message-Id: <c982bu$334$2@mamenchi.zrz.TU-Berlin.DE>

Ben Morrow  <usenet@morrow.me.uk> wrote in comp.lang.perl.misc:

[...]

> my $ret = do {
>     open my $LS, '-|', ls => -m => $filename
>         or die "can't fork ls: $!";
>     local $/;
>     <$LS>;
> };
> 
> As a separate issue, is possible to define some sort of DESTROY method
> to call die automatically if the implicit close at end of scope fails?
> It would make this sort of code both safe and clean.

You can bless the globref that is the filehandle into a class, so yes.
DESTROY is called (immediately) before the handle is closed, so you'd
have to close it yourself to warn on errors.

There is, of course, no good way to give the user the name under which
the handle was originally opened.  That could be handled if the blessing
happened at open() time, when the file name is known.

Another problem with this approach turns up when the filehandle already is
a blessed object.

Anno


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

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.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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


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