[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl-Forum] Problem with checking existence in a hash.



At 4:44 PM 8/20/00, Jimmy Lantz wrote:

[snip]

>What's wrong:
>############### For pseudo code see below. ####
>
>sub do_omreg
>{
>$omregfil = 'Server G4:DATABAS:data:omreg.data';
>&KursModul::read_file($omregfil);
>@omregfil = @KursModul::lines;
>undef %is_kurskod;
>for (@omregfil) { $is_kurskod{$_} = 1 }
>
>@omregkoder = split(/ /,$in{'omreg'});
>
>foreach $some_code (@omregkoder)
>{
>unless ($is_kurskod{$some_code} = 1) #Is this my problem ??
>{
>$fel = 'ja';
>$omregfel = 'ja';
>push (@fel_omreg_kod, $some_code);
>}
>}
>
>}
>
>###########Pseudo-code:
>This sub is supposed to:
>Set the variable $omregfil to a file path.
>Use a read routine from a module
>Set the result to @omregfil.
>the assign every value in the array to a hash and set the hash value to 1.
>then split the value from form input to the array @omregkoder
>then handle each of the values in the array @omregkoder
>and unless the value exist in the hash %is_kurskod then do some variable
>assignments and then push that value into another array.

Didn't look at anything else in your code snippet, but this seems to 
be a place to start:

>unless ($is_kurskod{$some_code} = 1) #Is this my problem ??

Yes, that's a problem. You mean to be testing whether 
$is_kurskod{$some_code} is equal to 1, but what you are doing is 
giving it the value 1, and your 'unless' conditional always gets the 
same result.

In Perl, the '=' sign is for assigning values, _not_ for testing 
them. The assignment X = 1 will always return true. Try:

   unless ($is_kurskod{$some_code} == 1)

Note the use of '=='. This is the numerical 'equals' test. ( 'eq' is 
the string equivalent to '==').

If you didn't care what value $is_kurskod{$some_code} has, as long as 
it's not 0 or some other value that Perl resolves as false, you could 
write the above as:

   unless ($is_kurskod{$some_code})

HTH

1;


-- 

- Bruce

__Bruce_Van_Allen___bva@cruzio.com__Santa_Cruz_CA__

==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-forum-request@macperl.org