Sharath
Sharath

Reputation: 61

If condition not matching inside a foreach loop in perl

I'm trying to match a string with if statement inside a foreach loop but its not matching although i get the same string with printed before if statement inside foreach loop. Please help.

use Net::Telnet;
$ip='xx.xxx.xx.xx';
$ip_port='10002';
$port  = new Net::Telnet->new( Host=>$ip,Port=>$ip_port,Dump_log=> "dump.log");

my @folder= $port->cmd("ls");
sleep(2);

$folders=@folder;
print "Number of folders are:$folders\n";

foreach my $folder(@folder)
{
        print "Folder before if is:$folder\n";
        if(($folder eq "acc") || ($folder eq "bda"))
    {

       # some code here.
    }
}

Upvotes: 0

Views: 1174

Answers (1)

vogomatix
vogomatix

Reputation: 5041

Your strings probably contain white space. You can use something like chomp to remove it, or alternatively use regexs.

Try:

if ($folder =~ /^(acc|bda)/) {
    # some code here
}

Upvotes: 1

Related Questions