0

I have written a sub procedure in perl to check whether if my string array contains a particular value.

sub check_if_entity_exists() {

    my $entity      = shift;
    my @entityarray = @_;

    print "my entity to be checked $entity  \n";
    print "entity array before change  @entityarray : \n";

    join(" ", map { s/^\s*|\s*$//g; $_ } @entityarray);

    print " array after change @entityarray \n";

    my $status = "FALSE";

    if (grep { $_ eq $entity } @entityarray) {
        $status = "TRUE";
        return $status;
    }
    else {
        return $status;
    }
}

In the above code @entityarray = xyz.com $entity = xyz.com Since entity is there in entity array i expect to set to true but flow is going to false

Output log: my entity to be checked xyz.com entity array before change xyz.com : array after change xyz.com

6
  • It works for me (given that you remove the prototype from the sub). How do you call the sub? Commented Apr 18, 2013 at 9:59
  • How are you calling this function, because I tried it with 'xyz.com' as per your exmple and it returned "TRUE". So it worked for me. Commented Apr 18, 2013 at 9:59
  • @stevenl and Quick Joe, presumably the two differ in leading or trailing whitespace, which is not showing up in the question. The removal of whitespace is the part of the code that is failing. Commented Apr 18, 2013 at 10:07
  • I am calling like this check_if_entity_exists( @result_list_instance, $instance_to_be_checked ); Commented Apr 18, 2013 at 10:16
  • I was calling it reverse thanks Commented Apr 18, 2013 at 10:19

1 Answer 1

3

You have an empty prototype for your check_if_entity_exists subroutine. That insists that there must be no parameters to this subroutine, which is wrong. You should never use prototypes in Perl - they work differently from prototyps in other langauges and are meant for something very specific.

You are also using map in void context, and join generates a string that is immediately discarded. You should always have

use strict;
use warnings;

at the top of all your programs, which would have told you

Useless use of join in void context

And you should write the map loop as

for (@entityarray) {
  s/^\s+//;
  s/\s+\z//;
}

Other than that, your code does what it should for me. If I call it like this

my @entityarray = ('    xyz.com    ');
my $entity = 'xyz.com';

print check_if_entity_exists($entity, @entityarray);

output

my entity to be checked xyz.com  
entity array before change      xyz.com     : 
 array after change xyz.com 
TRUE

It would be better to write this using the first function from List::Util, like this

use List::Util 'first';

sub check_if_entity_exists {
  my $entity = shift;
  defined(first { /^\s*\Q$entity\E\s*$/ } @_) ? 'TRUE' : 'FALSE';
}
Sign up to request clarification or add additional context in comments.

2 Comments

I am calling like this check_if_entity_exists( @result_list_instance, $instance_to_be_checked );
But your code pulls $entity from the beginning of the array, so you should reverse those two paremeters. Take the prototype off the subroutine definition anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.