Stream: helpdesk (published)

Topic: ✔ global variable but within the function


view this post on Zulip Dale Black (Nov 27 2021 at 02:39):

Is there a way to make a variable inside a function a "semi" global variable? Not sure that I am using the right wording, but for example, inside a function I have

    try
        pixel_size = header[(0x0028, 0x0030)]
    catch
        FOV = header[(0x0018, 0x1100)]
        matrix_size = header[(0x0028, 0x0010)]

        pixel_size = FOV / matrix_size
    end

and I want to make pixel_size available to the rest of the function but not necessarily globally. Is there a way to do this?

view this post on Zulip Mason Protter (Nov 27 2021 at 03:44):

You should be able to just do

    local pixel_size
    try
        pixel_size = header[(0x0028, 0x0030)]
    catch
        FOV = header[(0x0018, 0x1100)]
        matrix_size = header[(0x0028, 0x0010)]

        pixel_size = FOV / matrix_size
    end

view this post on Zulip Dale Black (Nov 27 2021 at 05:03):

Ooh, simple enough. Thanks!

view this post on Zulip Notification Bot (Nov 27 2021 at 05:03):

Dale Black has marked this topic as resolved.

view this post on Zulip Mason Protter (Nov 27 2021 at 05:12):

I should also mention that my preferred method would be to simply write

    pixel_size = try
        header[(0x0028, 0x0030)]
    catch
        FOV = header[(0x0018, 0x1100)]
        matrix_size = header[(0x0028, 0x0010)]

        FOV / matrix_size
    end

view this post on Zulip Mason Protter (Nov 27 2021 at 05:12):

Because everything is an expression in Julia

view this post on Zulip Dale Black (Nov 27 2021 at 05:13):

Oh wow, that's very cool

view this post on Zulip Dale Black (Nov 27 2021 at 05:13):

I like that better too


Last updated: Nov 22 2024 at 04:41 UTC