[30690] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1935 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Oct 20 14:09:46 2008

Date: Mon, 20 Oct 2008 11:09:11 -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           Mon, 20 Oct 2008     Volume: 11 Number: 1935

Today's topics:
    Re: "Escape" in perl sln@netherlands.com
    Re: Edit file in place using perl script xhoster@gmail.com
        fork() & pipe() <bgydevel@gmail.com>
    Re: fork() & pipe() xhoster@gmail.com
    Re: Need help on AoH or array or any other think that m cyrusgreats@gmail.com
    Re: Need help on AoH or array or any other think that m <glex_no-spam@qwest-spam-no.invalid>
    Re: Need help on AoH or array or any other think that m <jurgenex@hotmail.com>
    Re: Need help on AoH or array or any other think that m <tim@burlyhost.com>
    Re: PDL::Audio not working for me <kieranocall@gmail.com>
    Re: Procedural interface to mysql <cwilbur@chromatico.net>
    Re: Scalar variable in void context before a loop <tadmc@seesig.invalid>
    Re: Scalar variable in void context before a loop <jurgenex@hotmail.com>
    Re: Scalar variable in void context before a loop <john@castleamber.com>
    Re: Scalar variable in void context before a loop <tim@burlyhost.com>
    Re: split a multiple lines text <someone@example.com>
    Re: split a multiple lines text <rvtol+news@isolution.nl>
    Re: split a multiple lines text <jurgenex@hotmail.com>
    Re: split a multiple lines text <bfzhao@gmail.com>
    Re: split a multiple lines text sln@netherlands.com
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Mon, 20 Oct 2008 18:03:15 GMT
From: sln@netherlands.com
Subject: Re: "Escape" in perl
Message-Id: <2mhpf49crsg8ch8bukhup6h3ufg109p5so@4ax.com>

On Mon, 20 Oct 2008 11:10:30 +0200, "Dr.Ruud" <rvtol+news@isolution.nl> wrote:

>sln@netherlands.com schreef:
>
>>     '%'.uc sprintf("%x", ord($1))
>
>
>After some cleaning it would look more like this: 
>
>      sprintf "%%%X", ord $1 

s/($htmlchrs)/sprintf "%%%02X", ord $1/ge;

incase escaping 0-15:   s/%(..)/pack("c",hex($1))/ge;




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

Date: 20 Oct 2008 14:29:23 GMT
From: xhoster@gmail.com
Subject: Re: Edit file in place using perl script
Message-Id: <20081020102948.363$lt@newsreader.com>

khan <mushtaqk921@gmail.com> wrote:
>
> Hi,
> I want change the file without using the temporary file.

I want a million bucks, peace on Earth, and the ability to fly.  That is
unlikely to work out as I want, either.

You could try Tie::File.  It will be slow on big files, as your changes are
not length-preserving.

Why not use the temp file?  Use the thing that works.

And see perldoc -f seek, where it discusses switching between reading and
writing.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.


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

Date: Mon, 20 Oct 2008 15:47:12 +0200
From: bgy <bgydevel@gmail.com>
Subject: fork() & pipe()
Message-Id: <48fc8bdb$0$29140$426a34cc@news.free.fr>

Hi,

I'm trying to understand fork() & pipe() mechanism.

Concerning fork() i understood but pipe() still presents some mysteries 
for me.

I explain, i would like to create 5 childrens (5 + 1 parent).
To understand i try to send each own pid to the parent.

Here is how i do this :

 > #!/usr/bin/perl
 >
 > use strict;
 > use warnings;
 > use IO::Handle;
 > use constant {
 >     MAX_PROCESS   => 2
 > };
 >
 > my ($child, $pid, @childs, @process_list, $child_pid);
 >
 > pipe(FROM_CHILD, TO_PARENT);
 >
 > for (1...MAX_PROCESS) {
 >     $pid = fork();
 >     if ($pid) { # Parent code
 >         push(@childs, $pid);
 > 	  close TO_PARENT;
 >         $child_pid = <FROM_CHILD>;
 >         close FROM_CHILD;
 >         print "My child's pid : $child_pid \n";
 >         push(@process_list, $child_pid);
 >     }
 >     else {  #child
 >         close FROM_CHILD;
 >         print TO_PARENT $$;
 >         close TO_PARENT;
 >         print "New process launched (",$_,"/",MAX_PROCESS,"): [$$]\n";
 >         exit(0);
 >     }
 > }
 >
 > print "[$$] -> @process_list \n";
 >
 > foreach (@childs) {
 >     waitpid($_,0);
 > }


Here is what i got :

 > maanes@void:~/workspace$ ./fork.pl
 > New process launched (1/2): [32497]
 > My child's pid : 32497
 > print() on closed filehandle TO_PARENT at ./fork.pl line 26.
 > New process launched (2/2): [32498]
 > readline() on closed filehandle FROM_CHILD at ./fork.pl line 19.
 > Use of uninitialized value in concatenation (.) or string at 
 ./fork.pl line 21.
 > My child's pid :
 > Use of uninitialized value in join or string at ./fork.pl line 33.
 > [32496] -> 32497


So for the first child it works properly.
But i have to admit that i don't understand this error.
So if someone could help me to fix this...

Thanks.


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

Date: 20 Oct 2008 14:52:06 GMT
From: xhoster@gmail.com
Subject: Re: fork() & pipe()
Message-Id: <20081020105231.250$eI@newsreader.com>

bgy <bgydevel@gmail.com> wrote:
>
>  >
>  > pipe(FROM_CHILD, TO_PARENT);
>  >
>  > for (1...MAX_PROCESS) {
>  >     $pid = fork();
>  >     if ($pid) { # Parent code
>  >         push(@childs, $pid);
>  >        close TO_PARENT;

You close the pipe the first time through the loop, so it is no longer
open the 2nd time through.

If you want all children to talk on the same pipe, then don't close it
in the loop.  If you want different pipes for each child, then create the
pipe inside the loop, not before it.

 ...
>  >         print TO_PARENT $$;

print TO_PARENT "$$\n";

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.


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

Date: Mon, 20 Oct 2008 08:24:52 -0700 (PDT)
From: cyrusgreats@gmail.com
Subject: Re: Need help on AoH or array or any other think that might help!
Message-Id: <6d6417a0-dde7-4a5b-af7f-8f7de806a878@r36g2000prf.googlegroups.com>

On Oct 18, 6:07=A0am, J=FCrgen Exner <jurge...@hotmail.com> wrote:
> "John W. Krahn" <some...@example.com> wrote:
>
> >J=FCrgen Exner wrote:
> >> perldoc -f split:
> >> =A0 =A0 =A0 =A0($customer, $category, undef, $price) =3D split ('|');
>
> >The '|' character is special in a regular expresion. =A0If you want to
> >match a literal '|' character you have to escape it:
>
> Ooops, you are right.
>
> jue

Thanks all I got it to work..


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

Date: Mon, 20 Oct 2008 10:51:15 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: Need help on AoH or array or any other think that might help!
Message-Id: <48fca8f3$0$89396$815e3792@news.qwest.net>

Tim Greer wrote:
> cyrusgreats@gmail.com wrote:
> 
>> On Oct 17, 2:59 pm, "J. Gleixner" <glex_no-s...@qwest-spam-no.invalid>
>> wrote:
>>> cyrusgre...@gmail.com wrote:
>>>> Hello good people out there. I need to write a perl script that
>>>> will parse the log and generate the following output:
>>> What have you done so far and what questions do you have?
>>>
>>>> Any help will be appreciated...million thanks in advance
>>> Put in some sort of effort by at least reading some
>>> documentation, or maybe a book, and posting your code.
>>>
>>> perldoc -f split
>>> perldoc -f open
>>> perldoc perldsc
>> here portion of the code:
>>
>> my %inventory;
>> open (FILE, $file) || die ("Could not open file. $!");
>> foreach my $el (<FILE)>) {
> 
> ^^^ You should always copy and paste your actual code you're using.  The
> above is broken:  (<FILE)>)

Define 'broken'.  It's not good code (OP use 'while' instead
of foreach), but it will run.

> 
>>   ($name, $category, $item, $price) = split ("|", $el);
> 
> I don't think that's going to do what you want.  Did you print the
> output to ensure it's going to work how you want, before you save the
> values into a hash.  I'd start there, fixing those issues, before you
> try and move onto saving the hash keys/values and processing against
> it.
> 


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

Date: Mon, 20 Oct 2008 09:17:44 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Need help on AoH or array or any other think that might help!
Message-Id: <7mbpf4h4hhq7ctvctfv97kuj37n3vb74du@4ax.com>

"J. Gleixner" <glex_no-spam@qwest-spam-no.invalid> wrote:
>Tim Greer wrote:

>>> foreach my $el (<FILE)>) {
>> 
>> ^^^ You should always copy and paste your actual code you're using.  The
>> above is broken:  (<FILE)>)
>
>Define 'broken'.  It's not good code (OP use 'while' instead
>of foreach), but it will run.

It doesn't even parse (Tim pointed it out verbatim). I guess that
qualifies as broken by any sensible definition of broken.

jue


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

Date: Mon, 20 Oct 2008 10:37:06 -0700
From: Tim Greer <tim@burlyhost.com>
Subject: Re: Need help on AoH or array or any other think that might help!
Message-Id: <7l3Lk.6089$ys6.335@newsfe02.iad>

J. Gleixner wrote:

> Tim Greer wrote:
>> cyrusgreats@gmail.com wrote:
>> 
>>> On Oct 17, 2:59 pm, "J. Gleixner"
>>> <glex_no-s...@qwest-spam-no.invalid> wrote:
>>>> cyrusgre...@gmail.com wrote:
>>>>> Hello good people out there. I need to write a perl script that
>>>>> will parse the log and generate the following output:
>>>> What have you done so far and what questions do you have?
>>>>
>>>>> Any help will be appreciated...million thanks in advance
>>>> Put in some sort of effort by at least reading some
>>>> documentation, or maybe a book, and posting your code.
>>>>
>>>> perldoc -f split
>>>> perldoc -f open
>>>> perldoc perldsc
>>> here portion of the code:
>>>
>>> my %inventory;
>>> open (FILE, $file) || die ("Could not open file. $!");
>>> foreach my $el (<FILE)>) {
>> 
>> ^^^ You should always copy and paste your actual code you're using. 
>> The
>> above is broken:  (<FILE)>)
> 
> Define 'broken'.  It's not good code (OP use 'while' instead
> of foreach), but it will run.

I am aware that a for/foreach and while can work equally well.  Invalid
syntax, as shown above -> (<FILE)>)  is what I define as "broken". 
Better?
-- 
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!


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

Date: Mon, 20 Oct 2008 06:35:34 -0700 (PDT)
From: kieran <kieranocall@gmail.com>
Subject: Re: PDL::Audio not working for me
Message-Id: <0c4418d9-1c58-4888-b223-63dba5e76b79@s9g2000prg.googlegroups.com>

On Oct 17, 8:01 pm, Joost Diepenmaat <jo...@zeekat.nl> wrote:
> kieran<kieranoc...@gmail.com> writes:
> > Hi all,
> > I am relatively new toperland have just begun using the PDL::Audio
> > module. I am having trouble getting basic functionality working, for
> > example "describe_audio" should return the number of samples in an
> > audio file. I have tried using the following code to find the number
> > of sample in a wav file.
>
> > use PDL;
> > use PDL::Audio;
>
> > $pdl = raudio "hello1.wav";
> > print "\n\n", describe_audio pdl, "\n";
>
> > The output i get for any wav file is as follows:  "mono sound with
> > samples"
> > where there should be either mono or stereo (I always get mono, even
> > for stereo files)
> > Also there should be a number of samples such as "mono file with
> > 1234567 samples", but it is always blank.
> > Any suggestions?
>
> I don't use PDL::Audio, if all you want is to check out the meta-data on
> an audio file, and/or read the sample data, there are more modules out
> there if PDL::Audio doesn't work for you.
>
> I would suggest Audio::SndFile it should work for both data and
> meta-data, but it needs libsndfile and I'm biased since I wrote it :-)
>
> --
> Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/

Hi Joost, thank for the reply, I had planned to use PDL::Audio ofr
filtering, FFT, cut_leading _silence, etc...
I am having problems with any of the functions i use from that module.
It would be great to see some examples of this code working as there
is a serious lack of documentation for this module. If anyone has used
this module successfully any sample code woul be a great help.
Best regards,
Kieran


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

Date: Mon, 20 Oct 2008 10:31:33 -0400
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: Procedural interface to mysql
Message-Id: <86ljwjqqwq.fsf@mithril.chromatico.net>

>>>>> "LE" == Lars Eighner <usenet@larseighner.com> writes:

    LE> <slrngfm69g.ksq.hjp-usenet2@hrunkner.hjp.at>, the lovely and
    LE> talented Peter J. Holzer broadcast on comp.lang.perl.misc:

    >> What would a "procedural interface" give you that DBI doesn't?

    LE> No objects.  So it would be faster, simpler, more intuitive,
    LE> more easily made to do what I want to do instead of what someone
    LE> thinks I ought to want to do.

"Faster" and "simpler" are highly unlikely to both be accomplished at
once.  It's a function call either way; you have the overhead of
additional method resolution, but you are spared the morass of PHP-style
mysql_ functions.

"More intuitive" and "more easily made to do what I want to do..." are a
result of your viewpoint rather than anything inherent in object
orientation.  

Frankly, you could fairly trivially put a wrapper around DBI calls so that

    $sth->prepare (....) 

turned into 
  
    dbi_prepare ($sth, ....) 

but you'd gain nothing except avoiding the visible use of objects, and
the code behind the wrapper would be exactly the same.  Would this be
faster, simpler, more intuitive, and easier for you to work with, simply
because you don't *see* that there are objects involved?

If so, the problem is with you, not with objects.

Charlton
   
   

-- 
Charlton Wilbur
cwilbur@chromatico.net


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

Date: Mon, 20 Oct 2008 06:15:13 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Scalar variable in void context before a loop
Message-Id: <slrngfoq21.tr0.tadmc@tadmc30.sbcglobal.net>

Mark Hobley <markhobley@hotpop.donottypethisbit.com> wrote:
> Tim Greer <tim@burlyhost.com> wrote:
>
>> Are you sure it wasn't $| before the loop?
>
> What is that?


Perl's special variables are described in

    perldoc perlvar


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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

Date: Mon, 20 Oct 2008 06:03:56 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Scalar variable in void context before a loop
Message-Id: <q50pf4dm7loait97gfgmrcqon1eua1io7k@4ax.com>

markhobley@hotpop.donottypethisbit.com (Mark Hobley) wrote:
>In my professional perl programming guide, some of the examples put the 
>variable to be used as an iterator in void context before the loop. For 
>example:
>
>$l;
>for ($l = 0; $l < 10; $l++) {
>  print $l;
>}
>
>I am curious as to what reasons there are for doing this, because there 
>does not appear to be any mention of it anywhere within the book.

Of ocurse I don't know what the author was thinking. But _I_ would write
this as
	for my $| (0..9) {
		print $|;
	}

Another question is why he would possibly want to assign 0 to 9 to the
autoflush variable. It's a binary variable, so the last 8 assignments
don't have any effect.

jue


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

Date: 20 Oct 2008 16:34:58 GMT
From: John Bokma <john@castleamber.com>
Subject: Re: Scalar variable in void context before a loop
Message-Id: <Xns9B3D75D34E4B5castleamber@130.133.1.4>

markhobley@hotpop.donottypethisbit.com (Mark Hobley) wrote:

> In my professional perl programming guide,

Which guide is that?

-- 
John    http://johnbokma.com/ - Hacking & Hiking in Mexico

Perl help in exchange for a gift:
http://johnbokma.com/perl/help-in-exchange-for-a-gift.html


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

Date: Mon, 20 Oct 2008 10:41:50 -0700
From: Tim Greer <tim@burlyhost.com>
Subject: Re: Scalar variable in void context before a loop
Message-Id: <zp3Lk.9266$TX6.5991@newsfe06.iad>

Mark Hobley wrote:

> Tim Greer <tim@burlyhost.com> wrote:
> 
>> Are you sure it wasn't $| before the loop?
> 
> What is that?
> 
>>  Or, perhaps it was a "my $l"?
> 
> Now that would have made more sense. This occurs a couple of times
> throughout the book. I bet it is a misprint.
> 
>>  Can you type the entire code here up to that point (or the
>> relevant portions anyway) -- the actual code, rather than an example,
> 
> Unfortunately, the entire code is just the example.
> 
> Mark.
> 

Sorry, I'm not familiar with the book, I just wanted to be sure that was
the full code from their example, as typed (a $l and $| might look very
similiar in print form, for example, and one could make more sense than
the other).  As for $| and it's meaning, see: perldoc -q buffer  Still,
it would be unlikely someone would have $|; just randomly there.  It
does sound like a typo.  If this is all over the book's examples, I'd
have to wonder if this is a good book to follow.
-- 
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!


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

Date: Mon, 20 Oct 2008 04:36:11 -0700
From: "John W. Krahn" <someone@example.com>
Subject: Re: split a multiple lines text
Message-Id: <G2_Kk.3854$B72.3094@newsfe19.iad>

bingfeng wrote:
> 
> Assume I have following string:
> my $cmds = <<DOC
>   __begin {
>      abc;
>      def;
>      {foo;bar}
>   } __end;
>   __begin {
>      cde;
>   } __end;
>   abc;
>   bad;
> DOC
> ;
> 
> I want to split it into an array, the first item is "__begin {
>      abc;
>      def;
>      {foo;bar}
>   } __end", the second item  is  "__begin {
>      cde;
>   } __end", and the third is "abc" and the fourth is "bad".
> 
> split obviously cannot be used here, so I use following regex:
> my @lines = ($cmds =~ /__begin.*?__end|[^;]+/sg);
> 
> but it does not work at all. so how can I do with this?

my @lines = $cmds =~ /^\s*__begin(?s:.*?)__end;$|^\s*\S+;$/mg;



John
-- 
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall


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

Date: Mon, 20 Oct 2008 13:47:32 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: split a multiple lines text
Message-Id: <gdi26c.1so.1@news.isolution.nl>

bingfeng schreef:

> Assume I have following string:
> my $cmds = <<DOC
>   __begin {
>      abc;
>      def;
>      {foo;bar}
>   } __end;
>   __begin {
>      cde;
>   } __end;
>   abc;
>   bad;
> DOC
> ;
>
> I want to split it into an array, the first item is "__begin {
>      abc;
>      def;
>      {foo;bar}
>   } __end", the second item  is  "__begin {
>      cde;
>   } __end", and the third is "abc" and the fourth is "bad".
>
> split obviously cannot be used here, so I use following regex:
> my @lines = ($cmds =~ /__begin.*?__end|[^;]+/sg);
>
> but it does not work at all. so how can I do with this?

my @blocks;

for my $re ( qr/__begin.*?__end/s, qr/^[^;]+/m ) {
  while ($cmds =~ s/\s*($re)\s*;/" "x ($+[0] - $-[0])/es) {
    push @blocks, [ $-[0], $1 ];
  }
}

for (sort { $a->[0] <=> $b->[0] } @blocks) {
    print "<--\n", $_->[1], "\n-->\n";
}


In the end, $cmds will still have the same length, but will contain only
whitespace (and any unmatched content).

-- 
Affijn, Ruud

"Gewoon is een tijger."



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

Date: Mon, 20 Oct 2008 06:13:04 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: split a multiple lines text
Message-Id: <0h0pf4tgbnjekptae37csda62dktclkca1@4ax.com>

bingfeng <bfzhao@gmail.com> wrote:
>Hello,
>Assume I have following string:
>my $cmds = <<DOC
>  __begin {
>     abc;
>     def;
>     {foo;bar}
>  } __end;
>  __begin {
>     cde;
>  } __end;
>  abc;
>  bad;
>DOC
>;
>
>I want to split it into an array, the first item is "__begin {
>     abc;
>     def;
>     {foo;bar}
>  } __end", the second item  is  "__begin {
>     cde;
>  } __end", and the third is "abc" and the fourth is "bad".

This sounds suspiciously like an X-Y problem to me. Are you reading this
text from a file? If yes, then if a block starts with '__begin{' you can
read that block until the '__end;' token is reached.

And yes, you can use split() if you do it in two steps:
First split on '__end;' . And in the second step repair the now missing
'__end;' for those items, that have a leading '__begin{' and for the
others split again at ';'.

jue


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

Date: Mon, 20 Oct 2008 08:27:49 -0700 (PDT)
From: bingfeng <bfzhao@gmail.com>
Subject: Re: split a multiple lines text
Message-Id: <d8286ba5-c758-4827-970e-fa579da1f912@i24g2000prf.googlegroups.com>

On 10=D4=C220=C8=D5, =CF=C2=CE=E77=CA=B136=B7=D6, "John W. Krahn" <some...@=
example.com> wrote:
> bingfeng wrote:
>
> > Assume I have following string:
> > my $cmds =3D <<DOC
> >   __begin {
> >      abc;
> >      def;
> >      {foo;bar}
> >   } __end;
> >   __begin {
> >      cde;
> >   } __end;
> >   abc;
> >   bad;
> > DOC
> > ;
>
> > I want to split it into an array, the first item is "__begin {
> >      abc;
> >      def;
> >      {foo;bar}
> >   } __end", the second item  is  "__begin {
> >      cde;
> >   } __end", and the third is "abc" and the fourth is "bad".
>
> > split obviously cannot be used here, so I use following regex:
> > my @lines =3D ($cmds =3D~ /__begin.*?__end|[^;]+/sg);
>
> > but it does not work at all. so how can I do with this?
>
> my @lines =3D $cmds =3D~ /^\s*__begin(?s:.*?)__end;$|^\s*\S+;$/mg;
>
> John
> --
> Perl isn't a toolbox, but a small machine shop where you
> can special-order certain sorts of tools at low cost and
> in short order.                            -- Larry Wall- =D2=FE=B2=D8=B1=
=BB=D2=FD=D3=C3=CE=C4=D7=D6 -
>
> - =CF=D4=CA=BE=D2=FD=D3=C3=B5=C4=CE=C4=D7=D6 -

Thank you, John, it works very well. You help save some hours!


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

Date: Mon, 20 Oct 2008 17:27:30 GMT
From: sln@netherlands.com
Subject: Re: split a multiple lines text
Message-Id: <0bfpf419r2qj09ekj7g5p8ttostmavb9nc@4ax.com>

On Mon, 20 Oct 2008 02:42:03 -0700 (PDT), bingfeng <bfzhao@gmail.com> wrote:

>Hello,
>Assume I have following string:
>my $cmds = <<DOC
>  __begin {
>     abc;
>     def;
>     {foo;bar}
>  } __end;
>  __begin {
>     cde;
>  } __end;
>  abc;
>  bad;
>DOC
>;
>
>I want to split it into an array, the first item is "__begin {
>     abc;
>     def;
>     {foo;bar}
>  } __end", the second item  is  "__begin {
>     cde;
>  } __end", and the third is "abc" and the fourth is "bad".
>
>split obviously cannot be used here, so I use following regex:
>my @lines = ($cmds =~ /__begin.*?__end|[^;]+/sg);
                                         ^^
my @lines = ($cmds =~ /__begin.*?__end|[^\s;]+/sg);

You were on the right track. [^;] however is first to match all before ';',
which means it grabs the'   __begin { .. abc;' then the next, then next.
'__begin.*?__end' is never matched. By including not whitespace, [^\s;] in 
the character class, begin and end have a chance.

sln

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

use strict;
use warnings;

my $cmds = <<DOC
  __begin {
     abc;
     def;
     {foo;bar}
  } __end;
  __begin {
     cde;
  } __end;
  abc;
  bad;
DOC
;

my @lines = ($cmds =~ /__begin.*?__end|[^\s;]+/sg);

for (my $i = 0; $i < @lines; $i++) {
	print "\n\$lines[$i] = \n\n\"$lines[$i]\"\n";
}

__END__

output:

$lines[0] =

"__begin {
     abc;
     def;
     {foo;bar}
  } __end"

$lines[1] =

"__begin {
     cde;
  } __end"

$lines[2] =

"abc"

$lines[3] =

"bad"




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

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 V11 Issue 1935
***************************************


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