Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Leading Characters Not Visible for Certain Array Elements While Printing

by graff (Chancellor)
on Oct 31, 2013 at 02:02 UTC ( #1060495=note: print w/ replies, xml ) Need Help??


in reply to Leading Characters Not Visible for Certain Array Elements While Printing

The first reply seems plausible - I can't imagine any other cause, based on the information you've given so far. When in doubt about line terminations on input, I just do:

s/\s+$//; # instead of chomp
Apart from that, I have to ask: where does your input file come from? How confident are you that it would never contain something that you wouldn't want included in a shell command line?

If you were to add "-T" on the shebang line of your script, it would die rather than do  `host $host` , and with good reason.

Now, if you were to use a module, like Net::DNS::Nslookup (and there may well be others), you could get the IP addresses you want without having to run a shell command in back-ticks for each one. And you wouldn't need to worry about pesky little details like an input file that might contain shell-magic characters in the first column. Instead, your script could look like this:

use strict; use Net::DNS::Nslookup; # ... do whatever you do open FH and load MYDB # (I'll fake it for now...) my %MYDB = ( 'WWW.PERLMONKS.ORG' => 'TPF' ); while (<DATA>) { s/\s+$//; my @hostpair = split ' '; my $HOST = uc( $hostpair[0] ); my $nslookup = Net::DNS::Nslookup->get_ips( $hostpair[0] ); for my $addr ( split( /\n/, $nslookup )) { my ( $name, $ip ) = split( /,/, $addr ); my $OWNER = ( exists( $MYDB{$HOST} )) ? $MYDB{$HOST} : "OTHER" +; print "$hostpair[0]\t$ip\t$OWNER\n"; } } __DATA__ www.perlmonks.org google.com
Maybe that doesn't do exactly what you intended, and maybe you'd rather just use the command-line "host" utility - in that case, do be careful about your input...


Comment on Re: Leading Characters Not Visible for Certain Array Elements While Printing
Select or Download Code
Re^2: Leading Characters Not Visible for Certain Array Elements While Printing
by justsomeguy (Novice) on Oct 31, 2013 at 03:11 UTC

    I'd love to use a module like that, unfortunately I'm kinda limited on what modules I can add to the standard distribution. I'm also running "host" just to get the FQDN for what amounts to a giant hostfile for a software asset scanning program. I'll check to see if there's a module that can quickly provide that--one I can possibly sneak into my libpath somewhere. Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1060495]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2014-09-16 05:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (157 votes), past polls