Gossamer Forum
Home : General : Perl Programming :

Getting certain variables?

Quote Reply
Getting certain variables?
Hi folks,

Can anybody tell me what I'm doing wrong here? Ok, user has submitted the form and I want to print the variables and values that aren't listed in the @ignore array. %in is the hash gotten from &readparse;. If I take out the foreach checking it prints them all, but when it's in there it won't print anything.


Code:
while (($key, $val) = each %in ) {
foreach $ignore (@ignore) {
unless ($key = $ignore) {
print FILE "$key|$val\n"
}
}
}

Ta,
adam
Quote Reply
Re: Getting certain variables? In reply to
 
Quote:
I should have used "eq" instead of "=" (cos it's a text string).

Right error, wrong reason. '=' is an assignment operator, so you were assigning the value of $ignore to $key each time. '==' is a numerical comparison and 'eq' is a string comparison.

Cheers,

Alex
Quote Reply
Re: Getting certain variables? In reply to
Just a little detail, this:

Code:

print FILE "$key|$val\n"

supposed to look like this:

Code:

print FILE "$key|$val\n";

Smile


Regards,

Pasha

[This message has been edited by Pasha (edited April 06, 1999).]
Quote Reply
Re: Getting certain variables? In reply to
Thanks Pasha.

Funnily enough it didn't generate an error, but I've fixed it now. I've also fixed another error, I should have used "eq" instead of "=" (cos it's a text string). I've got it printing now, but it's looping and printing each variable five times (foreach $var and foreach $ignore in @ignore), however it's printing the one I want an extra time, so I just have to sort out the loops and I reckon I'll have it!

Isn't Perl fun, he said with a headache!

Smile

adam
Quote Reply
Re: Getting certain variables? In reply to
Thanks chaps, I got it. I had to use a flag for checking because of the two loops.

Code:
while (($key, $val) = each %in ) {
foreach $ignore (@ignore) {
if ($ignore eq $key) { $flag++; }
}
if ($flag == 0) { print FILE "$key|$val\n"; }
$flag = 0;
}

I'm still wondering though if there's an easier way of doing it?

Cheers,
adam