in reply to Re: left justify/pad hex value
in thread left justify/pad hex value

one other question. Let's say LIST.file contained:
10.34.2.252 10.34.2.250 10.34.2.248
I understand how to open the file. But I need a way to read each octet and place it accordingly into $octet1, etc.

Replies are listed 'Best First'.
Re^3: left justify/pad hex value
by jwkrahn (Abbot) on Mar 03, 2010 at 19:42 UTC
    $ echo "10.34.2.252 > 10.34.2.250 > 10.34.2.248" | perl -e' use Socket; while ( <> ) { chomp; print "$_ -> ", unpack( "H*", inet_aton $_ ), "\n"; } ' 10.34.2.252 -> 0a2202fc 10.34.2.250 -> 0a2202fa 10.34.2.248 -> 0a2202f8

      Or just

      $ echo "10.34.2.252 > 10.34.2.250 > 10.34.2.248" | perl -nle 'print "$_ -> ",unpack "H*", eval' 10.34.2.252 -> 0a2202fc 10.34.2.250 -> 0a2202fa 10.34.2.248 -> 0a2202f8

      ;-)

      Just found out this works as well:
      #!/usr/bin/perl use strict; use warnings; my $data = '10.34.2.252'; my @values = split('\.', $data); foreach my $val (@values) { $val = sprintf("%02x", $val); print $val; } print "\n"; exit 0;
      Thanks for the help!
        Or:
        my $data = '10.34.2.252'; printf("%02x%02x%02x%02x\n",split('\.', $data));