You're using capturing parentheses, which will cause the capture to be inserted into the split result (read perldoc split on this). Also, please use strict and use warnings, those would have caught a typo in $chunck1 as well as your incorrect use of the variable name $sectionname2 (where you actually mean $chunk2. Below a corrected version of your script
#!/usr/bin/perl
use strict;
use warnings;
my $chunk1;
my $chunk2;
my $chunk3;
{
local $/ = undef;
(undef,$chunk1,$chunk2,$chunk3) = split(/^!(?:sectionname1|section
+name2|sectionname3)$/m, <DATA>);
}
print $chunk2;
__DATA__
!sectionname1
this is the data
!sectionname2
this is more data
!sectionname3
end
| [reply] [d/l] [select] |
Thanks so much it works now !
| [reply] |
There is, of course, More Than One Way To Do This.
You could localize $/ to your section separator. This may cause trouble if your file is really big, as could your technique of un-defining $/.
Or your could read the file line-by-line, switching output files as required (I would probably do this; my preference is usually in the realm of Brute Force & Ignorance).
Or you could correct your various spelling errors, like $chunck1 for what (probably) should be $chunk1. Note, too, that when you split with capturing parentheses, as you did, the separators are included in the list split returns. I would probably write your split somewhat differently:
my @chunks = split(/^\!(sectionname[1-9][0-9]*)$/m, <>);
There's a couple of reasons for this. First, when I see variable names like /\$chunkΎ-9]+$/, I immediately think "this should be an array." Secondly, typing sectionname thrice, as opposed to once, adds several opportunities for typographical errors (like omitting one of the n's in sectionname), and involves typing a dozen or extra characters, and, finally, what happens when you decided to chop the file into four, five, or 137 pieces?
emc
Any New York City or Connecticut area jobs? I'm currently unemployed.
There are some enterprises in which a careful disorderliness is the true method.
—Herman Melville
| [reply] [d/l] |
First idea is that this is not the code you are running, since
print "$sectionname2\n";
$sectionname2 isn't defined anywhere, not to say it has to do with what split yields. Maybe you meant $chunk2?
Then, you spell "chunck", then "chunk". There be bugs! better use an array to split those chun(c)ks into:
@chunks = split ...
More thibgs could be said about the code you really are running... mind to post that?
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
| [reply] [d/l] [select] |
More thibgs could be said
shmem has a heavy cold :-)
Cheers, Rob
| [reply] |