Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6574139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:13:22+00:00 2026-05-25T15:13:22+00:00

Simple question, but I am asking just to make sure I am not overlooking

  • 0

Simple question, but I am asking just to make sure I am not overlooking an obvious
solution which can be much more efficient.

If one has large data buffer, say very large list, that needs to be updated, and
wanted to pass it to a function to do the updating inside the function as in

a = Table[0,{10}]
a = update[a]

and since I can’t use pass by reference (in a CDF, one can’t change the
Atrributes of a function to anything, such as HoldFirst), then I am forced
to make a copy of the list inside the function itself in order to update it,
and return back the copy.

My question, other than using ‘global variables’ which is not good,
is there a more efficient way to do this?

ps. about a year ago, I asked about copy by reference, here is a link
to my Mathgroup question. (Thanks to Leonid answer there btw, was useful answer).

But my question here is a little different, since now I can NOT use HoldFirst,
are there any other alternatives that I am not seeing to avoid this extra
copying of data all the time, it seems to slow down the program when
the size becomes too large.

(can’t use SetAttributes and its friends, not allowed in CDF).

I’ll show the basic example first, then show how I would do it if
I could use the HoldFirst.

Example

update[a_List] := Module[{copyOfa = a}, copyOfa[[1]] = 5; copyOfa]
a = Table[0, {10}];
a = update[a]

----> {5, 0, 0, 0, 0, 0, 0, 0, 0, 0}

If I could use the HoldFirst, I would write

update[a_] := Module[{}, a[[1]] = 5; a]
Attributes[update] = {HoldFirst};

a = Table[0, {10}];
a = update[a]

----> {5, 0, 0, 0, 0, 0, 0, 0, 0, 0}

Much more efficient, since no copying is done. Pass by reference.

I could use a global variable, as in

a = Table[0, {10}];
updateMya[] := Module[{}, a[[1]] = 5]
updateMya[];
a
----> {5, 0, 0, 0, 0, 0, 0, 0, 0, 0}

But this is of course bad programming even if is it very fast.

Since I have large data buffers, and I’d like to modularize my Mathematica code,
I need to create functions that I pass it large data to process, but at the same
time wanted to keep it ‘efficient’.

Any other options one can see to do this?

sorry if this was asked before here, hard to search SO.

thanks,

addition 1

Using Unevaluated is easy to use, but I am no longer able to use the type checking I had to make sure that a list is being passed. For example

update[a_List] := Module[{}, a[[1]] = 5; a]
a = Table[0, {10}];
a = update[Unevaluated[a]]

The call now does not ‘bind’ to the definition, since ‘a’ now does not have header List.

So, I lose some of the robustness I had in the code. But using Unevaluated does work in CDF and changing the code to use it was easy. I just had to remove those extra ‘type checking’ that I had there to make it work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:13:22+00:00Added an answer on May 25, 2026 at 3:13 pm

    The function Unevaluated has pretty much the same effect as (temporarily) setting the Attribute HoldFirst so you could do something like

    update[a_] := Module[{}, a[[1]] = 5; a]
    a = Table[0, {10}];
    a = update[Unevaluated[a]]
    

    Edit

    Concerning addition 1: you could add type checking by doing something like

    Clear[update];
    update[a_] := Module[{}, a[[1]] = 5; a] /; Head[a] == List
    

    Then

    a = Table[0, {10}];
    update[Unevaluated[a]]
    

    works as before but

    b = f[1,2,3];
    update[Unevaluated[b]]
    

    just returns the last statement in unevaluated form.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel somewhat foolish asking such a simple question, but I can't seem to
This is probably a simple question but I can't seem to find the solution.
I'm not sure if I'm even asking this question correctly. I just built my
Just a quick simple question that I can not seem to find an answer
I know, I am asking very simple and stupid question but i want to
This is a very simple question and I feel stupid for asking it, but
This is probably a simple question but I am not an ASP.NET developer and
This is probably a simple question but I can't seem to figure out how
Just to make sure everyone understands what I am asking for, I will run
Asking a simple question, just want everyone have fun to solve it. I got

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.