Group Abstract Group Abstract

Message Boards Message Boards

1
|
6.6K Views
|
6 Replies
|
1 Total Like
View groups...
Share
Share this post:

What is the difference between << and Get

POSTED BY: Henrick Jeanty
6 Replies

See the note in the documentation page for Get that says

<<"name" is equivalent to <<name. The double quotes can be omitted if the name contains only alphanumeric characters and the characters `, /, ., ∖, !, -, _, :, $, *, ~, and ?, as described in greater detail in "Operator Input Forms".

POSTED BY: Jason Biggs

Thank you Jason. Your explanation makes it clear. However I now think that it is really confusing to have <<"name" equivalent to <<name What if I want to change the filename and put it in a variable so that << name could output to one file at one point and then, after doing name = "otherfilename", I want to again do << name?

I guess I will have to use the Get[filename] approach all the time.

Thank you Jason. Now to find out how to mark your reply as the definitive answer.

POSTED BY: Henrick Jeanty
Posted 5 years ago

Hi Henrick,

This works fine

ClearAll[x, a];
a = Sin[x];

Put[a, "foo"]

ClearAll[x, a]
a = << "foo"
(* Sin[x] *)

ClearAll[x, a]
foo = "foo";
a = << foo
(* Sin[x] *)

Please provide a specific example that demonstrates the failure. Also please use the code formatting button to format code. Read the "Code Formatting" section here.

POSTED BY: Rohit Namjoshi

Hello Rohit, I went ahead and tried your code and it does work. However, what I am talking about is exhibited here:

filename = "foo";

a = Get[filename]

(* Sin[x] *)

a = << filename

(* Get::noopen: Cannot open filename. *)

$Failed

I thought that a = << filename and a = Get[filename] meant the same thing. I though that << and Get were the same function. But, seemingly not.

POSTED BY: Henrick Jeanty
Posted 5 years ago

Hi Henrick,

Thanks for clarifying. That is unexpected. Seems like it might be a precedence issue since the following works

a = << (filename)
POSTED BY: Rohit Namjoshi

I thought that a = << filename and a = Get[filename] meant the same thing.

Based on the documentation for Get they are not the same thing. I would expect << filename to look for a file called "filename" somewhere on my $Path, regardless of any value attached to the variable filename.

POSTED BY: Jason Biggs
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard