Stream: helpdesk (published)

Topic: Assign Table variable name in Furncition


view this post on Zulip brett knoss (May 30 2021 at 00:32):

function d(x,y)
        x=3
       y=8
end

d(dog,cat)

This doesn't recognize dog as a variable
how do I get dog=3, cat=8
?

view this post on Zulip Brenhin Keller (May 30 2021 at 00:38):

dog = 3; cat = 8?
or if you want to be fancy

dog, cat = 3, 8

view this post on Zulip Brenhin Keller (May 30 2021 at 00:39):

But whether or not that's something you want to to do maybe depends on what you're ultimately trying to accomplish

view this post on Zulip Mason Protter (May 30 2021 at 01:03):

It sounds like you're trying to run a function inside out. That's now how the data flows. When you write d(dog, cat), julia looks up what values are associate with the variables dog and cat and then passes those variables into the function d.

Inside the function, you then bind those values to the arguments, x and y, but then inside the function body you then rebind x and y to 3 and 8, completely ignoring whatever values were passed in

view this post on Zulip Mason Protter (May 30 2021 at 01:07):

If you want d to assign the values 3 and 8 to new variables dog and cat, then you can't do that with a function, but you can do it using a macro.

julia> macro d(x, y)
           ex = quote
               $x = 3
               $y = 8
           end
           esc(ex)
       end

@d (macro with 1 method)

julia> @d(dog, cat)
8

julia> dog + cat
11

view this post on Zulip brett knoss (May 30 2021 at 01:30):

That works, but apparently I can't do the same thing in a dataframes, or maybe I'm trying to send data both ways

macro transaction(newdate,newcredit,credit_amount,newdebit, debit_amount)
    if credit_amount != debit_amount
        "Error--Debit and Credit must match"
    else
        ex= quote

            newtransaction = DataFrame(
            $newdate=[newdate]
            $newcredit = [credit_amount],
            $newdebit = [debit_amount])
        end
        esc(ex)
    end
end

view this post on Zulip Mason Protter (May 30 2021 at 01:34):

Macros are not like functions. They just operate on syntax.

view this post on Zulip brett knoss (May 30 2021 at 01:36):

Ok, so do I then need to somehow have a macro and a function?

view this post on Zulip Mason Protter (May 30 2021 at 01:38):

I would recommend just writing a function and no macro.

view this post on Zulip Mason Protter (May 30 2021 at 01:39):

And then just writing

new_transaction = transaction(newdate,newcredit,credit_amount,newdebit, debit_amount)

instead of trying to write a macro

view this post on Zulip Mason Protter (May 30 2021 at 01:41):

If you really want a macro for this though, I guess you could write

julia> using DataFrames

julia> macro transaction(newdate,newcredit,credit_amount,newdebit, debit_amount)
           ex= quote
               if $credit_amount != $debit_amount
                   "Error--Debit and Credit must match"
               else
                   newtransaction = $DataFrame(
                       newdate=[$newdate],
                       newcredit = [$credit_amount],
                       newdebit = [$debit_amount]
                   )
               end
           end
           esc(ex)
       end
@transaction (macro with 1 method)

julia> @transaction(1, 2, 3, 4, 3)
1×3 DataFrame
 Row  newdate  newcredit  newdebit
      Int64    Int64      Int64
─────┼──────────────────────────────
   1        1          3         3

view this post on Zulip brett knoss (May 30 2021 at 01:52):

macro transaction(newdate,newcredit,credit_amount,newdebit, debit_amount)
           ex= quote
               if $credit_amount != $debit_amount
                   "Error--Debit and Credit must match"
               else
                   newtransaction = $DataFrame(
                       Date=[$newdate],
                       $newcredit = [4],
                       $newdebit = [4]
                   )
               end
           end
           esc(ex)
       end
@transaction("Jan 1, 1900", chequing, 3, personal, 3)

won't let me add a variable, but I'm not sure which one.
wo


Last updated: Dec 28 2024 at 04:38 UTC