1

I'n new to Powershell so apologies if this is an obvious question. I have the following function which delimits a string:

 [int[]]$rec02 = 3,2,1,4,3,3,5,5,6,1,19,45,2,3,50

 function delimitString([string]$text, [int[]]$arrDelims)
        {
                [string]$out;
                [int]$total=0;
                foreach($d in $arrDelims)
                {        
                    $out += $text.substring($total,$d)+",";
                    $total +=$d;
                }

                $out += $text.substring($total,$text.length-$total)+",";
                return $out;
        }

When this is called as follows: delimitString "gjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjlllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffllllllllll" $rec02

How can I suppress/why are there new line/carriage returns at the start/end of the output?

gjj,jj,j,jjjj,jjj,jjj,jjjjj,jjjjj,jjjjjj,j,jjjjlllllllllllllll,llllllllllllllll lllllllllllllllllllllllllllll,ll,lll,llllllllllllllllllllllllllffffffffffffffff ffffffff,ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff fffffffffffffffffffffffffffffffffffffffffllllllllll,

I eventually write this to a file and therefore I am ending up with blank lines in the output.

2 Answers 2

1

[string]$out is not initialization. It jost outputs variable named 'out' as an addition to return. As there is not $out variable, before output it creates it with default value (empty string). When outputting this manner, it adds newline at the end of each outputting line.

So, [string]$out is actually (in your case) New-Object -TypeName 'System.String' -ArgumentList @([System.String]::Empty), as you see it is Function that returns an object

The same problem is with New-Item function. It returns new item's object, so, when you use it in function, you should send output to null

    Function TestReturn {
    New-Item -Path $( [System.IO.Path]::GetTempFileName() + '.test' ) -ItemType 'File'
    New-Item -Path $( [System.IO.Path]::GetTempFileName() + '.test' ) -ItemType 'File'
    return "Hello, World"
}
TestReturn

returns

;    Mode                LastWriteTime     Length Name                                                                                                                                             
;----                -------------     ------ ----                                                                                                                                             
;-a---        26.07.2014      9:08          0 tmpD9B4.tmp.test                                                                                                                                 
;-a---        26.07.2014      9:08          0 tmpDA03.tmp.test                                                                                                                                 
;Hello, World

To omit this, you should use New-Item ... | Out-Null or put value into a variable $temp = New-Item ... So the correct form is (scroll right!)

        Function TestReturn {
    New-Item -Path $( [System.IO.Path]::GetTempFileName() + '.test' ) -ItemType 'File' | Out-Null
    New-Item -Path $( [System.IO.Path]::GetTempFileName() + '.test' ) -ItemType 'File' | Out-Null
    return "Hello, World"
}
TestReturn

Because of this you should use $out='' or (it you fan typization) $out=[String]'' instead of [string]$out . Those to lines do VERY different things. You line tries to create and output variable. Line $out='' just initializes it.

To write to a file you should use $somevar | Out-File or OutFile -InputObject form

[int[]]$rec02 = @(3,2,1,4,3,3,5,5,6,1,19,45,2,3,50)

 function delimitString([string]$text, [int[]]$arrDelims)
        {
                $out='';
                $total=0;
                foreach($d in $arrDelims)
                {        
                    $out += $text.substring($total,$d)+",";
                    $total +=$d;
                }

                $out += $text.substring($total,$text.length-$total)+",";
                return $out 
        }
$tempFile = [System.IO.Path]::GetTempFileName()
$somevar = delimitString "gjjj...." $rec02 
Write-Host -ForegroundColor Red -NoNewline "BEGIN"
Write-Host -ForegroundColor Green -NoNewline $somevar
Write-Host -ForegroundColor Red "END"

Write-Host -ForegroundColor Red "Out Length: $($somevar.Length)"
$tempFile = [System.IO.Path]::GetTempFileName()
Write-Host -ForegroundColor Green "TempFile: $($tempFile)"
Out-File -FilePath $tempFile -Encoding 'Ascii' -InputObject $somevar
$somevar = Get-Content -Path $tempFile -Encoding 'Ascii'

NB: Out-File with Encoding 'UTF-8' adds BOM record at the beginning. To avoid this use [System.IO.File]::WriteAllText($path,$data,[System.Text.Encoding]::UTF8)

Sign up to request clarification or add additional context in comments.

Comments

1

I'm not sure I well understood the question, but it seems you are trying to initialize variable this way [string]$out;. In fact you are only printing the $out variable (which you never used before) as a string, so you don't get the result you expect by using this piece of code $out += to add something to a string.

Just try change this [string]$out; to this $out = ""; and you should be able to run your code correctly (now $out is a String).

6 Comments

I have amended the usage example slightly. In essence I call the function with a string and split it in intervals defined in the functions input array. I use the out variable to build up the delimited string - this is being assigned to and the output is correct however the start/end of the output has unexpected carriage return/line feed chars.
What you get if you try to save the returned value into a variable, let call it $a, and then you call $a.GetType().fullname? Do you Get System.Object[] or System.String?
very strange, if I try to execute it I get a System.Object[]
Is that not the base type?
("a").GetType().FullName -> System.String @("a").GetType().FullName -> System.Object[] I get a System.Object[] as output variable from your function, if you get a System.String maybe we are looking at a different piece of code ;)
|

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.