Re: Reading a value from a hash using a variable for the key
If so, how print TTL = $line\n does give me the right value ! (I get TTL = 125 if ping to Windows machine. Thanks, for your answers. On Jan 21, 4:30 pm, rob.di...@gmx.com (Rob Dixon) wrote: On 21/01/2011 05:50, Erez Schatz wrote: On 20 January 2011 15:38, Eyal B.ewinst...@gmail.com wrote: I'm getting an error on the line where I should use the TTL variable - and take the right value from the hash (%list) :Use of uninitialized value in print at D:\system\perl\os-rec\os-rec5_.pl line 24 ,HANDLE line 3. Any idea ? if($line =~ TTL=) { $line =~ s/.*TTL=//; print TTL = $line\n; print $list{$line} ; # print Machine $machine_IP is $list{$line} ; last; } Assuming a specific line is made of nothing but TTL=, then $line =~ s/.*TTL=//; will erase the line, leaving you with an empty (uninitialized) $line variable. No it won't, it will leave $line containing a null (zero-length) string. There is no way to change a string value to uninitialized (undef) by deleting characters from it. - Rob -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Reading a value from a hash using a variable for the key
On Jan 21, 11:42 pm, ewinst...@gmail.com (Eyal B.) wrote: On Jan 21, 4:30 pm, rob.di...@gmx.com (Rob Dixon) wrote: On 21/01/2011 05:50, Erez Schatz wrote: On 20 January 2011 15:38, Eyal B.ewinst...@gmail.com wrote: I'm getting an error on the line where I should use the TTL variable - and take the right value from the hash (%list) :Use of uninitialized value in print at D:\system\perl\os-rec\os-rec5_.pl line 24 ,HANDLE line 3. Any idea ? if($line =~ TTL=) { $line =~ s/.*TTL=//; print TTL = $line\n; print $list{$line} ; # print Machine $machine_IP is $list{$line} ; last; } I found the issue, and resolve it This regex clean unnecessary, extra digits: $line =~/.*TTL=\s*(\S+)\s*$/; Assuming a specific line is made of nothing but TTL=, then $line =~ s/.*TTL=//; will erase the line, leaving you with an empty (uninitialized) $line variable. No it won't, it will leave $line containing a null (zero-length) string. There is no way to change a string value to uninitialized (undef) by deleting characters from it. - Rob ok. So why on print TTL = $line\n; I do get TTL = 125, if it's undefined ? Thanks, Rob, for your answer. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Reading a value from a hash using a variable for the key
On Jan 21, 4:30 pm, rob.di...@gmx.com (Rob Dixon) wrote: On 21/01/2011 05:50, Erez Schatz wrote: On 20 January 2011 15:38, Eyal B.ewinst...@gmail.com wrote: I'm getting an error on the line where I should use the TTL variable - and take the right value from the hash (%list) :Use of uninitialized value in print at D:\system\perl\os-rec\os-rec5_.pl line 24 ,HANDLE line 3. Any idea ? if($line =~ TTL=) { $line =~ s/.*TTL=//; print TTL = $line\n; print $list{$line} ; # print Machine $machine_IP is $list{$line} ; last; } Assuming a specific line is made of nothing but TTL=, then $line =~ s/.*TTL=//; will erase the line, leaving you with an empty (uninitialized) $line variable. No it won't, it will leave $line containing a null (zero-length) string. There is no way to change a string value to uninitialized (undef) by deleting characters from it. - Rob ok. So why on print TTL = $line\n; I do get TTL = 125, if it's undefined ? Thanks, Rob, for your answer. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Reading a value from a hash using a variable for the key
Tried to implement your recommendations step by step . #! C:\Perl\bin\perl use strict; use warnings; my %list = (60=linux,61=linux,62=linux,63=linux, 64=linux,65=linux,125=Windows,126=Windows,127=Windows, 128=Windows,250=Unix,251=Unix,252=Unix,253=Unix, 254=Unix,255=Unix, 256=Unix,257=Unix,258=Unix, 259=Unix,260=Unix); my $path = hosts.txt ; # open (MACHINES,$path) or die Couldn't open file for writing; open my $input_fh, , $path or die Could not open '$path' - $!; # Shlomi, you wrote : Use three args open and don't use bareword file handles... # I'm asking : Trying to implement that (Though, didn't understand what is bareword)where do I indicate the MACHINES handle ? my $machine_IP ; while ($machine_IP = MACHINES) { chomp($machine_IP) ; my $line ; my $cmd ; $cmd = ping $machine_IP; open(HANDLE,$cmd|); # Shlomi, you wrote : You should use three-args open, lexical filehandles and or die. # I'm asking : Can you pls show me how should I write that ? # Furthermore, use $cmd is not needed here as you can interpolate directly from $cmd. # I didn't understand what that means. while ($line = HANDLE) { if($line =~ TTL=) # No need for wrapping $line in double quotes here as $line is a string. # as mentioned above { $line =~ s/.*TTL=//; print TTL = $line\n; print $list{$line} ; last; } # You can do all that in one regex match. # I didn't reach so far ... Is that # if (my ($ttl) = $line =~ m{TTL=(\d+)}) # print Machine $machine_IP is $list{$line} ; } } close HANDLE; close MACHINES; # Without make the changes about the File Handle - I still got the error : Use of uninitialized value within %list in print at C:\system\Perl\OS-recognize\os-rec5.1_.pl line 35, HANDLE line 4. Thanks, Shlomi, Eyal. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Reading a value from a hash using a variable for the key
On Jan 21, 7:50 am, moonb...@gmail.com (Erez Schatz) wrote: On 20 January 2011 15:38, Eyal B. ewinst...@gmail.com wrote: I'm writing a scripts that check the TTL of the ping and found the OS. According the TTL - the script should let me know which OS it is : Linux/ Windows or Unix (Hash table) I'm getting an error on the line where I should use the TTL variable - and take the right value from the hash (%list) :Use of uninitialized value in print at D:\system\perl\os-rec\os-rec5_.pl line 24 , HANDLE line 3. Any idea ? if($line =~ TTL=) { $line =~ s/.*TTL=//; print TTL = $line\n; print $list{$line} ; # print Machine $machine_IP is $list{$line} ; last; } Assuming a specific line is made of nothing but TTL=, then $line =~ s/.*TTL=//; will erase the line, leaving you with an empty (uninitialized) $line variable. A way to debug this will be to include a print $line just before the substitution, so you could find what is happening in each stage of the iteration. -- Erez La perfection soit atteinte non quand il n'ya plus rien à ajouter, mais quand il n'ya plus rien à retrancher. Hi Erez print TTL = $line\n; Does give me the right value ! (I.e TTL = 125) Also print $list{125} ; return the right value But this is not working : print $list{$line} ; What can happen for the variable ? -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Reading a value from a hash using a variable for the key
I'm writing a scripts that check the TTL of the ping and found the OS. According the TTL - the script should let me know which OS it is : Linux/ Windows or Unix (Hash table) I'm getting an error on the line where I should use the TTL variable - and take the right value from the hash (%list) :Use of uninitialized value in print at D:\system\perl\os-rec\os-rec5_.pl line 24 , HANDLE line 3. Any idea ? #! C:\Perl\bin\perl -w use strict; use warnings; my %list = (60,linux,61,linux,62,linux,63,linux,64,linux, 65,linux,125,Windows,126,Windows,127,Windows,128,Windows, 250,Unix,251,Unix,252,Unix,253,Unix,254,Unix,255,Unix, 256,Unix,257,Unix,258,Unix,259,Unix,260,Unix); my $path = hosts.txt ; my $machine_IP ; my $cmd ; # read machines hosts names List from the txt file c:1.txt and take the TTL data to th e variable: $line open (MACHINES,$path) or die Couldn't open file for writing; while ($machine_IP = MACHINES) { chomp($machine_IP) ; my $line ; $cmd = ping $machine_IP; open(HANDLE,$cmd|); while ($line = HANDLE) { if($line =~ TTL=) { $line =~ s/.*TTL=//; print TTL = $line\n; print $list{$line} ; # print Machine $machine_IP is $list{$line} ; last; } } } close HANDLE; close MACHINES; -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/