header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Extra array element in function return
Last Post 17 Dec 2008 12:37 PM by blue_fish. 4 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
blue_fishUser is Offline
New Member
New Member
Posts:4
Avatar

--
16 Dec 2008 06:28 PM
    Hi all,

    First, I am in the process of converting to Powershell from another scripting language (not a commonly used one I might add) so please bear with me if this is a trivial question.

    I have written a function (get-words) that takes a string and tokenises it into individual words using one or more blanks as the delimiter. I'm sure there is a more elegant way of doing it than I have created but the solution should work even if it is a bit clunky.

    The function returns an array of strings where each array element is 1 word. The function seems to work fine until it returns the array to the caller. After returning an array to the caller, the element counts has increased by 1 and the first element is now blank!

    For example from the caller:

    $returnedArray = get-words "abcd efg       hij "

    would give something like this:

    $returnedArray[0] = blank
    $returnedArray[1] = "abcd"
    $returnedArray[2] = "efg"
    $returnedArray[3] = "hij"
    and $returnedArray.count = 4

    but from within the function:

    function get-words ([string] $aString)
    {
      ... some processing to split the string
       $returnArray
    }
      
    you would get:
    $returnArray[0] = "abcd"
    $returnArray[1] = "efg"
    $returnArray[2] = "hij"
    and $returnArray.count = 3 


    Can anyone offer a reason for this mysterious extra array element that appears after the function call. It's very frustrating when you know you can do the same thing in another language and get the result you expect!

    Many thanks, Innes
    glnsizeUser is Offline
    Basic Member
    Basic Member
    Posts:193

    --
    16 Dec 2008 09:20 PM
    Without reading your code verbatim I would have to guess that you have a leading space/line break/hidden char in the string. 

    Fortunately though your situation is one that lends it self to powershell.  mainly because you really don't need a "script" at all.

    (" abcd     efg     h   i j ").split(" ") | where-object {$_ -ne " "}

    On a side note your post led me down a rabbit hole as to why it all worked. ... so thanks!

    ~Glenn
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    16 Dec 2008 11:00 PM
    The split() method takes a StringSplitOptions parameter which you can use to discard empty elements:

    PS > "abcd efg hij ".split(" ",[StringSplitOptions]::RemoveEmptyEntries)
    abcd
    efg
    hij

    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    glnsizeUser is Offline
    Basic Member
    Basic Member
    Posts:193

    --
    17 Dec 2008 04:19 AM

    Shay strikes again, I knew there was a way to do it with one command, but i just couldn't remember it last night.... That sir is why your the MVP!

    blue_fishUser is Offline
    New Member
    New Member
    Posts:4
    Avatar

    --
    17 Dec 2008 12:37 PM
    Guys,

    thanks for your replies. I knew there would be an elegant way to do what I wanted in Powershell and I wasn't disappointed. It's wicked that you can compress a dozen lines of code into a single command to do what you need.

    However it still didn't solve my problem of the extra array element in the emitted output from the function call. After a good nights sleep and Glenn's tip off I found my problem in the function. Here's where I went wrong. In my function I wanted to specifically declare an empty string variable, which I did like so: [string] $word Ooops! This inadvertantly emitted my extra element. What I should have done to create my empty variable was: $word = [string] and with that change my function did exactly what I expected.

    Despite not needing my overblown get-words function now I've learnt some valuable Powershell lessons from having gone through the whole process.

    Cheers, Innes
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer