0

I need to compare two Perl object values, one is from a variable and another one is from an array value

ImmediateParent and data contain path directive values(C:\Users\Public\Documents)

while (length(basename(dirname(($immediateParent)))) > 1)
{
    $immediateParent = (dirname(($immediateParent)));
    my ($dictionaryitem) = $';
    my $boolean =0;

    foreach $dictionaryitem (@data)
    {
        if ($immediateParent eq $dictionaryitem->[0])
        {
            $boolean = 1;
            last;
        }
    }
        
    if ($boolean)
    {
        last;
    }
}

I attempted to compare the values of two paths directories, but the condition always returned true, so it was ineffective. Would you kindly advise me on how to compare two path values?

Magesh Maggi
  • 269
  • 2
  • 10
  • Can you also show what values you are trying to compare? We don't know what `@data` contains or what the value of $immediateParent is. – choroba Jun 30 '22 at 06:01
  • @choroba both contain path directive values(C:\Users\Public\Documents) – Magesh Maggi Jun 30 '22 at 06:37
  • Please show the output of `use Data::Dumper; print Dumper($immediateParent, $dictionaryitem);` right before the comparison. – choroba Jun 30 '22 at 06:38
  • Strings are indeed compared using `eq`, like you do, and paths are just strings. So it seems to me that something else isn't right, if you are getting results which aren't reasonable. Did you ever print them, before the `if` comparison, to see what is going on -- whether they are indeed just strings, that they have sensible values (and whether they are indeed the same)? – zdim Jun 30 '22 at 06:39
  • Run your script in _debug_ mode, set __watch__ for `immediateParent` and `dictionaryitem` and inspect what values you compare. You code does not allow to see a whole picture. It is unknown how you obtain `$'` value -- it must be some form of comparison and we are not aware what variables was in that _equation_ to begin with. – Polar Bear Jun 30 '22 at 07:02
  • __Note:__ you call `dirname(($immediateParent)` twice when you could set a variable to store the value only once. The content of `@data` is unknown from your script. Perhaps you should restate your question with sufficient code to understand what is going on. – Polar Bear Jun 30 '22 at 07:04
  • @choroba itr prints $VAR1 = 'C:\\Users\\Public'; $VAR2 = 'C:\\Users\\Public'; but stil if condition does not pass – Magesh Maggi Jun 30 '22 at 08:11
  • @PolarBear Both the value contain folder path directives – Magesh Maggi Jun 30 '22 at 08:13
  • `my ($dictionaryitem) = $';` oof! There's no guarantee that `$'` is meaningful here! – ikegami Jun 30 '22 at 13:36

1 Answers1

1

If Data::Dumper shows

$immediateParent = 'C:\\Users\\Public';
$dictionaryitem  = 'C:\\Users\\Public';

then you should compare them directly without array dereference, i.e. remove the ->[0]:

if ($immediateParent eq $dictionaryitem)

The fact that Perl let you dereference a string is weird. Are you not using strict?

Polar Bear
  • 6,762
  • 1
  • 5
  • 12
choroba
  • 231,213
  • 25
  • 204
  • 289