[26499] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 8658 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Nov 12 18:05:57 2005

Date: Sat, 12 Nov 2005 15:05:05 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sat, 12 Nov 2005     Volume: 10 Number: 8658

Today's topics:
        A question about m/PATTERN/g - first match disappears <me@privacy.net>
    Re: A question about m/PATTERN/g - first match disappea <1usa@llenroc.ude.invalid>
    Re: A question about m/PATTERN/g - first match disappea <me@privacy.net>
    Re: A question about m/PATTERN/g - first match disappea <1usa@llenroc.ude.invalid>
    Re: A question about m/PATTERN/g - first match disappea <abigail@abigail.nl>
    Re: alphanumeric counter - howto? <Oliver-Bleckmann@freenet.de>
    Re: alphanumeric counter - howto? <segraves_f13@mindspring.com>
    Re: how to interpret string as code? <joe@inwap.com>
    Re: how to interpret string as code? <jurgenex@hotmail.com>
    Re: why the perl documents is hard to understand? <jwkenne@attglobal.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sat, 12 Nov 2005 15:06:24 -0700
From: "Allan M. Due" <me@privacy.net>
Subject: A question about m/PATTERN/g - first match disappears
Message-Id: <3tn7b2Fsn514U1@individual.net>

Hi Folks,

Can someone clear up my misunderstanding of the match operator.  I don't
understand why the following occurs.  First script:

-----
use strict;

my $string = 'one1 blather one2 blather blather one3 One4';
my @array;

if ($string =~ m/one\d+/gis) {
 (@array) = $string =~ m/one\d+/gis;
}

print "@array";
------

produces:

one2 one3 One4

I expected the one1 to be there but it isn't

If change the if line so there is no g

-------
use strict;

my $string = 'one1 blather one2 blather blather one3 One4';
my @array;

if ($string =~ m/one\d+/is) {
 (@array) = $string =~ m/one\d+/gis;
}

print "@array";
--------

I get:

 one1 one2 one3 One4

Why does the first not capture one1 into @array?

I've read the docs but the reason completely eludes me.

TIA

AmD

 -- 
My Sig is a P228
To contact me use: amd1, then the at sign, then xbarx, then obligatory dot
and then com; or just click here = http://xbarx.com/m/amd1




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

Date: Sat, 12 Nov 2005 22:24:42 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: A question about m/PATTERN/g - first match disappears
Message-Id: <Xns970CB121219BFasu1cornelledu@127.0.0.1>

"Allan M. Due" <me@privacy.net> wrote in
news:3tn7b2Fsn514U1@individual.net: 

> Can someone clear up my misunderstanding of the match operator.  I
> don't understand why the following occurs.  First script:
> 
> -----
> use strict;
> 
> my $string = 'one1 blather one2 blather blather one3 One4';
> my @array;
> 
> if ($string =~ m/one\d+/gis) {
>  (@array) = $string =~ m/one\d+/gis;
> }

Now, why do you want to do that? :) You may have overlooked this, but I 
think the most succint explanation is given by the documentation for 
pos. When you match a string using a regex with the g modifier, the 
position of the match is stored, so that the next match can match the 
next occurence (if any). Otherwise, it would not be possible to do:

#!/usr/bin/perl

use strict;
use warnings;

my $s = q{one1 blather one2 blather blather one3 One4};

while ( $s =~ m{ (one\d) }gimsx ) {
   print "$1\n";
}

__END__

> if ($string =~ m/one\d+/is) {
>  (@array) = $string =~ m/one\d+/gis;
> }

There is no need to match twice:

if ( @matches = ($s =~ m{ (one\d) }gimsx) ) {
   print "@matches\n";
}

>  -- 
> My Sig is a P228

I don't know what that is but your sig separator is not correct: It 
should be dash-dash-space-newline.

Sinan
-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html



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

Date: Sat, 12 Nov 2005 15:38:08 -0700
From: "Allan M. Due" <me@privacy.net>
Subject: Re: A question about m/PATTERN/g - first match disappears
Message-Id: <3tn96iFtknp6U1@individual.net>

A. Sinan Unur wrote:

<snip excellent clarification and code suggestion>

Thanks for clearling that up for me, much appreciated.

>> --
>> My Sig is a P228
>
> I don't know what that is but your sig separator is not correct: It
> should be dash-dash-space-newline.
>


My sig separator is dash-dash-space-newline in my sig file.  Must have
messed it up when I posted.  Sorry about that.

A Sig P228 is just that a Sig P228.  Give it a Google if  you are
interested.

-- 
My Sig is a P228
To contact me use: amd1, then the at sign, then xbarx, then obligatory
dot and then com; or just click here = http://xbarx.com/m/amd1




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

Date: Sat, 12 Nov 2005 22:48:14 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: A question about m/PATTERN/g - first match disappears
Message-Id: <Xns970CB51ECABBDasu1cornelledu@127.0.0.1>

"Allan M. Due" <me@privacy.net> wrote in news:3tn96iFtknp6U1
@individual.net:

> A. Sinan Unur wrote:
> 
> <snip excellent clarification and code suggestion>
> 
> Thanks for clearling that up for me, much appreciated.

You are welcome.

>> ... should be dash-dash-space-newline.
> 
> My sig separator is dash-dash-space-newline in my sig file.  Must have
> messed it up when I posted.  Sorry about that.

No problem. My newsreader automatically snips properly formatted 
signatures, that's why I noticed it.

> A Sig P228 is just that a Sig P228.  Give it a Google if  you are
> interested.

I see. Thanks.

Sinan
-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html



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

Date: 12 Nov 2005 22:55:19 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: A question about m/PATTERN/g - first match disappears
Message-Id: <slrndncsmn.2l3.abigail@alexandra.abigail.nl>

A. Sinan Unur (1usa@llenroc.ude.invalid) wrote on MMMMCDLVI September
MCMXCIII in <URL:news:Xns970CB121219BFasu1cornelledu@127.0.0.1>:
~~  
~~  Now, why do you want to do that? :) You may have overlooked this, but I 
~~  think the most succint explanation is given by the documentation for 
~~  pos. When you match a string using a regex with the g modifier, the 
~~  position of the match is stored, so that the next match can match the 
~~  next occurence (if any).


That's for a /g match *IN SCALAR CONTEXT*. In list context, it's quite
different.


Abigail
-- 
perl -we 'print q{print q{print q{print q{print q{print q{print q{print q{print 
               qq{Just Another Perl Hacker\n}}}}}}}}}'    |\
perl -w | perl -w | perl -w | perl -w | perl -w | perl -w | perl -w | perl -w


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

Date: Sat, 12 Nov 2005 21:21:58 +0100
From: "Oliver Bleckmann" <Oliver-Bleckmann@freenet.de>
Subject: Re: alphanumeric counter - howto?
Message-Id: <dl5j31$f6h$1@newsserver.rz.tu-ilmenau.de>

ok, i did it myself.
now this is my solution...

sub base36
{
 my ($num) = @_;
 use integer;
 my @chars  = ('0'..'9', 'A'..'Z');
 my $result = "";
 for(my $b=@chars; $num; $num/=$b)
 {
  $result .= $chars[$num % $b];
 }
 return scalar reverse $result;
}

for ($i = 0; $i <= 2000; $i++ )
{
 $tmp = base36($i);
 for ( $c = length($tmp); $c < 6; $c++ )
 {
 $tmp = "0" . "$tmp";
 }
 print "$tmp\n";
} 




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

Date: Sat, 12 Nov 2005 20:37:22 GMT
From: "Bill Segraves" <segraves_f13@mindspring.com>
Subject: Re: alphanumeric counter - howto?
Message-Id: <6osdf.8240$m81.5094@newsread1.news.atl.earthlink.net>

"Oliver Bleckmann" <Oliver-Bleckmann@freenet.de> wrote in message
news:dl2krm$3p6$1@newsserver.rz.tu-ilmenau.de...
> Hi,
> I need an alphanumeric counter, which counts from 000000 to ZZZZZZ.
> Is there an easy way to do this?

Yes. There is an easy way to do this. Hint: Read the documentation for the
functions pop, push, shift, and unshift, as well as the _Perl Cookbook_,
First Edition, Recipe "4.16. Implementing a circular list.

A six-digit Veeder-Root counter, Hobbs Meter, for the pilots among us, or
automotive odometer, is similar to what you've requested, except for the
additional characters.

1. Consider thirty-six characters, Digits 0-9 and the Letters A-Z
(capitals), as the set of characters needed for your counter. Note: Your
problem statement did not make this clear.
2. Now, consider six wheels, each with the thirty-six characters in 1.
above, one wheel for each of the columns in the display of your counter.
3. Starting with the right-most wheel, each time a wheel goes completely
around, it should increment the next wheel on its left by one count, and so
on ...
4. Write a Perl script that "spins" the above-described six wheels and
displays the state of the six-wheel set after each increment of the
right-most wheel.

The above approach, which does not require the use of any of Perl's
arithmetic operators, has no practical limit on the size of the count, as
the count is never represented numerically. My counter is still running, and
has not yet changed the left-most column.

> PS: Can perl calculate with alphanumeric numbers?

See responses from others.

Cheers.
--
Bill Segraves









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

Date: Sat, 12 Nov 2005 02:49:03 -0800
From: Joe Smith <joe@inwap.com>
Subject: Re: how to interpret string as code?
Message-Id: <bcOdneQ_R_8_VejenZ2dnUVZ_sGdnZ2d@comcast.com>

jason@jumpsql.com wrote:

 > As the fields in my data table are not fixed, ...


In that case, use a variable set of array indexes to
access the appropriate columns.

 >
 > # ------------------------------------------------------------
 > $name = 'John Doe';
 > $fields = '$firstname, $lastname';


$fn_index = 0;
$ln_index = 1;

 > ($fields) = split(/\s/, $name);


Without using 'eval', that makes no sense, and eval is
not the appropriate way to do it.

 > print "$firstname";        # returns John
 > print "$lastname";        # returns Doe


   @fields = split ' ',$name;
   print "First name = $fields[$fn_index]\n";
   print "Last name = $filds[$ln_index]\n";

Another way would be

($firstname,$lastname) = @fields[$fn_index,$ln_index];

     -Joe


 > # ------------------------------------------------------------
 >
 > where line 3 is interpreted as:
 > # ------------------------------------------------------------
 > ($firstname, $lastname) = split(/\s/, $name);
 > # ------------------------------------------------------------
 >
 > How can this be accomplished? Any help would be appreciated. Thank
 > you.
 >
 >
 >
 > Jason Q.



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

Date: Sat, 12 Nov 2005 15:59:31 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: how to interpret string as code?
Message-Id: <Djodf.1933$vS4.1810@trnddc01>

Jason Quek wrote:
> Andrew McGregor <a.mcgregor@pobox.com> wrote:
>
>> Jason Quek wrote:
>>>
>>> How can this be accomplished? Any help would be appreciated. Thank
>>> you.
>>
>> I'm not sure what the question is.  What is line 2 attempting to do?
>> What happens when you replace line 3 with the last example of line 3?
>
>> my ($firstname, $lastname) = split(/\s/, $name);
>
> This works but the problem is the field order of my data is not fixed.
> Sometimes it is ($firstname, $lastname), sometimes it is ($lastname,
> $firstname) so I need specify the order of the fields only during
> script execution.

if ($normal_order) {
    my ($firstname, $lastname) = split(/\s/, $name);
} else {
    my ($lastname, $firstname) = split(/\s/, $name);
}

jue 




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

Date: Sat, 12 Nov 2005 15:57:40 -0500
From: "John W. Kennedy" <jwkenne@attglobal.net>
Subject: Re: why the perl documents is hard to understand?
Message-Id: <fHsdf.2013$ha2.1150@fe08.lga>

Jürgen Exner wrote:
> Xiaoshen Li wrote:
> 
>>Hi,
>>
>>I am learning Perl now. I have computer science background. One
>>important tool for learning a language is to learn how to read its
>>help manual. For example, JAVA has all the functions document pages
>>online.
> 
> 
> How awkward! Do you really have to dial up your ISP, start your favourite 
> browser, ... just to find out how a Java function works?

No, you can download the whole thing as a big .zip or .tar.gz file. 
(It's separate from the JDK because it's larger, and the JDK gets 
updated more often, but it downloads from the same webpage.)

And it uses HTML because it's over 200MiB, which is obviously too big to 
handle with a perldoc-like mechanism.

And, no, it's not larger /because/ it's HTML; ActivePerl puts the POD 
into HTML, and it comes to less than 16MiB. The Java documentation is 
much more detailed documentation of a much larger library.

-- 
John W. Kennedy
"The pathetic hope that the White House will turn a Caligula into a 
Marcus Aurelius is as naïve as the fear that ultimate power inevitably 
corrupts."
   -- James D. Barber (1930-2004)


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

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


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