2

Hey guys I am having trouble figuring out how to convert the following function (which requires an input) into a variable

 function Convert-ToLetters ([parameter(Mandatory=$true,ValueFromPipeline=$true)][int] $value)  {
   $currVal = $value;
   $returnVal = '';
   while ($currVal -ge 26) {
      $returnVal = [char](($currVal) % 26 + 65) + $returnVal;
      $currVal =  [int][math]::Floor($currVal / 26)
   }
  $returnVal = [char](($currVal) + 64) + $returnVal;

     return $returnVal
  }

What this Function does is to convert a number into letters.

Now what I want to achieve is to somehow do this:

$convert2letter = Convert-ToLetters()

So that I can do something like

$WR= "$convert2letter($CValue1)" + "-" + "$convert2letter($CValue2)" + "-" + "3"

But Powershell isnt allowing me to do $convert2letter

So what can I do here?

Thanks

5
  • 1
    Related: stackoverflow.com/questions/24798389/… Commented Dec 7, 2016 at 19:23
  • 2
    Why would you do it like this? Why not just call the function inside your string? Commented Dec 7, 2016 at 19:39
  • Thanks.... but maybe because my brain is just failing today... I could not figure out how to apply it here...even though I recognize it is VERY relevant and most probably the solution to my problem... I am sorry... Commented Dec 7, 2016 at 19:40
  • @Matt I am not sure what you mean.... my problem is that if I do a "Convertto-Letter($Cvalue1)" + "-" + "Convertto-Letter($Cvalue1)" + "-" + "3" ......... ill get a result of Convert-ToLetters(5)-Convert-ToLetters(21)-3 instead of say E-U-3 Commented Dec 7, 2016 at 19:45
  • No need to put solutions in the question. That is what the answers are for! Commented Dec 7, 2016 at 19:58

3 Answers 3

5

Without more information for justifying it I would think instead of this:

$WR= "$convert2letter($CValue1)" + "-" + "$convert2letter($CValue2)" + "-" + "3"

You should just be doing this:

$WR = "$(Convert-ToLetters $CValue1)-$(Convert-ToLetters $CValue2)-3"

which uses subexpressions. Or use the format operator

$WR = "{0}-{1}-3" -f (Convert-ToLetters $CValue1), (Convert-ToLetters $CValue2)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much!!! .... I cant believe I was forgetting something so simple.....I ended up just doing $WR = "$(Convert-toletters $Cvalue1)" + "-" + "$Convert-toletters $Cvalue2" + "-" + "3" .... Man I must be becoming really stupid today
2

If I understand your question, you are saying you want to invoke a function whose name is stored in a variable. One way to do this is by embedding a subexpression - $() - in a string and use the invocation/call operator - & to execute the function. For example:

function T {
  param(
    $a
  )
  "Result = $a"
}

$fn = "T"
"We want $(& $fn Test)"

Output:

We want Result = Test

Comments

1
New-Alias -Name `$convert2letter -Value Convert-ToLetters

Not sure what you are trying to achieve, this doesn't make a lot of sense.

3 Comments

maybe it isnt something allowed in Powershell.... I was trying to make a script where I do several $WR...which is an alpha numeric "address"... but the source output that I originally get is numeric than Alphabetical in some parts...so I have to convert them.... i just wanted to see if I can somehow set $convert2letter as just the function without value.... since you cant use " " when typing the function with value
but why? why can't you call the function?
when I try ...... I get + $convert2letter = Convert-ToLetters() + ~ An expression was expected after '('. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : ExpectedExpression

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.