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

I am trying to get something from command line where it reads the string and inserts the string inside of itself. Such as:
myscript name
output would be:
  nanameme

I know I need to use the substr() and length() functions. Am I getting close here??
my $argv = $ARGV[0] || die "No parameters given.\n"; foreach $argv { substr($argv, 3, length($argv)) print $argv; }

Replies are listed 'Best First'.
•Homework alert! Re: Substring for command line entry
by merlyn (Sage) on Jun 21, 2002 at 18:53 UTC
    In the absence of a practical application for such a trick, I will call this one as homework unless otherwise stated.

    Please do not answer this question. You are likely helping someone cheat.

    -- Randal L. Schwartz, Perl hacker

Re: Substring for command line entry
by Abigail-II (Bishop) on Jun 21, 2002 at 18:29 UTC
    No, you are not getting close. You don't specify what you want to do, all you do is give 1 example, and the code is so off it's unguessable what you want.

    Do you want to repeat every two characters? Then you could just do:

    die "No parameters given\n" unless @ARGV; $ARGV [0] =~ s/(..)/$1$1/g; print $ARGV [0], "\n";
    If you want to do something else, please be more specific.

    Abigail

Re: Substring for command line entry
by stajich (Chaplain) on Jun 21, 2002 at 18:49 UTC
    If you want to insert into a part of a string you can use substr as shown below, but a clearer question will get you a better answer.
    substr($argv,2,0) = $argv; print $argv;
Re: Substring for command line entry
by little (Curate) on Jun 21, 2002 at 19:30 UTC
    Wether homework or not, I highly recommend studying the Getopt module which is part of the standard distribution and can handle command line input pretty well.

    Have a nice day
    All decision is left to your taste
      The Getopt:: modules don't do anything that could help him though.

      Makeshifts last the longest.

        At least its a starting point if you need to write shell scripts or ambivalent ones. Only if you know what your input is you can process on the input (as is).
        Have a nice day
        All decision is left to your taste