Strange behavior when presence checked for each element in an array

M

mmasroorali

Dear All,
Below is a minimal version of my code. I want to check whether all the
elements of an array are contained in another array. Sounds fairly
simple, but somehow one is not being found. The second line of output
should also be a Match, which is not as you can see.

Could anybody please tell me what is it I am missing. I have ran
everything through ptkdb without a clue.

Regards.

#!/usr/bin/perl

use strict;
use warnings;

use Text::parseWords;

my $f;

my $inputline = '"a, b", c, d';
my $flag = "a,b";

# Separate flags into a list of tokens
my @flag = split(/,/,$flag);

# Get the flag part from input line
(my $inputflags, my $rest) = quotewords(",", 0,$inputline);

# Split the flags into an array and convert them to a hash for
# faster lookup
my @inputflags = split(",", $inputflags);
my %ininputflags = ();
for (@inputflags) { $ininputflags{$_} = 1;}

# Check if all of the flags are contained the in the input line flag
field (first field)
for $f(@flag)
{
if ($ininputflags{$f})
{
print "Match for $f in :mad:inputflags:\n";
}
else
{
print "Unmatch for $f in :mad:inputflags:\n";
}
}


OUTPUT
Match for a in :a b:
Unmatch for b in :a b:
 
T

Tad McClellan

Below is a minimal version of my code. I want to check whether all the
elements of an array are contained in another array. Sounds fairly
simple,


Sounds like it might be Asked Frequently:

perldoc -q array

How do I compute the difference of two arrays? How do I
compute the intersection of two arrays?
but somehow one is not being found. The second line of output
should also be a Match, which is not as you can see.
Could anybody please tell me what is it I am missing.


A space character.

my $inputline = '"a, b", c, d'; ^
^

(my $inputflags, my $rest) = quotewords(",", 0,$inputline);
my @inputflags = split(",", $inputflags);


$inputflags[1] eq ' b' will now be true.

A pattern should *look like* a pattern (not a string).

my @inputflags = split(/, /, $inputflags);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top