HK has asked for the wisdom of the Perl Monks concerning the following question:

I'm using (attempting to use) PERL to build a representation of an electrical system in a hash of arrays. Taps (branches) off of the main line (trunk) need to use names T1, T2, T3, etc. Taps of taps need to be T1A, T1B, etc T2A, T2B etc. Taps of taps of taps - T1A1, T1A2, etc T2B1, T2B3 etc. Taps will be several layers deep. How can I "autoincrement" in this way?

Replies are listed 'Best First'.
Re: alphanumeric autoincrement?
by tachyon (Chancellor) on Aug 16, 2001 at 11:27 UTC

    Here are a couple of useful OO type functions

    %taps; $taps{'T1'}++; print "New taps, same level\n"; print new_tap('T1')," " for (0..15); print "\nNew taps, new level\n"; print new_level('T1')," " for (0..30); sub new_tap { my $tap = shift; my ($root, $end)= $tap =~ m/^(\w+?)([\d]+|[A-Z]+)$/i; my $new; { $new = $root.$end; if (exists $taps{$new}) { $end++; redo; } $taps{$new}++; } return $new; } sub new_level { my $tap = shift; my ($root, $end)= $tap =~ m/^(\w+?)([\d]+|[A-Z]+)$/i; my $suf = ($end =~ m/[0-9]/) ? 'A' : '1'; { $new = $root.$end.$suf; if (exists $taps{$new}) { $suf++; redo; } $taps{$new}++; } return $new; }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: alphanumeric autoincrement?
by rchiav (Deacon) on Aug 16, 2001 at 03:18 UTC
    You're probably going to have to deal with the different parts independantly. For example..
    #!/usr/bin/perl -w use strict; my $trunc = 1; my $tap = 'A'; my $tapoftap = 1; my ($x, $y, $z); for $x (1..20) { for $y (1..10) { for $z (1..10) { print "T$trunc$tap$tapoftap\n"; $tapoftap++; } $tapoftap =1; $tap++; } $trunc++; $tap = 'A'; }
    Just an example. I don't know how you really need to deal with them. As far as I know, I don't think you can create your own user defined types in perl.

    Also, you could use the above to build an array or a list depending on what you're doing..

    There's also the option of creating a sub that returns a value based on what you want to do. Something that takes the current Trunc-tap-subtap and a keyword that signifies which you want to increment.

    Hope this helps..
    Rich

Re: alphanumeric autoincrement?
by dragonchild (Archbishop) on Aug 16, 2001 at 05:10 UTC
    You can create your own user-defined types in Perl. They're called objects. In addition, you can overload the various operators. I'm pretty sure you can overload ++, both postfix and prefix, but don't quote me on that.

    Then, all you do is create each tap as an instance of this Tap class. This will also be really good cause I'm sure you can think of other operations you'd love to either have a tap do or be able to do on a tap, like copy it or set various attributes.

    ------
    /me wants to be the brightest bulb in the chandelier!

    Vote paco for President!

      You mean you can mimic a user deinfed type by using objects. Not actually create a "type". I see nothing that's the equivilant to typedef in perl. You could mimic similar things with an array or a sub, without the expense of OO, but it's still not a user defined "type".
        Here's the thing - there is no such thing as a "type" in Perl to begin with. (Well, not till Perl6, and even then, not really.) Whenever you do $foo++, the interpreter is converting $foo to an integer, whether it was a string or a float before, then incrementing it. This can lead to interesting behavior. For example, if you do:
        my $foo = "abc"; $foo++; print "$foo\n"; ---- abd my $foo = 1.1; $foo++; print "$foo\n"; ---- 2.1
        Neither of those behaviors, while perfectly logical, have any correspondence to C or Pascal or Java. The only thing the Perl compiler checks is what kind of variable you are working with, not what type.

        So, you are only ever working with an int, for example, through programmer choice. So, you're only ever working with a user-defined type by programmer choice. The compiler isn't going to enforce anything for you with regard to type. (Well, it's not exactly anything, but close enough for government work.)

        ------
        /me wants to be the brightest bulb in the chandelier!

        Vote paco for President!

Re: alphanumeric autoincrement?
by clearcache (Beadle) on Aug 16, 2001 at 10:43 UTC
    ...maybe I would create a hash with keys 1 to 26, values A to Z. Refer to each level of your tap with a numeric value, print out $hash{$numeric_value} for your naming scheme when alphas should be used.

    I'm a little curious about what you'll be reading as an input to build this representation in your hash of arrays...and about what you'll be using the hashes for once you've got them built (are you creating some sort of report?) I could probably come up with a better suggestion if I understood the input...