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

I am trying to find the location of IP of different IP addresses, so i have created an array of IP and want to process them, but some error is showing up.... I have written below code so far, the logic seems fine but not sure what the problem is... Any thoughts

system("clear"); use Geo::IPfree; use warnings; @a1 = qw(192.168.32.121 200.293.12.12); @code = [0]; @name = [0]; print "@a1, \n"; print "$a1[1] \n"; $num = scalar(@a1); print "$num \n"; #value is 2 my $geo = Geo::IPfree->new; for($i=0; $i<$num; $i++){ my ($code[i], $name[i]) = $geo->LookUp(@a[i]); #$code[i] = $a[i] print ("$code[i] - $name[i] \n"); print "@code \n"; }

Replies are listed 'Best First'.
Re: For Loop with Geo::IPFree
by jethro (Monsignor) on Jul 07, 2012 at 21:50 UTC

    Error messages are there to tell you something. Read them carefully and they will give you valuable hints. They also give us valuable hints, so it is advisable to tell us what error message you get. So we don't have to (download any module you use and) execute your script just to find out what error message you got

    I can see that you have lines like @code= [0];. Probably you want @code=(0); instead. () is for lists while [] is for references aka pointers to lists. Not that it changes anything, it seems the value is overwritten anyway. So @code=(); to clear the array would work as well

    Also $geo->LookUp(@a[i]); should be $geo->LookUp($a[i]);. Your syntax probably still works because this is how array slices are specified, but it could lead to hard to find bugs

      Well i got the error i did not write $i, instead i was just using i.... That was the problem. LOL The error message was too too long and gave some problem regarding the module and all

Re: For Loop with Geo::IPFree
by cztmonk (Monk) on Jul 08, 2012 at 10:27 UTC

    This is my code...

    use strict; use warnings; use Geo::IPfree; my @a1 = qw(192.168.32.121 200.293.12.12); my @code = (); my @name = (); my $num = scalar(@a1); print "@a1 \n"; print "$a1[1] \n"; print "$num \n"; #value is 2 my $geo = Geo::IPfree->new; for (my $i=0; $i<$num; $i++){ ($code[$i], $name[$i]) = $geo->LookUp($a1[$i]); print ("$code[$i] - $name[$i] \n"); print "@code \n"; } exit; # This code results in the following output: 192.168.32.121 200.293.12.12 200.293.12.12 2 ZZ - Reserved for private IP addresses ZZ BR - Brazil ZZ BR