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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it possible to substitue 1 value with 'n' number of values???

Status
Not open for further replies.

newbie1006

Technical User
Jun 10, 2011
9
US
Example:

Input:
FRSVTu_ORT_TRU

Output:
FRSVT0_ORT_TRU
FRSVT1_ORT_TRU
FRSVT2_ORT_TRU
FRSVT3_ORT_TRU

is there a way to do this using perl scripting?
 
Here's something quick'n'dirty:

Code:
perl -e 'while(++$i<=$ARGV[0]){($s=$ARGV[1])=~s/u/$i/;print"$s\n";}' 4 FRSVTu_ORT_TRU

Annihilannic.
 
is there a way to do this using perl scripting?

There's a way to do practically anything using a perl script, however your question is too vague to give a likely suitable answer.

What exactly are you having trouble with? How is that input supposed to be acquired? Where are you wanting it output?

- M
 
I have a file with content and I'm extracting information from it and storing it in an "array" and I have number of statements in that array. So when I see something like this "FRSVTu_ORT_TRU" in the array I should be able to do something and print out this

output:
FRSVT0_ORT_TRU
FRSVT1_ORT_TRU
FRSVT2_ORT_TRU
FRSVT3_ORT_TRU

so basically everytime I see a 'u' I need to substitute that with '0','1','2','3' and print them separately.

And the output is printed out in the command prompt.
 
Code:
use strict;
use warnings;

my $string = 'FRSVTu_ORT_TRU';

if ($string =~ /(.*?)u(.*)/s) {
	my $pre = $1;
	my $post = $2;
	
	for (1..4) {
		print "$pre$_$post\n";
	}
}

- Miller
 
Thank you very much Miller, but this would just work for that particular string. I have a numerous of those lines in an array, and I need to read an array and for every single line that has a 'u' it has to replace it with those 4 other substitutions I need and print that out.
 
Dude, we can't help you any more than I just have without actual code that you're working with and a specific problem that you're having. I've shown you how to use a regex to search for a 'u' and dynamically replace it with an iterator.

Your situation is too vague to help more than that.

Regards,
- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top