in reply to Re: How to enter a MAC address
in thread How to enter a MAC address

Thanks Liverpol, that should work. My problem was I was breaking the address into bytes, then displaying each byte as an entry widget, using a label widget with ':' in it as a separator. I couldn't get the entry widgets to show my numbers as hex, they kept getting converted to decimal.

This is what I had. I'm pretty new to this stuff, learning perl and tk as I go.

use strict; use Tk; # Take a 6 element array and make it string. sub makeString { my @data = @_; my $result = sprintf "%02x:%02x:%02x:%02x:%02x:%02x", $data[0], $data[1], $data[2], $data[3], $data[4], $data +[5]; return $result; } sub doit { my @addy = @_; my $mw = MainWindow->new(); # Show the current address my $current = makeString(@addy); $mw->Label(-text => $current)->pack(-side => 'top'); # now build some entry widgets to change it for (my $i = 0; $i < 6; $i++) { $mw->Entry(-textvariable => \$addy[$i], -width => 3) ->pack(-side => 'left'); $mw->Label(-text => ':', -width => 1)->pack(-side => 'left'); } $mw->Button(-text => "Done", -command => sub {$mw->destroy;}) ->pack(-side => 'bottom'); MainLoop; return(@addy); } my @foo = (0x10, 0x22, 0x33, 0x4b, 0x5f, 0x6d); print "Starting with: " . makeString(@foo) . "\n"; my @bar = doit(@foo); print "Ended with: " . makeString(@bar) . "\n";

Replies are listed 'Best First'.
Re^3: How to enter a MAC address
by liverpole (Monsignor) on Feb 14, 2007 at 22:28 UTC
        "... learning perl and tk as I go"

    Well, I'm impressed.  Learning Perl takes enough effort (though mostly fun :-D), and Tk is no simple discipline to master.  Good for you!

    If you want to display hexadecimal, you could just use sprintf to reformat each hex pair before the assignment:

    $mw->Entry(-textvariable => [ sprintf "%02x", $addy[$i] ], -width => 3 +) ->pack(-side => 'left');

    Another minor suggestion:  check your loop variable so you don't print an extra ':' after the final byte:

    if ($i < 5) { $mw->Label(-text => ':', -width => 1)->pack(-side => 'left'); }

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      I'm close. How do I validate my entry? I need either 1 or 2 hexadecimal characters. I've got 6 entry widgets, 1 per byte. Each treats it's contents as a string. I'm validating on focusout. So far none of my regex's has worked, the naive version was:
      -validatecommand => sub{$_[0] =~ /\x/},
      Gee, hung up on a regex. Whoodathunk? :)

      I've got 1 other problem. The first entry widget doesn't have focus when it starts, I have to hit tab first. How do I fix this?

        Update:  After re-reading the documentation, I see that what I originally suggested is wrong.  \xnn matches "the character whose numeric value is nn", where "nn" is a pair of hex digits.

        I think that /\x/ will only guarantee you that you have 1 hex digit in your string, though won't work.  If you want to make sure there are exactly 2 hex digits, use something like:

        -validatecommand => sub{$_[0] =~ /^[0-9a-f]{2}$/i},

        The ^ matches the beginning of the line, the $ matches the end, and the {2} matches exactly 2 hex digits (0-9 or a-f).  The i at the end causes ignoring of case, of course.

        Check perlre if you need to read up on regexes.  Good luck!


        s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/