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

#!/usr/bin/perl -w @array=(a..z); splice @array,4,5,map(uc,@alpha[4..8]); $,=''; print "@array\n";
output is a b c d <SPACE>j k l m n o p q r s t u v w x y z what is wrong in this code ..i am trying to capitalize e, f, h, i Thanks in Advance

Replies are listed 'Best First'.
Re: splice question
by Zaxo (Archbishop) on Nov 06, 2003 at 19:58 UTC

    You have spelled '@array' as '@alpha'. The code works as you wish with that correction.

    After Compline,
    Zaxo

      Thanks ..my bad ..i need to relax a bit .did not catch that !
        i need to relax a bit

        4 out of 5 dentists surveyed recommend "use strict;" along with rest and relaxation for solving most misspelled variable name problems.


        Dave


        "If I had my life to live over again, I'd be a plumber." -- Albert Einstein
Re: splice question
by Not_a_Number (Prior) on Nov 06, 2003 at 20:07 UTC

    Short answer: replace @alpha with @array!

    Slightly longer but more fundamental answer: always put use strict; at the beginning of your code. OK, this means that you have to change your first line to:

    my @array = ('a'..'z');

    ...but the extra typing pays dividends, since Perl itself would have pointed this error out to you, thus saving you the time it took you to post to perlmonks :-)

    dave

    Update: BTW, your line $,=''; doesn't actually do anything unless you had previously set $, to something else...

      ...your line $,=''; doesn't actually do anything unless...

      It doesn't do anything in the program, anyway, because he quoted the array. He wants $".

        ...He wants $"

        Sorry, I must be missing something:

        my @array = ('a'..'z'); $" = ''; print "@array";

        What's the point? Just do:

        my @array = ('a'..'z'); print @array;

        Why not set a lot of other special variables to the null string (eg $\ = ''; $/=''; while we're about it? :-)

        dave

Re: splice question
by Thelonius (Priest) on Nov 06, 2003 at 19:57 UTC
    Here's what I get when I run it:
    Unquoted string "a" may clash with future reserved word at pmtest.pl l +ine 2. Unquoted string "z" may clash with future reserved word at pmtest.pl l +ine 2.
    Name "main::alpha" used only once: possible typo at pmtest.pl line 3.
    a b c d      j k l m n o p q r s t u v w x y z
    
      The warnings about unquoted 'a' and 'z' is simply because that line should really be properly written as my @array = ('a' .. 'z');