How to use eq syntax in perl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • omakhileshchand
    New Member
    • May 2012
    • 13

    How to use eq syntax in perl

    Code:
    @pri_status=`asterisk -rx "dahdi show status" |cut -c40-47`;
    =cut
    the output of pri_status is
    Alarms
    OK
    OK
    OK
    RED
    OK
    OK
    OK
    RED
    =cut
    Code:
    $n=$#pri_status;
    
    for($i=1;$i<=$n;$i++)
    {
            if ("$pri_status[$i]" eq "RED")
            {
                    print "Pri is down\n";
            }
            else
            {
                    print "Pri is Up\n";
            }
    }
    The output of code is
    Pri is Up
    Pri is Up
    Pri is Up
    Pri is Up
    Pri is Up
    Pri is Up
    Pri is Up
    Pri is Up

    whenever the codition like that
    if("RED" eq "RED")

    the output is
    Pri is Up

    but the output should be
    Pri is Down

    Please help me....
    Last edited by numberwhun; Aug 31 '12, 04:37 AM. Reason: Please use code tags when entering code in the forums. With 10 posts under your belt, I am sure someone has told you this at least once already.
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    You're forgetting about the leading/trailing spaces that in $pri_status[$i]

    Personally, I'd probably look at using the Asterisk::AGI module instead of the backticks command, but either way is fine.

    Here's my test script and its output.
    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my %pri_status = (
                   OK      => 'Up',
                   RED     => 'Down',
                   YELLOW  => 'Down',
                   UNCONFI => 'Unconfigured'
    );
    
    my @pri_span = `/usr/sbin/asterisk -rx 'dahdi show status'`;
    
    for my $i (1..$#pri_span) {
    
        my $alarm = substr($pri_span[$i], 41,7);
        $alarm =~ s/\s+$//;
        
        print "PRI span $i is $pri_status{$alarm}\n";
    }
    FYI, I ran this on a failover system, which is why the spans are down.
    [root@009asteris k101 ~]# ./agi.pl
    PRI span 1 is Down
    PRI span 2 is Down
    PRI span 3 is Down
    PRI span 4 is Unconfigured

    Comment

    • omakhileshchand
      New Member
      • May 2012
      • 13

      #3
      hi RonB,
      thank for your reply

      But my question is that whenever I'm compare the condition(In my code) it gives the output like:
      PRI is UP for all the cases (in the case of if("RED" eq "RED") the output should be PRI is DOWN)
      Why eq condition should not give proper output.

      Please guide me..

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        If you dump out your @pri_status array, you'll see that each element has leading and trailing spaces and in my test it also has a line terminator. Those spaces and line terminator are the reason why the eq test is failing and falling through to the else block.

        Code:
        #!/usr/bin/perl
        
        use strict;
        use warnings;
        use Data::Dumper;
        
        my @pri_span = `/usr/sbin/asterisk -rx 'dahdi show status' | cut -c40-47`;
        print Dumper \@pri_span;
        
        # I'm not sure where the \n line terminator is coming from
        # but lets get rid of them
        chomp @pri_span;
        
        foreach my $status ( @pri_span ) {
            print "<$status>\n";
        }
        On my server produces:
        Code:
        [root@009asterisk101 ~]# ./agi.pl 
        $VAR1 = [
                  '  Alarms
        ',
                  '  RED   
        ',
                  '  RED   
        ',
                  '  RED   
        ',
                  '  UNCONF
        '
                ];
        <  Alarms>
        <  RED   >
        <  RED   >
        <  RED   >
        <  UNCONF>

        Comment

        Working...