netxus
August 29, 2009, 12:25pm
1
Hi Anyone can help me??i writing a perl using array to translate ip address to the hostname my script are below.this line print "$myhost{$ipaddr}\n"; blank hostname dint come out:mad:
#!/usr/local/bin/perl
@ipaddr=("192.1.168.2","172.25.1.13","129.1.2.5");
%myhost = { 192.1.168.2 =>"Machine 1",172.25.1.13=>"Machine 2",129.1.2.5=>"Machine 3");
foreach $ipaddr(@ipaddr ){
print "$ipaddr\n";
print "$myhost{$ipaddr}\n";
}
---------- Post updated at 11:22 AM ---------- Previous update was at 11:16 AM ----------
fyi i have try
print "$myhost{192.1.168.2}";
supposely come out Machine 1 but nothing comeout...blank!! .anyone pls help me..
---------- Post updated at 11:25 AM ---------- Previous update was at 11:22 AM ----------
this line print "$ipaddr\n"; no problem can print out the ip addre:b:
192.1.168.2
172.25.1.13
129.1.2.5
Here's what can be written without having touched perl for a long once time: a good bad example
#!/usr/bin/perl
@ipaddr = ("192.1.168.2","172.25.1.13","129.1.2.5");
foreach $ipaddr(@ipaddr){
$i = $i + 1;
%myhost->{"$ipaddr"} = "Machine $i";
$val = %myhost->{$ipaddr};
print "IP $ipaddr has name $val\n";
}
I wish I could strike that, just to make sense at tyler_durden's post, otherwise I'd be glad to erase it.
A couple of mistakes in your script.
Your hash initialization is incorrect, it should be a hash-list equivalence.
Your hash keys are not the same as array elements.
Since this is Perl 101, I will not (by spoon-feeding you the solution) steal from you the joy of discovering things on your own.
tyler_durden
After a few readings:
#!/usr/bin/perl -w
use strict;
my @ipaddr = ("192.1.168.2","172.25.1.13","129.1.2.5");
my $i = undef;
my %hosts = ();
my $val = undef;
foreach my $ip (@ipaddr){
$i++;
$hosts{$ip} = ("Machine $i" ) ;
print "IP: $ip has name: $hosts{$ip}\n";
}
doesn't print errors any more
I hope this is going to be more helpfull.
netxus
August 30, 2009, 1:40am
5
Hi daPeach
Thanks for your kind help.
---------- Post updated at 12:40 AM ---------- Previous update was at 12:38 AM ----------
Hi durden_tyler
Thanks for your point out...Thanks