http://qs1969.pair.com?node_id=118046

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

The following snippet is intended to iterate through my Cisco LAN switches to do stuff with each one.   My naming convention is switchNNNa, where NNN is a 3digit number greater than 100 and the last character is always the letter a.

But the "a" following the device number is tripping me up.   Perl thinks I mean "$ia" instead of "$i"a with this error: Global symbol "$ia" requires explicit package name at line 5

Suggestions from Wiser Monks?   Perl 5.00503 on Debian 2.3r3, in case it matters.
    cheers,
    Don
    striving toward Perl Adept
    (it's pronounced "why-bick")

#!/usr/bin/perl -w use strict; my $i = 100; for ($i..$i+200) { my $host = "switch$ia"; print "$host\n"; $i++; }

Replies are listed 'Best First'.
Re: "switch$ia" but don't interpolate trailing "a"
by jeroenes (Priest) on Oct 10, 2001 at 21:01 UTC
    Not that I am wiser, but:
    my $host = "switch${i}a";
    does the trick.
(tye)Re: "switch$ia" but don't interpolate trailing "a"
by tye (Sage) on Oct 10, 2001 at 21:44 UTC

    My favorite is "switch$i$,a". (:

            - tye (but my friends call me "$,T$,y$,e$,")
Re: "switch$ia" (Russ: Useless Benchmarking again)
by Russ (Deacon) on Oct 10, 2001 at 21:16 UTC
    I have to assume someone has done this before, but I didn't see it, so...

    Benchmarking interpolated strings vs. appended strings:

    use Benchmark; timethese( 1000000, { interp => sub {my $R = 1; my $S = "Test${R}a"}, append => sub {my $R = 1; my $S = 'Test' . $R . 'a'} });
    results in:
    Benchmark: timing 1000000 iterations of interp, manual... interp: 4 wallclock secs ( 3.68 usr + 0.05 sys = 3.73 CPU) @ 2680 +96.51/s manual: 3 wallclock secs ( 3.79 usr + -0.13 sys = 3.66 CPU) @ 2732 +24.04/s
    Slightly faster to append strings together than to interpolate them, but statistically, it's a wash...

    Russ
    Brainbench 'Most Valuable Professional' for Perl

      Someone please correct me if I'm wrong, ISTR interpolated strings are converted to concatenation at compile time?

        You are correct, at least according to the B::Deparse documentation. Interpolation is just syntactic sugar for concatenation. You can see it for yourself:

        $ perl -MO=Deparse,-p,-q -e '$foo = "BAR"; $bar = "a${foo}c"; $baz="a" +.$foo."c"' ($foo = 'BAR'); ($bar = (('a' . $foo) . 'c')); ($baz = (('a' . $foo) . 'c'));

        Update: Added concatenation to the example for comparison. Also, the output using B::Terse to dump the opcode tree may be more convincing.

        <sheepish grin>
        hehehe You're probably right.

        So, in the spirit of further useless benchmarking:

        use Benchmark; timethese( 10000000, { 5 => sub {my $R = 5;}, 6 => sub {my $R = 6;} });
        results in:
        5: 6 wallclock secs ( 6.61 usr + -0.03 sys = 6.58 CPU) @ 1519756 +.84/s 6: 6 wallclock secs ( 5.89 usr + 0.08 sys = 5.97 CPU) @ 1675041 +.88/s
        As you can see, it is slightly faster to assign a 6 than a 5, so I will use 6's more often from now on... :-)
        </sheepish grin>

        Russ
        Brainbench 'Most Valuable Professional' for Perl