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

Inlined Substitution

Status
Not open for further replies.

CJason

Programmer
Oct 13, 2004
223
US
Is it possible to do an inlined "substitution"? For example, given the following:
Code:
$this = $that1 . $that2;
I need to do this...but, INLINED:
Code:
$that1 =~ s/X//i;
$that2 =~ s/Y//i;
$this = $that1 . $that2;
So, I'm trying to do something like this:
Code:
$this = ($that1 =~ s/X//i) . ($that2 =~ s/Y//i);
Which, obviously, doesn't work!

I can come up with a way of doing this with "substr", but it gets messy...so, I was hoping there was another, simpler way of doing it.

Thanks in advance!!
 
Not what I'd call elegant, but works in a couple of tests I did
Code:
$this1 = "abc" ;
$this2 = "def";

$this = (($this1 =~ s/b/B/) ? $this1 : $this1) . (($this2 =~ s/e/E/) ? $this2 : $this2) ;
print ">$this<\n";

Note that both $this1 and $this2 will be changed as well if the substitution is successful.
 
Thanks for the reply! I'll check that out.
 
Perl:
my $this = $that1 . $that2 if (($that1 =~ s/X//g . $that2 =~ s/Y//g) || 1);
the || 1 is there to catch the edge case where $that1 and $that2 are both set to ''.

CJason - you'll notice that we've all given up asking why, we just assume this is for your code converter...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
steve...thanks for the response. And, thanks guys for giving up asking "what for" [tongue] You're right...it's regarding my converter...yet again!?!?!? You guys are proving invaluable! I think I know perl quite well, but throw in some of the issues I'm running into with this converter...and I'm a lost puppy.

Thanks again!
 
Do you guys think there's a way to do this without modifying $that1 and $that2?
 
Just do the same think but create 2 new var's.
$var3 = $that1;
$var4 = $that2;

and then do it to $var3 and $var4.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Continuing down the road of no return
Code:
$this1 = "abc" ;
$this2 = "def";

$this = eval {$t1 = $this1 ; $t1 =~ s/b/B/; $t2 = $this2 ; $t2 =~ s/e/E/ ; $t1 . $t2 } ;
print ">$this< >$this1< >$this2<\n";
 
Is the requirement that it be inlined, or just that it should only occupy one line? I mean, could you just
Perl:
$that1 =~ s/X//g; $that2 =~ s/Y//g; my $this = $that1 . $that2;

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Code converter? Convert what to what? Bad perl code into worse other code?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I think it's converting to perl, but from what I have no idea. Sinclair Spectrum Basic, possibly...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Thanks for the responses. I could possibly call a subroutine or, like stevexff said, I just need to keep it on one line.

Good suggestions guys!

Yes, I'm converting TO perl.
 
But from what? We have to know now...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
If you HAVE to know ;-) ......

DCL

Yes, the VMS-Digital Command Language

[sad]
 
I guess there was nothing on CPAN that would do that already, then? [smile]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top