jvd has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm trying to load an array from a text file. The loading seems to work, only the code below doesn't work anymore when I load @politici from a text file which looks like:
N.Gingrich S.Thurmon R.Reagan S.Bono C.Eastwood
What's wrong ?
#!/usr/bin/perl -w @sterren=('J.v.Dijk','R.Reagan','C.Eastwood','M.Jackson','Cher','S.Bon +o'); @politici=('N.Gingrich','S.Thurmon','R.Reagan','S.Bono','C.Eastwood',' +JvDijk','M.Thatcher'); %diff=(); foreach (@sterren) {$diff{$_}=1;} @diff=grep( ! $diff{$_}, @politici); print @diff;

Replies are listed 'Best First'.
Re: array from file and compare
by talexb (Chancellor) on Dec 24, 2001 at 02:48 UTC
    I'd highly recommend the Perl Cookbook by O'Reilly. It answers this and many other questions.

    Your question is answered on page 106 in section 4.6 as follows:

    # Store each of the 'sterren' elements into the hash FirstGroup. foreach $Element ( @sterren ) { $FirstGroup{ $Element } = 1; } # If any of the sterren elements also exist in array politici, # store that value into the hash 'Both'. foreach $Element ( @politici ) { if ( $FirstGroup{ $Element } ) $Both{ $Element } = 1; } # Finally (this step may or may not be necessary depending on what # you want to do) store the keys back into an array. @Result = keys %Both;

    "Excellent. Release the hounds." -- Monty Burns.

Re: array from file and compare
by jlongino (Parson) on Dec 24, 2001 at 00:43 UTC
    You'll probably want to use  chomp @politici; before you do any comparisons. HTH,

    --Jim

Re: array from file and compare
by Fastolfe (Vicar) on Dec 24, 2001 at 03:08 UTC
    List::Util may also be of interest to you and your fellow classmate who asked this question earlier today.