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

Inline substitution

Status
Not open for further replies.

CJason

Programmer
Oct 13, 2004
223
US
All,

This is kind of an odd issue...but, "fun" for all you guru's, I bet! This one has stumped me for awhile now.

I am writing a code converter (please keep that in mind...because that SHOULD limit the possible solutions). The issue I've run into is that I need to do a substitution "on the fly" within a line of code. For example (and this is VERY simplyfied):

Original Code
Code:
1: A = "HELO"
2: A = "B" + (A - "L")
This results in A = "BHEO". The "+" means "concatenate" and notice how "L" was removed (the "-") from A before the contatenation.

What would you programmatically convert line 2 into for perl? It needs to be short and to the point...if possible.

My attempt (obviously, doesn't quite work correctly):
Code:
$a = "HELO"
$a = "B" . ((index($a,"L")>=0)?substr($a,index($a,"L"),1,""):$a);

Thanks in advance!!
 
This does it:
Code:
( $a = "B$a" ) =~ y/L//d;
 
I made that example pretty simple...as I stated...let's get a little more complex, shall we...
Code:
IF (C .EQS. ("B" + (A - "L"))) THEN C = "YES"
As you can see, I need this "substitution" to occur "inlined" with the IF comparison.

Any ideas?
 
ishnid's solution is easily transposed to your last requirement: I suspect however that there are more requirements, perhaps substitute a letter in place, add at the end,etc., and hopefully some limitations (only 4 uppercased letters chosen from a limited set?).
Without a full picture it's no use guessing solutions.
Even yours is inlinable:
Code:
$a='B'.(($n=index($a,'L'))>=0?substr($a,$n,1,''):'',$a);

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
prex1, your coding example is exactly what I needed...I believe! I'll post back if I run into any issues with it, but it's looking good so far!

One question...for anyone, I guess...why does prex1's solution work? I'm not really following the ":'',$a" part of it...but, using that and putting parenthesis around the entire thing, seems to do the trick???
 
How about:
Code:
$a = 'HELO';
$c = 'BHEO';

if ( ( $a = "B$a" ) =~ y/L//d && $a eq $c ) {
   $c = 'YES';
}
 
ishnid, your example brings up a good point...won't your example change the value of $a? I don't want the value of $a to change in that example. AND, now that I re-look at prex1's solution, it will also change the value!?!? I guess that's a new requirement [ponder]

 
Just use a temporary variable to not modify your original one.
I'll have to insist: set up all of your requirements (a basic ability of any programmer [ponder]) before trying any partial solution.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
prex1, for your solution, it's working great! However, I get the following warning:

"Useless use of a constant in void context at ..."

Is there some way to eliminate this warning using your solution? It's no big deal, I was just trying to clean up errors/warnings as much as possible.

Thanks in advance!
 
Code:
$a='B'.(($n=index($a,'L'))>=0?substr($a,$n,1,''):'',$a)[1];
but also
Code:
$a='B'.($a=~tr/L//d,$a);
The latter will replace all occurrences of 'L' in the string, the former only the first one.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top