Hi all;

I have to control wheter a string is an array element or not.
I tried to exists function but it didn't give me the desired output.

print "Which hotel do you want to remove from the list?\n";
        chomp($a = <STDIN>);
        
       if(exists $name {$a}){

       foreach $record(@name){

       if($record eq $a) 
       {
                delete $name[$j];
                delete $rayt[$j];
                delete $comf[$j];
                delete $kitchen[$j];
                delete $act[$j];
       }

       $j++;
       
       }
       
       }
       
       else{
                print "\nThe record couldn't find...\n";
        }

When I try to remove a hotel name (which is already exist) from the array, it prints "The record couldn't find..."

Thanks for your helps....

Recommended Answers

All 3 Replies

Your code is hard to understand. You appear to be checking for the existence of a hash key:

if(exists $name {$a}){

even though the syntax is a little messed up (a space between $name and {$a}) it probably still works.

but then you are using aray syntax when you try to delete later on, such as:

delete $name[$j];

square brackets [] are array indexing, curly brackets {} are hash keys. So I can't tell what it is you are actually trying to do by looking at your code. Are you removing something from an array or a hash?

Sorry for the late reply...

As regard as my question, I 've not any problem about deleting something in an array or a hash.

I just wonder how I can use exists function in an array??? (not in a hash.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.