xavier mas wrote:
A Dissabte 13 Gener 2007 18:53, Xavier Noria va escriure:
On Jan 13, 2007, at 6:29 PM, xavier mas wrote:
hello list,

I am trying to find if an element in one primary file (transformed
to array)
is included in two other different secondary files (transformed to
arrays,
too); the result is going to be printed as 1 or 0:
According to the code that's 1 or -1.

...
#creating arrays from its text files
@img_array=<IMATGES>; @dict_array=<DICT>; @in_array=<IN>;
#creating hashes from its arrays
foreach $in (@in_array) {chomp($in); $in_hash{$in}= 1;}
foreach $in (@dict_array) {chomp($in);$dict_hash{$in}= 1;}
foreach $in (@img_array) {chomp($in);$img_hash{$in}= 1;}
#searching primary element in secondary hashes
while (($key, $value) = each %in_hash) {
if (exists $dict_hash{$key}) {$dic_flag="1";}else {$dic_flag="-1"}
if (exists $img_hash{$key}) {$img_flag="1";}else {$img_flag="-1";}
#printing result
print "$img_flag, $dic_flag\n";
A bit of air would improve readability, the code is easy but dense
just because of lack of layout.

but it seems 'exists' function doesn't fly to do this -the element
isn't
always found into the secondary hashes. Any suggestion of why it
doesn't and
how to do it?
Besides de potential mismatch given by the fact that the "img" hash
has an "img" flag, but the "dict" hash has a "dic" flag (everything
seems correct in that snippet anyway), I see no problem. Could you
please send a minimal, self-contained code with minimal example files
that let us reproduce the issue?

-- fxn

PS: Please turn strict and warnings on.

thank you for your answer, Xavier.

Here's an example:
in (file, array and hash) contains: "woman, lion, ball"
img (file, array and hash) contains: "ball, dog, cat, lion".
dict (file, array and hash) contains: "house, man, woman, kid, kitchen, lion"


Comparing in with dict ans img, I'll expect as a result (all previous code is between the while curly braces): -1, 1 1, 1
1, -1
but the result is, instead:
-1, -1
-1, -1
-1, -1
that means never finds it.

I hope this is enough data.

I think it is very likely that you have leading or (more likely) trailing
whitespace in one or more of your data files. Add this subroutine to your
program

sub readfile {
  my $fh = shift;
  my @data;
  while (<$fh>) {
    s/^\s+//;
    s/\s+$//;
    push @data, $_ if length;
  }
  @data;
}

and then replace your line

@img_array=<IMATGES>; @dict_array=<DICT>; @in_array=<IN>;

with

@img_array = readfile(\*IMATGES);
@dict_array = readfile(\*DICT);
@in_array = readfile(\*IN);

and see if you get any improvement.

If you need to post any further code then please edit it so that there is one
statement per line, as otherwise I have to do that myself before I can read it.

HTH,

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to