A
Amaninder
Hi everyone
I tried lot of things but can not able to figure out why the same code
in for loop is giving me different results.
Code 1 is giving me the correct result but Code 2 does NOT. All i am
doing in ths Code 1 is that i have
my $got = "3 500 1660 17800 19360 N";
my $exp = "<0 3> <0 500> <0 1660> 17800 19360 <0
N>";
and i am filling @btw with ('"0 3". "3", "0 500", "500", "0 N" ,
"N").
Now the code in "Code 2" is same except it is funtion and i am
receiving $got and $exp as a parameters.
Problem is in "Code 2" $got =~ s/$split[$i]//; deletes the content of
$got whereas in "Code 1" it does NOT when lenght of $split[$i] = 0 in
both cases.
####################Code 1 #######################################
#!/usr/bin/perl -w
use strict;
sub trim($)
{
my $string = shift;
if (defined($string) ){
$string =~ s/^\s+//;
$string =~ s/\s+$//;
}
return $string;
}
my @btw;
my $errMess;
my $got = "3 500 1660 17800 19360 N";
my $exp = "<0 3> <0 500> <0 1660> 17800 19360 <0
N>";
my @split = split(/<|>/, $exp);
for(my $i = 0; $i <= $#split; $i=$i+2){
if($i+1 > $#split){
last;
}
push (@btw, $split[$i + 1]);
$split[$i] = trim ($split[$i]);
$got =~ s/$split[$i]//;
if ( $i + 2 > $#split){
push (@btw, $got);
}else{
$split[$i + 2] = trim ($split[$i + 2]);
#in freq one if $split[$i + 2]= ' ' , then trim will delete
all the
#space and leave it to nothing. length = 0
#Therefore, if lenght is 0 let $split[$i + 2] = ' ', so that
$point != 0
if( length ($split[$i + 2]) == 0){
$split[$i + 2] = ' ';
}
my $point = index $got, $split[$i + 2];
if( $point == -1){
$errMess = sprintf ( "'%s' is NOT found", $split[$i + 2]);
last;
}elsif($point == 0){
$errMess = sprintf ( ">,< should NOT be together");
last;
}
push (@btw, substr $got, 0, $point, '');
$got = trim ($got);
}
}
foreach my $temp (@btw){
printf ("\n%s", $temp?$temp:'');
}
printf ("\n%s.", $errMess?$errMess:'');
#####################Code 2###################################
sub compareLines{
my $exp = shift;
my $got = shift;
#default is pass
my $passOrFail = 'pass';
my $errMess;
my @split = split(/<|>/, $exp);
my @btw;
for(my $i = 0; $i <= $#split; $i=$i+2){
if($i+1 > $#split){
last;
}
push (@btw, $split[$i + 1]);
$split[$i] = trim ($split[$i]);
$got =~ s/$split[$i]//;
if ( $i + 2 > $#split){
push (@btw, $got);
}else{
$split[$i + 2] = trim ($split[$i + 2]);
#in freq one if $split[$i + 2]= ' ' , then trim will
delete all the
#space and leave it to nothing. length = 0
#Therefore, if lenght is 0 let $split[$i + 2] = ' ', so
that $point != 0
if( length ($split[$i + 2]) == 0){
$split[$i + 2] = ' ';
}
my $point = index $got, $split[$i + 2];
if( $point == -1){
$errMess = sprintf ( "compareLines(): '%s' is NOT
found", $split[$i + 2]);
last;
}elsif($point == 0){
$errMess = "compareLines(): >,< should NOT be
together";
last;
}
push (@btw, substr $got, 0, $point, '');
$got = trim ($got);
}
}
#if there is any error return
if( defined ($errMess) && $errMess =~ /.+/){
return ($errMess, $passOrFail);
}
#send to compare elements in the array
($errMess, $passOrFail) = compareElemArray(\@btw);
if( defined ($errMess) && $errMess =~ /.+/){
# add compareLines
$errMess = "compareLines(): $errMess";
}
return ($errMess, $passOrFail);
}
Regards
Amaninder Saini
I tried lot of things but can not able to figure out why the same code
in for loop is giving me different results.
Code 1 is giving me the correct result but Code 2 does NOT. All i am
doing in ths Code 1 is that i have
my $got = "3 500 1660 17800 19360 N";
my $exp = "<0 3> <0 500> <0 1660> 17800 19360 <0
N>";
and i am filling @btw with ('"0 3". "3", "0 500", "500", "0 N" ,
"N").
Now the code in "Code 2" is same except it is funtion and i am
receiving $got and $exp as a parameters.
Problem is in "Code 2" $got =~ s/$split[$i]//; deletes the content of
$got whereas in "Code 1" it does NOT when lenght of $split[$i] = 0 in
both cases.
####################Code 1 #######################################
#!/usr/bin/perl -w
use strict;
sub trim($)
{
my $string = shift;
if (defined($string) ){
$string =~ s/^\s+//;
$string =~ s/\s+$//;
}
return $string;
}
my @btw;
my $errMess;
my $got = "3 500 1660 17800 19360 N";
my $exp = "<0 3> <0 500> <0 1660> 17800 19360 <0
N>";
my @split = split(/<|>/, $exp);
for(my $i = 0; $i <= $#split; $i=$i+2){
if($i+1 > $#split){
last;
}
push (@btw, $split[$i + 1]);
$split[$i] = trim ($split[$i]);
$got =~ s/$split[$i]//;
if ( $i + 2 > $#split){
push (@btw, $got);
}else{
$split[$i + 2] = trim ($split[$i + 2]);
#in freq one if $split[$i + 2]= ' ' , then trim will delete
all the
#space and leave it to nothing. length = 0
#Therefore, if lenght is 0 let $split[$i + 2] = ' ', so that
$point != 0
if( length ($split[$i + 2]) == 0){
$split[$i + 2] = ' ';
}
my $point = index $got, $split[$i + 2];
if( $point == -1){
$errMess = sprintf ( "'%s' is NOT found", $split[$i + 2]);
last;
}elsif($point == 0){
$errMess = sprintf ( ">,< should NOT be together");
last;
}
push (@btw, substr $got, 0, $point, '');
$got = trim ($got);
}
}
foreach my $temp (@btw){
printf ("\n%s", $temp?$temp:'');
}
printf ("\n%s.", $errMess?$errMess:'');
#####################Code 2###################################
sub compareLines{
my $exp = shift;
my $got = shift;
#default is pass
my $passOrFail = 'pass';
my $errMess;
my @split = split(/<|>/, $exp);
my @btw;
for(my $i = 0; $i <= $#split; $i=$i+2){
if($i+1 > $#split){
last;
}
push (@btw, $split[$i + 1]);
$split[$i] = trim ($split[$i]);
$got =~ s/$split[$i]//;
if ( $i + 2 > $#split){
push (@btw, $got);
}else{
$split[$i + 2] = trim ($split[$i + 2]);
#in freq one if $split[$i + 2]= ' ' , then trim will
delete all the
#space and leave it to nothing. length = 0
#Therefore, if lenght is 0 let $split[$i + 2] = ' ', so
that $point != 0
if( length ($split[$i + 2]) == 0){
$split[$i + 2] = ' ';
}
my $point = index $got, $split[$i + 2];
if( $point == -1){
$errMess = sprintf ( "compareLines(): '%s' is NOT
found", $split[$i + 2]);
last;
}elsif($point == 0){
$errMess = "compareLines(): >,< should NOT be
together";
last;
}
push (@btw, substr $got, 0, $point, '');
$got = trim ($got);
}
}
#if there is any error return
if( defined ($errMess) && $errMess =~ /.+/){
return ($errMess, $passOrFail);
}
#send to compare elements in the array
($errMess, $passOrFail) = compareElemArray(\@btw);
if( defined ($errMess) && $errMess =~ /.+/){
# add compareLines
$errMess = "compareLines(): $errMess";
}
return ($errMess, $passOrFail);
}
Regards
Amaninder Saini