Element Fetching


use open ':encoding(utf8)';
binmode STDOUT, ':utf8';


my $CurBrLoopSngl = qw/(?:[^\{\}]*)/; my $CurBrLoopMany = qw/((?:[^{}]*(?:{(?:[^{}]*(?:{[^{}]*})*[^{}]*)*})*[^{}]*)*)/;
my $SqrBrLoopSngl = qw/(?:[^\[\]]*)/; my $SqrBrLoopMany = qw/((?:[^\[\]]*(?:{(?:[^\[\]]*(?:{[^\[\]]*})*[^\[\]]*)*})*[^\[\]]*)*)/;


use strict;
use warnings;

my $texcnt = ""; my $ri = 1; readFileinString("testing.xml", \$texcnt);
#$texcnt=~s/<div/\n<enter><div/g; $texcnt=~s/<\/div/\n<enter><\/div/g;
my @alllines = split /\n/, $texcnt;
my $storelines = "";  my $cI = 1;  my $oI = 0;

for(my $i=0; $i<=$#alllines; $i++)
{
if($alllines[$i]=~m/<div/i)
{
$oI++; $alllines[$i]=~s/<div/<div$oI/i;
}
elsif($alllines[$i]=~m/<\/div/i)
{
if($cI == $oI)
{
$alllines[$i]=~s/<\/div/<\/div$cI/i; $cI = $oI;
}
elsif($cI ne $oI)
{
$alllines[$i]=~s/<\/div/"<\/div".$oI--/ei;
}
}

$storelines .= $alllines[$i]."\n";
}
print $storelines;


#---------------> File reading
sub readFileinString
#--------------->
{
my $File = shift;
my $string = shift;
open(FILE1, "<$File") or die "\nFailed Reading File $ri\: [$File]\n\tReason: $!";
read(FILE1, $$string, -s $File, 0);
close(FILE1);
$ri++;
}


#------------------------------->
sub writeFileinString
#------------------------------->
{
my $File = shift;
my $string = shift;
my @cDir = split(/\\/, $File);
my $tmp = "";
for(my $i = 0; $i < $#cDir; $i++)
{
$tmp = $tmp . "$cDir[$i]\\";
mkdir "$tmp";
}
if(-f $File){
unlink($File);
}
open(FILE, ">$File") or die "\n\nFailed File Open for Writing: [$File]\n\nReason: $!\n";
print FILE $$string;
close(FILE);
}

Comments