Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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__
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...

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1060495]
[1nickt]: Yaerox probably best to whip up an SSCCE demonstrating the failure in what you have now and post to SoPW as a question.
[Corion]: Yaerox: That's a somewhat hard problem. Encode solves the conversion part, but for guessing what encoding a file is in, that's the hard part
[Corion]: Yaerox: There is Encode::Guess, but that needs a limited set of inputs, and it also cannot handle multiple single-byte encodings
[Corion]: If you have a BOM, that's a really easy way to recognize UTF-8. Otherwise, you can try to decode a file from UTF-8, and if that works OK and doesn't crash, most likely the file was valid UTF-8
[Corion]: But as "ansi" (Latin-1?) is a single-byte encoding, any file is a valid ANSI file

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (11)
As of 2017-03-28 13:17 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (332 votes). Check out past polls.