CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Compare list of multiline strings

 



wmalgal
New User

Nov 5, 2009, 9:12 AM

Post #1 of 4 (949 views)
Compare list of multiline strings Can't Post

Hello All,

I am writing a script that accesses a code store stored in oracle. Now I can get a handle to oracle etc no problem etc.. thats not the issue.. nope..



The issue is thus : I want to be able to compare the code that is stored in three different environments... test, preprod and production... So I query the database and this returns me an array where each element contains a block a module of code. I compare this with the corresponding module of code in another environment and I flag it if there is a difference.



I am using the following function ..



sub diffarray_code{
print "\n Finding difference in code ...";
print "\narray_one = $#array_one, array_two = $#array_two\n";
my $arrayone=0;
my $arraytwo=0;
foreach $X (@array_one){
$i = 0;
print"Checking $arrayone against:\n";
while (($i <= $#array_two && (@array_two[$i] ne $X))) {
print "Array two element$i\n";
$i++;
}

if ($i <= $#array) {
# true
print "TRUE!!!!!!\n";
}
else
{
# false
push (@difference, $X);

}
$arrayone++;
}

}




It seems to find a difference in all case when in fact I know there is only one difference if you see what I mean..



Any help with this would really be appreciated... If you see what I am mean..



wmalgal


7stud
Enthusiast

Nov 5, 2009, 4:27 PM

Post #2 of 4 (941 views)
Re: [wmalgal] Compare list of multiline strings [In reply to] Can't Post


In Reply To
Hello All,

I am writing a script that accesses a code store stored in oracle. Now I can get a handle to oracle etc no problem etc.. thats not the issue.. nope..



The issue is thus : I want to be able to compare the code that is stored in three different environments... test, preprod and production... So I query the database and this returns me an array where each element contains a block a module of code. I compare this with the corresponding module of code in another environment and I flag it if there is a difference.



I am using the following function ..



sub diffarray_code{
print "\n Finding difference in code ...";
print "\narray_one = $#array_one, array_two = $#array_two\n";
my $arrayone=0;
my $arraytwo=0;
foreach $X (@array_one){
$i = 0;
print"Checking $arrayone against:\n";
while (($i <= $#array_two && (@array_two[$i] ne $X))) {
print "Array two element$i\n";
$i++;
}

if ($i <= $#array) {
# true
print "TRUE!!!!!!\n";
}
else
{
# false
push (@difference, $X);

}
$arrayone++;
}

}




It seems to find a difference in all case when in fact I know there is only one difference if you see what I mean..



Any help with this would really be appreciated... If you see what I am mean..



wmalgal

How does this:

@array_two[$i]

not give you an error? Maybe you can use this program as an example:


Code
use strict; 
use warnings;

my @arr1 = ("a", "b", "c");
my @arr2 = ("a", "b", "c");

foreach my $a1 (@arr1) {
my $count = 0;

foreach my $a2 (@arr2) {
print "$a1 <--> $a2: ";

if($a1 ne $a2) {
print $count, "\n";
}else{
print "eq\n";
}

$count++
}

print "---------\n";
}

--output:--
a <--> a: eq
a <--> b: 1
a <--> c: 2
---------
b <--> a: 0
b <--> b: eq
b <--> c: 2
---------
c <--> a: 0
c <--> b: 1
c <--> c: eq
---------


Is that what you are seeing? Or something different?


(This post was edited by 7stud on Nov 5, 2009, 4:58 PM)


FishMonger
Veteran / Moderator

Nov 5, 2009, 4:36 PM

Post #3 of 4 (939 views)
Re: [7stud] Compare list of multiline strings [In reply to] Can't Post


In Reply To
How does this:

@array_two[$i]

not give you an error?


Because it's not an error.

That's an array slice but if not used correctly, you're get this warning (assuming warnings are enabled).

Quote
Scalar value @array_two[$i] better written as $array_two[$i]


http://perldoc.perl.org/perldata.html#Slices


(This post was edited by FishMonger on Nov 5, 2009, 4:39 PM)


7stud
Enthusiast

Nov 5, 2009, 5:49 PM

Post #4 of 4 (933 views)
Re: [FishMonger] Compare list of multiline strings [In reply to] Can't Post


In Reply To

In Reply To
How does this:

@array_two[$i]

not give you an error?


Because it's not an error.

That's an array slice but if not used correctly, you're get this warning (assuming warnings are enabled).

Quote
Scalar value @array_two[$i] better written as $array_two[$i]


http://perldoc.perl.org/perldata.html#Slices


How do you use that syntax correctly? Also, can you explain the error here:


Code
my @arr1 = ("a", "b", "c"); 
$arr1[1] = ("z", "x", "y");

--output:--
Useless use of a constant in void context at 1perl.pl line 5.

Where is the constant and where is the void context?

Wow, this really highlights the difference between a list and the array that stores the list:


Code
use strict; 
use warnings;

my @arr1 = ("a", "b", "c");
my @arr2 = ("x", "y", "z");

$arr1[1] = ("z", "x", "y");
print @arr1, "\n";

$arr1[1] = @arr2;
print @arr1, "\n";

--output:--
Useless use of a constant in void context at 1perl.pl line 6.
Useless use of a constant in void context at 1perl.pl line 6.
ayc
a3c

How does 'y' end up in @arr1? It almost looks like each of the elements of the list are assigned to $arr[1] in turn, leaving the last assignment $arr[1] = "y" the one that sticks.


(This post was edited by 7stud on Nov 5, 2009, 6:00 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives