Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl If then comparing arrary elements 1

Status
Not open for further replies.

jerrig1

Technical User
Nov 3, 2005
3
US
below is a sample script I wrote and in the testing section there is what I thought was a simple IF/Else statement. the output I get when I run this all fall in the "Else" but that is not my expected results. I am not new to scripting, but relativly new to perl. is my IF statement, quotes, or operators wrong?

please help!



##################################################
##################################################
$nor= "operating_normally";
$mir= "mirrorclone_synching";
@vdisks = ("disk1",disk2,disk3,disk4,disk5,disk6,disk7);
@vdstate = ($nor,$nor,$nor,$nor,$mir,$nor,$mir);

$count3 = 0;
foreach $item (@vdstate) {
if (length($item) > 19) {
$notready = 1;
# print $vdstate[$count3] .",\n";
}

#####testing######
#print "vdisk=$vdisks[$count3] state=$vdstate[$count3] \n";
if ("$vdstat[$count3]"eq"mirrorclone_synching") {
print "****** VDISK=$vdisks[$count3] STATE=$vdstate[$count3] \n";
}
else {
print "else*$vdstate[$count3]*-VS-*mirrorclone_synching*\n";
}
##### end testing #######


$count3++;
}
print " \n notready=$notready \n";
print "end \n";
######################################################
######################################################
 
Code:
if ("$vdstat[$count3]"eq"mirrorclone_synching") {

should probably be
Code:
if ("$vdstate[$count3]"eq"mirrorclone_synching") {

you don't need the quotes either around $vdstate[$count3];

You might want to think of using a hash on this data instead of trying to keep the arrays syncd.

You should also use "use strict;" that would have warned you about $vdstat being incorrect since it hadn't been established.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
thank you Travis69:

that was it... I mis typed the array name. what did you mean by using a hash to keep the arrays synced?

 
you can create a hash like

$hash{'data1'} = $nor;

or you can construct the whole thing up front like

$hash{ data1 => $nor, data2 => $nor};


Then when you want to know the value of data1 you just do print $hash{data1} and it will print out $nor.

or you can loop through every key

for my $key (keys %hash) {
print "$hash{$key}\n";
}


Hashes are a little tough for people to get their head around but once you do you'll realize how easy it is and how much time it will save you.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top