I want to put my strings starting from AA to \ in to an array and want to save it. There are about 2000-3000 strings in a text file starting from the same initials, i.e., AA
to /
. I'm doing it by this way. Please correct me if I'm wrong.
AA c0001
BB afsfjgfjgjgjflffbg
CC table
DD hhhfsegsksgk
EB jksgksjs
\
AA e0002
BB rejwkghewhgsejkhrj
CC chair
DD egrhjrhojohkhkhrkfs
VB rkgjehkrkhkh;r
\
$flag = 0
while ($line = <ifh>)
{
if ( $line = m//\/g)
{
$flag = 1;
}
while ( $flag != 0)
{
for ($i = 0; $i <= 10000; $i++)
{ # Missing brace added by editor
$array[$i] = $line;
} # Missing brace added by editor
}
} # Missing close brace added by editor; position guessed!
print $ofh, $line;
close $ofh;
Here's a way to read your data into an array. As I said in a comment, "saving" this data to a file is pointless, unless you change it. Because if I were to print the @data
array below to a file, it would look exactly like the input file.
So, you need to tell us what it is you want to accomplish before we can give you an answer about how to do it.
This script follows these rules (exactly):
$line
$line
\
, stop concatenating
lines and save $line
into @data
.These matching regexes are pretty loose, as they will match AAARGH
and \bonkers
as well. If you need them stricter, you can try /^\\$/
and /^AA$/
, but then you need to watch out for whitespace at the beginning and end of line. So perhaps /^\s*\\\s*$/
and /^\s*AA\s*$/
instead.
The code:
use warnings;
use strict;
my $line="";
my @data;
while (<DATA>) {
if (/^AA/) {
$line = $_;
while (<DATA>) {
$line .= $_;
last if /^\\/;
}
}
push @data, $line;
}
use Data::Dumper;
print Dumper \@data;
__DATA__
AA c0001
BB afsfjgfjgjgjflffbg
CC table
DD hhhfsegsksgk
EB jksgksjs
\
AA e0002
BB rejwkghewhgsejkhrj
CC chair
DD egrhjrhojohkhkhrkfs
VB rkgjehkrkhkh;r
\