What is the true value?

  • Comments posted to this topic are about the item What is the true value?

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • Good question. I like all those "Param ($databaseName)" red herrings. As my powerscript knowledge is pretty close to nil, they made me struggle to understand what was going on.

    I didn't actually know enough about powershell to answer from knowledge, but it's actually pretty easy to deduce which result is corect from simple priniples of programming language design. Basically the language's syntax has to be coherent, and represent some fairly rational semantics (excluding languages deliberately designed not to have those properties, which are always recreational languages not languages for serious use like powershell). The single return statement gave me a clue - presumably every other function returned nothing so assigning the function value set the variable to null except for that single function which returned something. The functions seemed to have three posible behaviours: printwhat the outer scope variable was at call time (that wasn't it as there were not enough blanks which presumably represented nulls), use the outer scope variable but possibly overwrite it first (if so the fifth and sixth calls had to print the same and none of the options had that) or use the outer scope variable unless they created an inner scope variable and used it (that did fit with one of the options, which thus had to be the right option). Having gone through that the hard way, I realised I could have reached the same result much faster by noticing the single return statement and then looking only at blanks (nulls). But it was fun mental excercise anyway (even though it took me quite a while to realize that those "Param" lines could have no effect whatever in this script - and that left me wondering what a "Param" line actually means, which I couldn't deduce from this script as they have no meaning here).

    Tom

  • Thank you for the question. Nice to see a question on PowerShell. By the way what cool things can you do with PS ? I have not used it yet.

  • I also have no knowledge about powershell script. I have saved the script as.ps1 and executed it in command prompt like mentioned below.

    powershell -ExecutionPolicy ByPass -File c:\Document1.ps1

    (I could not run the file directly. I had to bypass the executionpolicy )

    Got the result. 🙂

  • This was removed by the editor as SPAM

  • Stewart "Arturius" Campbell (7/14/2014)


    Good question, thanks Gary.

    this was a good metal exercise this early on a Monday morning.

    A little too much exercise for me on a Monday morning. Missed that the value passed to the last statement would come from the value set in the third statement.

    Nice question!

    [font="Verdana"]Please don't go. The drones need you. They look up to you.[/font]
    Connect to me on LinkedIn

  • Gary Varga (7/13/2014)


    Comments posted to this topic are about the item <A HREF="/questions/powershell/112950/">What is the true value?</A>

    This is a little OT but here goes! I'm learning F# and among other things using it to replace PowerShell scripts -- mostly as an exercise for me but partially because I'm coming to believe the F# just does these things better. Here's my attempt at the same script in F#:

    let mutable databaseName = "DB1"

    let CheckConnection1 () =

    printfn "%s" databaseName

    ""

    let CheckConnection2 databaseName =

    printfn "%s" databaseName

    ""

    let CheckConnection3 () =

    match databaseName with

    | "" -> databaseName <- "DB3"

    | x -> databaseName <- x

    printfn "%s" databaseName

    databaseName

    let CheckConnection4 () =

    let databaseName = "DB4"

    printfn "%s" databaseName

    let CheckConnection5 databaseName =

    let databaseName_scoped = "DB5"

    printfn "%s" databaseName_scoped

    ()

    let CheckConnection6 databaseName =

    printfn "%s" databaseName

    databaseName <- CheckConnection1() // databaseName

    databaseName <- CheckConnection2 databaseName

    databaseName <- CheckConnection3()

    CheckConnection4() // databaseName

    CheckConnection5 databaseName

    CheckConnection6 databaseName

    Note that I had to declare that databaseName is mutable for this to work at all. Normally you wouldn't do that. Also, note that F# won't let me pass the parameter to functions defined without parameters. Part of the beauty and type safety of F#.

    BTW if you want to test this for yourself, Fire up FSI from Visual Studio or, if you don't have VS, download FSI from Fsharp.Org.

  • Excellent question, thanks, Gary! Variable scoping is something that can easily foil many people, especially with tricky functions like this.

    I, too, like TomThomson, have no PowerShell experience, except for reading a few articles on SQLServerCentral.com! I was, however, able to figure it out (after a few times through and coming up with combinations that didn't match any of the options!) by carefully watching the variable scoping based upon my VB and C# experience.

    The only minor quibble that I have is that Function CheckConnection1 and Function CheckConnection2 appeared twice, so I was initially expecting an error message to be part of one of the options. Since there were no error messages in the options, and both versions of the functions were identical, I figured they must be just copy-and-paste errors (which happens to everyone:-)).

  • Thanks for the question. It'd be nice to see more PoSH questions.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • gbritton1 (7/14/2014)


    Gary Varga (7/13/2014)


    Comments posted to this topic are about the item <A HREF="/questions/powershell/112950/">What is the true value?</A>

    This is a little OT but here goes! I'm learning F# and among other things using it to replace PowerShell scripts -- mostly as an exercise for me but partially because I'm coming to believe the F# just does these things better. Here's my attempt at the same script in F#:

    let mutable databaseName = "DB1"

    let CheckConnection1 () =

    printfn "%s" databaseName

    ""

    let CheckConnection2 databaseName =

    printfn "%s" databaseName

    ""

    let CheckConnection3 () =

    match databaseName with

    | "" -> databaseName <- "DB3"

    | x -> databaseName <- x

    printfn "%s" databaseName

    databaseName

    let CheckConnection4 () =

    let databaseName = "DB4"

    printfn "%s" databaseName

    let CheckConnection5 databaseName =

    let databaseName_scoped = "DB5"

    printfn "%s" databaseName_scoped

    ()

    let CheckConnection6 databaseName =

    printfn "%s" databaseName

    databaseName <- CheckConnection1() // databaseName

    databaseName <- CheckConnection2 databaseName

    databaseName <- CheckConnection3()

    CheckConnection4() // databaseName

    CheckConnection5 databaseName

    CheckConnection6 databaseName

    Note that I had to declare that databaseName is mutable for this to work at all. Normally you wouldn't do that. Also, note that F# won't let me pass the parameter to functions defined without parameters. Part of the beauty and type safety of F#.

    BTW if you want to test this for yourself, Fire up FSI from Visual Studio or, if you don't have VS, download FSI from Fsharp.Org.

    Sorry about the copy and paste error.

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • Glad you guys liked the question. It was my first but I'll try and submit more (and with less errors - I answer these too and hope for as error free questions as those that submit them can manage!!!).

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!

  • Thanks Gary, good question and nice to expose people to Powershell. I have used Powershell extensively on Sharepoint and it is such a fantastic utility, easy to use and powerful. I haven't got so deeply into it with SQL Server yet, but that will come.

    If anyone is into using Powershell, I highly recommend PowerGUI - it's free and an extremely useful tool for writing scripts.

    Sean

  • I am using PowerShell but it still took me over 15 minutes. Thanks for an interesting one, Gary!

  • Fist powershell QotD, thanks.

    Igor Micev,My blog: www.igormicev.com

  • I had no clue. Thanks for the introduction to PowerShell! 🙂

Viewing 15 posts - 1 through 15 (of 16 total)

You must be logged in to reply to this topic. Login to reply