But, I'm sure there's a much better way, since this script goes through all the IPs in @BANNED_IP and takes some time to execute if the list gets longer.
I hope I'm clear to you ;)
Thanks in advance;
Mzzl, Chris
Are you sure it'll take that long to go through...?
Just as a test... on my machine it took under a second pretty much to loop through 1 000 000 values in an array and compare them to a number. I doubt you'll want to ban that many users hehe :).
The script I used was this by the way:
#!/usr/bin/perl
for ($i = 0; $i < 1000000; $i++) {
push (@numbers, int rand 1000);
}
I think the way you've got of doing it will work just fine...
On the other hand my machine is a 1.2Ghz Athlon... ;).
But isn't there just a function to check if a value is in an array; for example:
I have a value $my_favorite containing fish and I have an array @food containing ("bread","apple","steak","patatoos","fish","icecream","vegitebles") (I don't know how to write some, but I hope you get the idea)
Now lets say the function I'm talking about is named where_is() and I use the next line:
$position = where_is($food,$my_favorite);
Then I want $position to be 4
Get it?
Thanks, Chris
Forgot a [/b]
Nope, nothing like that... you'd have to make your own function, something like:
sub where_is {
my ($array, $what) = @_;
my ($i);
for ($i = 0; $i < scalar @$array; $i++) {
return $i if $what =~ /^$$array[$i]$/;
}
return -1;
}
That's assuming you're passing an array reference to the subroutine, rather than the array itself... i.e you'd call it with
print where_is (\@food, $my_favourite);
Rather than
print where_is (@food, $my_favourite);
Seriously - don't worry about your code - it's quite a common thing to do, really, and it really does use up less CPU time than you might think.
Perl isn't like PHP where there's a function for anything and everything, it gives you what you need language-wise, and you do the rest :)
Ok, so there's no function what does what the function you made does...
Well, then I'll start using yours, thank you very much.
Mzzl, Chris
P.S. what does scalar do and why is the $ after ^ at /^$$array[$i]$/
The 'scalar' forces the array to be interpreted in a scalar context - i.e. it returns the number of elements in the array. With arrays you can often use $#array to return the last element in the array, but I was uncertain about using this on an array reference.
The regular expression /^$$array[$i]$/ isn't too bad.
The ^ at the front and the $ at the end make sure it's an exact match. There are two $'s before 'array', because $array isn't a real array at all, it's actually something which merely refers to the @array, so in order to actually retrieve any values from it, it has to be dereferenced, and you can do this by prepending a $. (You can also do $array->[$i] I think).
Hope that helps a bit.
Well, english isn't my primairy language, but I'll just read over it a couple of times and hope that's make me understand it.
Thanks a lot for all help
Hmm yeah, my explanation wasn't great....
It might not be much easier in terms of reading - but this (http://www.perldoc.com/perl5.6.1/pod/perlreftut.html) will be more complete than anything I produce, and could help clear things up for you. I took a while to get the hang of references myself..
That URL made most of it clear ;)
It even made me able to solve an other problem I gave up on :p