[22409] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4630 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Feb 26 18:10:40 2003

Date: Wed, 26 Feb 2003 15:10:13 -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           Wed, 26 Feb 2003     Volume: 10 Number: 4630

Today's topics:
        please help me, my program will not work <tyrannous@o-space.com>
    Re: please help me, my program will not work <klaus@eltermann.com>
    Re: please help me, my program will not work <tyrannous@o-space.com>
    Re: please help me, my program will not work <klaus@eltermann.com>
    Re: please help me, my program will not work (Sam Holden)
    Re: please help me, my program will not work <tyrannous@o-space.com>
    Re: please help me, my program will not work (Tad McClellan)
    Re: please help me, my program will not work (Jay Tilton)
    Re: please help me, my program will not work <glex_nospam@qwest.net>
        Quickie: Contents of Array into 1 Varaible <spero126NOSPAM@yahoo.com>
    Re: Quickie: Contents of Array into 1 Varaible <spero126NOSPAM@yahoo.com>
    Re: Quickie: Contents of Array into 1 Varaible <johannes.fuernkranz@t-online.de>
    Re: Reading Multiple Input Files with <> <klaus@eltermann.com>
    Re: regexp - Alternative to $& (Tad McClellan)
    Re: regexp - Alternative to $& <asu1@c-o-r-n-e-l-l.edu>
    Re: regexp producing uninitialized var errors. <REMOVEsdnCAPS@comcast.net>
        remove unwanted characters <bing-du@tamu.edu>
    Re: remove unwanted characters <ajglist@izzy.net>
    Re: remove unwanted characters <uri@stemsystems.com>
    Re: remove unwanted characters (Tad McClellan)
        scan text to find hyperlinks ? <ericosman-nospam@rcn.com>
    Re: Simple question about Perl Regular Expressions (Anno Siegel)
    Re: Simple question about Perl Regular Expressions <ericm@vertical........com>
        splitting <johannes.fuernkranz@t-online.de>
    Re: splitting <mgjv@tradingpost.com.au>
    Re: splitting <shondell@cis.ohio-state.edu>
    Re: splitting <johannes.fuernkranz@t-online.de>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 26 Feb 2003 19:51:12 -0000
From: <tyrannous@o-space.com>
Subject: please help me, my program will not work
Message-Id: <b3j5re$du$1@news7.svr.pol.co.uk>

This program is the foundation for an ecological simulation. I is very basic
at present, but i plan to build on it.

Anyway, it is supposed to display the itmes on the screen and then wait for
1 second, repeating the process infinitly. This not what is happening. Can
anyone help me?


if (!opendir(MAP25X80, "map_25x80"))
{

   mkdir 'map_25x80';
   $a=1;
   $b=1;

   while ($a!=26)
   {

      open(SAVE, ">./map_25x80/R$a.til") or die;

      while ($b!=81)
      {

         $ooe_ran=int(rand(10));
         print SAVE "object\n" if ($ooe_ran == 0);
         print SAVE "object\n" if ($ooe_ran == 1);
         print SAVE "empty\n" if ($ooe_ran == 2);
         print SAVE "empty\n" if ($ooe_ran == 3);
         print SAVE "empty\n" if ($ooe_ran == 4);
         print SAVE "empty\n" if ($ooe_ran == 5);
         print SAVE "empty\n" if ($ooe_ran == 6);
         print SAVE "empty\n" if ($ooe_ran == 7);
         print SAVE "empty\n" if ($ooe_ran == 8);
         print SAVE "empty\n" if ($ooe_ran == 9);
         $b++;

      }

      close(SAVE);
      $b=1;
      $a++;

   }

}

system "copy map_25x80\\*.*";

open(ROW25, "R25.til") or die "Error: Cannot open R25.til in current
directory.\n";
@row25=<ROW25>;
close(ROW25);

$row25[0]="robot\n";
$row25[1]="empty\n";
$row25[2]="empty\n";
$row25[3]="empty\n";
$row25[4]="empty\n";

open(ROW25, ">R25.til") or die "Error: Cannot write to R25.til in current
directory.\n";
print ROW25 @row25;
close(ROW25);

@row25=();

&cycle;

sub drawscreen
{

   system "cls";
   @ds_a=();
   $ds_c=1;

   until ($ds_c==26)
   {

      open(ROW, "R$ds_c.til") or die "Error: Cannot open R$ds_c.til in
current directory.\n";
      @ds_a=<ROW>;
      close(ROW);

      foreach (@ds_a)
      {

         chomp $_;
         if ($_ eq "empty")
         {

            print ".";

         }
         if ($_ eq "object")
         {

            print "o";

         }
         if ($_ eq "robot")
         {

            print "C";

         }

      }

      $ds_c++;

   }

   @ds_a=();
   $ds_c="";

}

sub cycle
{

   &drawscreen;
   sleep 1;
   &cycle;

}




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

Date: Wed, 26 Feb 2003 21:12:39 +0100
From: Klaus Eltermann <klaus@eltermann.com>
Subject: Re: please help me, my program will not work
Message-Id: <b3j7m3$jt4$3@news.online.de>

tyrannous@o-space.com wrote:

> sub cycle
> {
> 
>    &drawscreen;
>    sleep 1;
>    &cycle;
> 
> }

You are aware, that you are getting recursive call's without a break 
condition, are you?

cycle() will call itself, will call itself, ... until you run out off 
stack space.

CU Klaus



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

Date: Wed, 26 Feb 2003 20:49:11 -0000
From: <tyrannous@o-space.com>
Subject: Re: please help me, my program will not work
Message-Id: <b3j985$t95$1@news6.svr.pol.co.uk>

So then how do i get it to call itself without stacking?

<tyrannous@o-space.com> wrote in message
news:b3j5re$du$1@news7.svr.pol.co.uk...
> This program is the foundation for an ecological simulation. I is very
basic
> at present, but i plan to build on it.
>
> Anyway, it is supposed to display the itmes on the screen and then wait
for
> 1 second, repeating the process infinitly. This not what is happening. Can
> anyone help me?
>
>
> if (!opendir(MAP25X80, "map_25x80"))
> {
>
>    mkdir 'map_25x80';
>    $a=1;
>    $b=1;
>
>    while ($a!=26)
>    {
>
>       open(SAVE, ">./map_25x80/R$a.til") or die;
>
>       while ($b!=81)
>       {
>
>          $ooe_ran=int(rand(10));
>          print SAVE "object\n" if ($ooe_ran == 0);
>          print SAVE "object\n" if ($ooe_ran == 1);
>          print SAVE "empty\n" if ($ooe_ran == 2);
>          print SAVE "empty\n" if ($ooe_ran == 3);
>          print SAVE "empty\n" if ($ooe_ran == 4);
>          print SAVE "empty\n" if ($ooe_ran == 5);
>          print SAVE "empty\n" if ($ooe_ran == 6);
>          print SAVE "empty\n" if ($ooe_ran == 7);
>          print SAVE "empty\n" if ($ooe_ran == 8);
>          print SAVE "empty\n" if ($ooe_ran == 9);
>          $b++;
>
>       }
>
>       close(SAVE);
>       $b=1;
>       $a++;
>
>    }
>
> }
>
> system "copy map_25x80\\*.*";
>
> open(ROW25, "R25.til") or die "Error: Cannot open R25.til in current
> directory.\n";
> @row25=<ROW25>;
> close(ROW25);
>
> $row25[0]="robot\n";
> $row25[1]="empty\n";
> $row25[2]="empty\n";
> $row25[3]="empty\n";
> $row25[4]="empty\n";
>
> open(ROW25, ">R25.til") or die "Error: Cannot write to R25.til in current
> directory.\n";
> print ROW25 @row25;
> close(ROW25);
>
> @row25=();
>
> &cycle;
>
> sub drawscreen
> {
>
>    system "cls";
>    @ds_a=();
>    $ds_c=1;
>
>    until ($ds_c==26)
>    {
>
>       open(ROW, "R$ds_c.til") or die "Error: Cannot open R$ds_c.til in
> current directory.\n";
>       @ds_a=<ROW>;
>       close(ROW);
>
>       foreach (@ds_a)
>       {
>
>          chomp $_;
>          if ($_ eq "empty")
>          {
>
>             print ".";
>
>          }
>          if ($_ eq "object")
>          {
>
>             print "o";
>
>          }
>          if ($_ eq "robot")
>          {
>
>             print "C";
>
>          }
>
>       }
>
>       $ds_c++;
>
>    }
>
>    @ds_a=();
>    $ds_c="";
>
> }
>
> sub cycle
> {
>
>    &drawscreen;
>    sleep 1;
>    &cycle;
>
> }
>
>




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

Date: Wed, 26 Feb 2003 21:49:33 +0100
From: Klaus Eltermann <klaus@eltermann.com>
Subject: Re: please help me, my program will not work
Message-Id: <b3j9ra$l7a$1@news.online.de>

tyrannous@o-space.com wrote:
> So then how do i get it to call itself without stacking?
> 
----- 8< ---------------
>>
>>@row25=();
>>
>>&cycle; <------- REMOVE/CHANGE TO

while( 1 ) {
	drawscreen;
	sleep( 1 );
}

###########

remove also the body of cycle();

So you will draw the screen, sleep a while, draw the screen, for ever 
and ever :-)

CU Klaus



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

Date: 26 Feb 2003 21:04:55 GMT
From: sholden@flexal.cs.usyd.edu.au (Sam Holden)
Subject: Re: please help me, my program will not work
Message-Id: <slrnb5qavn.fdb.sholden@flexal.cs.usyd.edu.au>

On Wed, 26 Feb 2003 20:49:11 -0000, tyrannous@o-space.com wrote:
> So then how do i get it to call itself without stacking?

Instead of:

sub foo {
	# ... code ...
	foo()
}

use:

sub for {
	while (1) {
		# ... code ...
	}
}

And don't top post... And do trim the post to only what you are replying to...


-- 
Sam Holden



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

Date: Wed, 26 Feb 2003 21:33:49 -0000
From: <tyrannous@o-space.com>
Subject: Re: please help me, my program will not work
Message-Id: <b3jbrq$j0g$1@newsg1.svr.pol.co.uk>

I made the suggested changes.

The screen only displays for a glimps of a second and then goes blank (over
and over again)

What is wrong with it now?

"Klaus Eltermann" <klaus@eltermann.com> wrote in message
news:b3j9ra$l7a$1@news.online.de...
> tyrannous@o-space.com wrote:
> > So then how do i get it to call itself without stacking?
> >
> ----- 8< ---------------
> >>
> >>@row25=();
> >>
> >>&cycle; <------- REMOVE/CHANGE TO
>
> while( 1 ) {
> drawscreen;
> sleep( 1 );
> }
>
> ###########
>
> remove also the body of cycle();
>
> So you will draw the screen, sleep a while, draw the screen, for ever
> and ever :-)
>
> CU Klaus
>




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

Date: Wed, 26 Feb 2003 16:09:22 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: please help me, my program will not work
Message-Id: <slrnb5qeoi.5u4.tadmc@magna.augustmail.com>

tyrannous@o-space.com <tyrannous@o-space.com> wrote:

> Subject: please help me, my program will not work


Please put the subject of your article in the Subject of your article.


> So then how do i get it to call itself without stacking?


[snip TOFU]


I thought you said you weren't going to top-post anymore?

I give up.


*plonk*


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Wed, 26 Feb 2003 22:25:30 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: please help me, my program will not work
Message-Id: <3e5d3e39.853664@news.erols.com>

You're _still_ top-posting.

Stop it.

<tyrannous@o-space.com> wrote:

: The screen only displays for a glimps of a second and then goes blank (over
: and over again)
: 
: What is wrong with it now?

Buffer flushing.

Set $|=1 somewhere in your program.



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

Date: Wed, 26 Feb 2003 16:45:30 -0600
From: Jeff D Gleixner <glex_nospam@qwest.net>
Subject: Re: please help me, my program will not work
Message-Id: <Zpb7a.89$Iq.39755@news.uswest.net>


Reordered posts and removed the re-posted code.. sheesh..

 >> sub cycle
 >> {
 >>
 >>    &drawscreen;
 >>    sleep 1;
 >>    &cycle;
 >>
 >> }
 >>
 >>
 >>You are aware, that you are getting recursive call's without a break 
condition, are you?
 >>cycle() will call itself, will call itself, ... until you run out off stack 
space.
 >>CU Klaus


 > So then how do i get it to call itself without stacking?

First, please edit your post to the relevant parts.

Your code could use a lot of help (use strict and use warnings for starters), 
however
provided it does what you want, remove cycle() toss a while(1) in drawscreen and
call it.

 .
 . your code..
 .
@row25=();
drawscreen();
sub drawscreen
  {
	while(1)
	{
		..all your code currently in drawscreen..

		sleep 1;
	}
}

also, look at using elsif() instead of if.. if.. if..



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

Date: Wed, 26 Feb 2003 22:46:41 -0000
From: "Spero" <spero126NOSPAM@yahoo.com>
Subject: Quickie: Contents of Array into 1 Varaible
Message-Id: <b3jg1g$jki$1@wisteria.csv.warwick.ac.uk>

Hi,
    I want to put all the contents of an array (which will be between 1 and
4 strings) into a variable separated by white space.

    For example, @array_of_colors contains:

red
green
blue

    And I would like to store this in $colors, so that $colors becomes:
"red green blue" (without quotes)

    How is this possible? I really could not work it out or hunt down the
answer.

Thank-you for your time,

Spero






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

Date: Wed, 26 Feb 2003 22:57:14 -0000
From: "Spero" <spero126NOSPAM@yahoo.com>
Subject: Re: Quickie: Contents of Array into 1 Varaible
Message-Id: <b3jgl9$juo$1@wisteria.csv.warwick.ac.uk>

By chance I found it. I use the join function :)

Really starting to love perl.

Spero

"Spero" <spero126NOSPAM@yahoo.com> wrote in message
news:b3jg1g$jki$1@wisteria.csv.warwick.ac.uk...
> Hi,
>     I want to put all the contents of an array (which will be between 1
and
> 4 strings) into a variable separated by white space.
>
>     For example, @array_of_colors contains:
>
> red
> green
> blue
>
>     And I would like to store this in $colors, so that $colors becomes:
> "red green blue" (without quotes)
>
>     How is this possible? I really could not work it out or hunt down the
> answer.
>
> Thank-you for your time,
>
> Spero
>
>
>
>




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

Date: Wed, 26 Feb 2003 23:55:16 +0100
From: =?ISO-8859-15?Q?Johannes_F=FCrnkranz?= <johannes.fuernkranz@t-online.de>
Subject: Re: Quickie: Contents of Array into 1 Varaible
Message-Id: <b3jgkv$6vo$07$1@news.t-online.com>

Spero wrote:
> Hi,
>     I want to put all the contents of an array (which will be between 1 and
> 4 strings) into a variable separated by white space.
> 
>     For example, @array_of_colors contains:
> 
> red
> green
> blue
> 
>     And I would like to store this in $colors, so that $colors becomes:
> "red green blue" (without quotes)

$color = join ' ',@array_of_colors;

for spaces in particular:

$color = "@array_of_colors";

						Juffi




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

Date: Wed, 26 Feb 2003 21:27:42 +0100
From: Klaus Eltermann <klaus@eltermann.com>
Subject: Re: Reading Multiple Input Files with <>
Message-Id: <b3j8ib$ken$1@news.online.de>

Hi,

What do you *really* want to to?

1.)	while( ($inl=<IN1>) && ($inl=<IN2>) ) {
		....
	}

2.)	while( ($inl=<IN1>) || ($inl=<IN2>) ) {
		....
	}

3.)	or do you want to do really simultaneous reading without relation 
from one input to the other? In this case you should read about fork().

CU Klaus

Jodyman wrote:
> How can I go through 2 files simultaneously using while?
> Is there a trick to doing this?
> 
> For example:
> 
> while ($in1=<IN1>) {
>         while (<IN2>) {
>               do something with $_
>               do something with $in1
>          }
> }
> 
> With the above the whole <IN2> is read and acted upon
> before the next <IN1> is accessed.
> 
> Any help would be appreciated.  I have a mental block here.
> 
> Jody



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

Date: Wed, 26 Feb 2003 13:42:44 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: regexp - Alternative to $&
Message-Id: <slrnb5q65k.5q9.tadmc@magna.augustmail.com>

A. Sinan Unur <asu1@c-o-r-n-e-l-l.edu> wrote:

> I am running my CGI script with the -T option.

> if($location =~ /^\w+(\/\w+)*/) {
> 	$location = $&;

> Now, I have seen the performance warning in perlre regarding $&. I am 
> wondering if there is an alternative to using $& in this context.


   if ( $location =~ m#^(\w+(/\w+)*)# ) {
      $location = $1;


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: 26 Feb 2003 19:49:36 GMT
From: "A. Sinan Unur" <asu1@c-o-r-n-e-l-l.edu>
Subject: Re: regexp - Alternative to $&
Message-Id: <Xns932E96D397381asu1cornelledu@132.236.56.8>

tadmc@augustmail.com (Tad McClellan) wrote in 
news:slrnb5q65k.5q9.tadmc@magna.augustmail.com:

> A. Sinan Unur <asu1@c-o-r-n-e-l-l.edu> wrote:
> 
>> I am running my CGI script with the -T option.
> 
>> if($location =~ /^\w+(\/\w+)*/) {
>>      $location = $&;
> 
>> Now, I have seen the performance warning in perlre regarding $&. I am 
>> wondering if there is an alternative to using $& in this context.
> 
> 
>    if ( $location =~ m#^(\w+(/\w+)*)# ) {
>       $location = $1;

A-ha! It makes so much sense now that I see it. Thanks.

Sinan.

-- 
A. Sinan Unur
asu1@c-o-r-n-e-l-l.edu
Remove dashes for address
Spam bait: mailto:uce@ftc.gov


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

Date: Wed, 26 Feb 2003 16:02:44 -0600
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: regexp producing uninitialized var errors.
Message-Id: <Xns932EAD5388CFEsdn.comcast@216.166.71.239>

-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1

"Morgan LaVigne" <morgan_don't_spam_me_you_bastards@classroominc.org>
wrote in news:1dydnURjYcnPbMGjXTWc3g@giganews.com: 

> I have an array, it's populated, and if I do this:
> $ref=0;
> foreach(@lines)
> {
>         if(m/Comment/)
>         {
>                 delete $lines[$ref];
>         }
>         $ref++;
> }
> I get :
> Use of uninitialized value in pattern match (m//) at (eval 1) line
> 842, <TXT> line 851.
> For every single line. (TXT is the input filehandle I used to populate
> the array.)
> Any thoughts?

Is this your actual code, copied & pasted here, or is this a retyping of 
the code?  Because there's no eval in what you wrote, and no obvious reason 
why the RE you posted would have an undefined value in it.  And which is 
line 842?

- -- 
Eric
print scalar reverse sort qw p ekca lre reh 
ts uJ p, $/.r, map $_.$", qw e p h tona e;

-----BEGIN xxx SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBPl05ZWPeouIeTNHoEQJOdQCfQsIDPHX21yTn7HDsdwhS0MMELDkAn0Rq
DnfXyjsxy6XznjAyEvP95u6L
=Rh7I
-----END PGP SIGNATURE-----


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

Date: Wed, 26 Feb 2003 13:25:19 -0600
From: Bing Du Test <bing-du@tamu.edu>
Subject: remove unwanted characters
Message-Id: <3E5D149F.99A9B677@tamu.edu>

Greetings,

We need to use Perl to process files transferred from Windows machines
to UNIX.  Sometimes, ^M is appended at the end of each line when a PC
file arrives on Unix.
Just wondering if there is anyway to remove all the ^M's?

Thanks in advance for any help.

Bing



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

Date: Wed, 26 Feb 2003 19:42:07 GMT
From: Alan Gutierrez <ajglist@izzy.net>
Subject: Re: remove unwanted characters
Message-Id: <slrnb5q78p.g74.ajglist@izzy.net>

In article <3E5D149F.99A9B677@tamu.edu>, Bing Du Test wrote:

> We need to use Perl to process files transferred from Windows machines
> to UNIX.  Sometimes, ^M is appended at the end of each line when a PC
> file arrives on Unix.

> Just wondering if there is anyway to remove all the ^M's?

Remove carrage returns:

s/\r//;

If the files are dos files you could set the input line separator:

$/ = '\r\n';

-- 
Alan Gutierrez - ajglist@izzy.net
http://khtml-win32.sourceforge.net/ - KHTML on Windows


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

Date: Wed, 26 Feb 2003 19:45:48 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: remove unwanted characters
Message-Id: <x77kbmhllh.fsf@mail.sysarch.com>

>>>>> "AG" == Alan Gutierrez <ajglist@izzy.net> writes:

  AG> In article <3E5D149F.99A9B677@tamu.edu>, Bing Du Test wrote:
  >> We need to use Perl to process files transferred from Windows machines
  >> to UNIX.  Sometimes, ^M is appended at the end of each line when a PC
  >> file arrives on Unix.

  >> Just wondering if there is anyway to remove all the ^M's?

  AG> Remove carrage returns:

  AG> s/\r//;

that removes only one \r.

  AG> If the files are dos files you could set the input line separator:

  AG> $/ = '\r\n';

that won't work as you used single quotes.

uri

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


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

Date: Wed, 26 Feb 2003 13:44:10 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: remove unwanted characters
Message-Id: <slrnb5q68a.5q9.tadmc@magna.augustmail.com>

Bing Du Test <bing-du@tamu.edu> wrote:

> We need to use Perl to process files transferred from Windows machines
> to UNIX.  Sometimes, ^M is appended at the end of each line when a PC
> file arrives on Unix.


If you use the correct FTP mode (ASCII), they won't be there...


> Just wondering if there is anyway to remove all the ^M's?


   tr/\r//d;


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Wed, 26 Feb 2003 17:41:29 -0500
From: Eric Osman <ericosman-nospam@rcn.com>
Subject: scan text to find hyperlinks ?
Message-Id: <3E5D4299.6070305@rcn.com>


You've probably noticed how if text contains something looking like
a hyperlink, alot of applications will display a HOT SPOT on the
link, even though the text doesn't explicitly contain an html ANCHOR
tag around the hyperlink.



Well, suppose I want my perl script to scan some text lines, FINDING
such hyperlink-looking things, so that I can make them a hot spot
(presumably by putting an html ANCHOR tag around them) .

In other words, I want my perl script to identify things that look
like hyperlinks in text.

For finding the hyperlink-looking things in the text, which of these
would you suggest:

o	Write it myself using some sort of regular expression
	concoction.

o	Use the such-and-such well-known existing perl package to
	do it (which one ????)

Thanks.  /Eric



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

Date: 26 Feb 2003 19:35:18 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Simple question about Perl Regular Expressions
Message-Id: <b3j4tm$8nd$2@mamenchi.zrz.TU-Berlin.DE>

Hermeto Mendes  <hermeto@hotmail.com> wrote in comp.lang.perl.misc:
> I do not know how to get one simple Perl Regular Expression to work:
> 
> How would one write:
> 
> firstname lastname
> lastname, firstname
> 
> so both variants are recognised, but I have to write the name only once?
> 
> I tried: 
> 
> 'firstname lastname|lastname\, firstname'
> 
> but possibly this can be achieved in shorter writing?

Not on the regex level, as far as I can see.

But interpolation can help with systematic regex construction, for
instance if you have to generate regexes for many names.  If you have
the first and last names in the obviously-named variables, you get a
regex for both forms through

    my $regex = qr/$first $last|$last, $first/;

This could be run in a loop where $first and $last are set each time
through.

Anno


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

Date: Wed, 26 Feb 2003 21:45:01 GMT
From: Eric McDaniel <ericm@vertical........com>
Subject: Re: Simple question about Perl Regular Expressions
Message-Id: <Xns932E8BF2C91Cooheehoohahah@207.225.159.6>

anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote in news:b3j4tm$8nd$2
@mamenchi.zrz.TU-Berlin.DE:

> Hermeto Mendes  <hermeto@hotmail.com> wrote in comp.lang.perl.misc:
>> I do not know how to get one simple Perl Regular Expression to work:
>> 
>> How would one write:
>> 
>> firstname lastname
>> lastname, firstname
>> 
>> so both variants are recognised, but I have to write the name only 
once?
>> 
>> I tried: 
>> 
>> 'firstname lastname|lastname\, firstname'
>> 
>> but possibly this can be achieved in shorter writing?
> 
> Not on the regex level, as far as I can see.
> 
> But interpolation can help with systematic regex construction, for
> instance if you have to generate regexes for many names.  If you have
> the first and last names in the obviously-named variables, you get a
> regex for both forms through
> 
>     my $regex = qr/$first $last|$last, $first/;
> 
> This could be run in a loop where $first and $last are set each time
> through.
> 
> Anno
> 

Also, be careful when using the alternation metacharacter when matching 
strings in a regex. For example, 

    my $first = 'J';
    my $last = 'Ro',
    my $regex = qr/$first $last|$last, $first/;

    print 'J Ro' =~ $regex;    # prints 1, as expected
    print 'JRRo, J' =~ $regex; # prints 1, probably *not* as expected

What you probably want is to "anchor" the strings with like so:

    my $regex = qr/^$first $last$|^$last, $first$/;


-Eric


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

Date: Wed, 26 Feb 2003 23:41:17 +0100
From: =?ISO-8859-1?Q?Johannes_F=FCrnkranz?= <johannes.fuernkranz@t-online.de>
Subject: splitting
Message-Id: <b3jfqo$dbe$02$1@news.t-online.com>

Hi,

This one seems to be trivial, but I can't figure it out:

I want to split a line on commas, but only if they are not preceded by a 
backslash.

mysplit 'a,b,c' => qw(a b c)

but

mysplit 'a,b\,c' => qw(a b\,c)

What's the best way of doing this?

I can think of things like using split ',' and then joining consecutive 
values if the first one ends with '\', but that isn't very appealing.

thx, Juffi



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

Date: Wed, 26 Feb 2003 22:52:33 GMT
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: splitting
Message-Id: <slrnb5qh9h.ufd.mgjv@verbruggen.comdyn.com.au>

On Wed, 26 Feb 2003 23:41:17 +0100,
	Johannes Fürnkranz <johannes.fuernkranz@t-online.de> wrote:
> 
> I want to split a line on commas, but only if they are not preceded by a 
> backslash.

my @fields = split /(?<!\\),/, $line;

See the perlre documentation for the zero-width negative look-behind
assertion.

Martien
-- 
                        | 
Martien Verbruggen      | Useful Statistic: 75% of the people make up
Trading Post Australia  | 3/4 of the population.
                        | 


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

Date: 26 Feb 2003 18:00:01 -0500
From: Ryan Shondell <shondell@cis.ohio-state.edu>
Subject: Re: splitting
Message-Id: <xcw3cmaps0e.fsf@psi.cis.ohio-state.edu>

Johannes Fürnkranz  <johannes.fuernkranz@t-online.de> writes:

> Hi,
> 
> This one seems to be trivial, but I can't figure it out:
> 
> I want to split a line on commas, but only if they are not preceded by
> a backslash.
> 
> 
> mysplit 'a,b,c' => qw(a b c)
> 
> but
> 
> mysplit 'a,b\,c' => qw(a b\,c)
> 
> What's the best way of doing this?

Sounds like a job for a negative look-behind assertion.

my $x = 'a,b\,c';

print split /(?<!\\),/, $x;

The pattern /(?<!\\),/ will match any comma not preceded by a \.

Check out perldoc perlre for more info.


Ryan
-- 
perl -e '$;=q,BllpZllla_nNanfc]^h_rpF,;@;=split//,
$;;$^R.=--$=*ord for split//,$~;sub _{for(1..4){$=
=shift;$=--if$=!=4;while($=){print chr(ord($;[$%])
+shift);$%++;$=--;}print " ";}}_(split//,$^R);q;;'


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

Date: Wed, 26 Feb 2003 23:59:13 +0100
From: =?ISO-8859-1?Q?Johannes_F=FCrnkranz?= <johannes.fuernkranz@t-online.de>
Subject: Re: splitting
Message-Id: <b3jgsc$vm9$06$1@news.t-online.com>

Martien Verbruggen wrote:
> On Wed, 26 Feb 2003 23:41:17 +0100,
> 	Johannes Fürnkranz <johannes.fuernkranz@t-online.de> wrote:
> 
>>I want to split a line on commas, but only if they are not preceded by a 
>>backslash.
> 
> 
> my @fields = split /(?<!\\),/, $line;
> 
> See the perlre documentation for the zero-width negative look-behind
> assertion.

Apologies, I should have checked. I was absolutely certain that there is 
only look-ahead...

Sorry and thanks,
					Juffi



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

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.  

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


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