Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

substrings

Status
Not open for further replies.

bary

IS-IT--Management
Aug 27, 2003
1
US
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??
[tt]
my $argv = $ARGV[0] || die "No parameters given.\n";

foreach $argv
{

substr($argv, 3, length($argv))
print $argv;
}[/tt]
 
Don't really need the foreach, since the "my $argv..." line gives you a single value, rather than a list.

For the substr itself, you're close...

substr($argv, 2, 0) = $argv;

will do what your example request shows. Using 0 for the LENGTH parameter makes it insert, instead of overwrite the following portion of the variable.

The way you have it written, the substr would return the portion of $argv from the 4th character to the end, but since you don't capture the return value of the substr call, it would be discarded, then you print out the original $argv.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top