Print characters from list with a For-loopWhy should I avoid the For loop in Mathematica?How do I find the elements in a list that return the highest value for a function?NestList with a list inside?How to specify a specific range for a loop?How to put some values from a list into a function in a Do loopPerform For-loop and show output which meets a certain criterionHow to get Complement function working in a For loop?How to label outputs from PrintHow I can go to a new line in a nested For-loop?Iterative loop for a given listExtracting values from a list
What is the right way to float a home lab?
Is it legal in the UK for politicians to lie to the public for political gain?
Asking bank to reduce APR instead of increasing credit limit
Access to all elements on the page
Why were the Night's Watch required to be celibate?
Accidentally cashed a check twice
Show sparse matrices like chessboards
Can The Malloreon be read without first reading The Belgariad?
Rotated Position of Integers
How should I push back against my job assigning "homework"?
How to detach yourself from a character you're going to kill?
Are there practical reasons to NOT use a stepper motor with lead screw for the X and or Y axes?
Is there a practical difference between different types of Berachos?
GFCI Outlet in Bathroom, Lights not working
How to provide realism without making readers think grimdark
Concise way to draw this pyramid
Why does MS SQL allow you to create an illegal column?
Sucuri detects malware on wordpress but I can't find the malicious code
How can a single Member of the House block a Congressional bill?
Is having a hidden directory under /etc safe?
Pros and cons of writing a book review?
Is the decompression of compressed and encrypted data without decryption also theoretically impossible?
Hygienic footwear for prehensile feet?
Unconventional Opposites
Print characters from list with a For-loop
Why should I avoid the For loop in Mathematica?How do I find the elements in a list that return the highest value for a function?NestList with a list inside?How to specify a specific range for a loop?How to put some values from a list into a function in a Do loopPerform For-loop and show output which meets a certain criterionHow to get Complement function working in a For loop?How to label outputs from PrintHow I can go to a new line in a nested For-loop?Iterative loop for a given listExtracting values from a list
$begingroup$
Suppose I have the list
primary = "b","a","e"
I want to get the following output with a For-loop (or While-loop)
"b"
"a"
"e"
My code is:
For[i = 0, i < 3, Part[primary, i], i++]
but there was no Output.
functions core-language
$endgroup$
add a comment |
$begingroup$
Suppose I have the list
primary = "b","a","e"
I want to get the following output with a For-loop (or While-loop)
"b"
"a"
"e"
My code is:
For[i = 0, i < 3, Part[primary, i], i++]
but there was no Output.
functions core-language
$endgroup$
$begingroup$
For
loops andPrint
statements aren't used in Mathematica in the same way as in other languages (well, you can, but it's usually not a good idea). See here andPrint
orEcho
.
$endgroup$
– Roman
May 17 at 15:02
1
$begingroup$
Print /@ primary;
$endgroup$
– John Doty
May 17 at 15:03
3
$begingroup$
How about TableForm[primary] or Column[primary].
$endgroup$
– bill s
May 17 at 19:10
add a comment |
$begingroup$
Suppose I have the list
primary = "b","a","e"
I want to get the following output with a For-loop (or While-loop)
"b"
"a"
"e"
My code is:
For[i = 0, i < 3, Part[primary, i], i++]
but there was no Output.
functions core-language
$endgroup$
Suppose I have the list
primary = "b","a","e"
I want to get the following output with a For-loop (or While-loop)
"b"
"a"
"e"
My code is:
For[i = 0, i < 3, Part[primary, i], i++]
but there was no Output.
functions core-language
functions core-language
edited May 17 at 14:58
m_goldberg
90.4k873203
90.4k873203
asked May 17 at 14:27
Chelsea KimChelsea Kim
161
161
$begingroup$
For
loops andPrint
statements aren't used in Mathematica in the same way as in other languages (well, you can, but it's usually not a good idea). See here andPrint
orEcho
.
$endgroup$
– Roman
May 17 at 15:02
1
$begingroup$
Print /@ primary;
$endgroup$
– John Doty
May 17 at 15:03
3
$begingroup$
How about TableForm[primary] or Column[primary].
$endgroup$
– bill s
May 17 at 19:10
add a comment |
$begingroup$
For
loops andPrint
statements aren't used in Mathematica in the same way as in other languages (well, you can, but it's usually not a good idea). See here andPrint
orEcho
.
$endgroup$
– Roman
May 17 at 15:02
1
$begingroup$
Print /@ primary;
$endgroup$
– John Doty
May 17 at 15:03
3
$begingroup$
How about TableForm[primary] or Column[primary].
$endgroup$
– bill s
May 17 at 19:10
$begingroup$
For
loops and Print
statements aren't used in Mathematica in the same way as in other languages (well, you can, but it's usually not a good idea). See here and Print
or Echo
.$endgroup$
– Roman
May 17 at 15:02
$begingroup$
For
loops and Print
statements aren't used in Mathematica in the same way as in other languages (well, you can, but it's usually not a good idea). See here and Print
or Echo
.$endgroup$
– Roman
May 17 at 15:02
1
1
$begingroup$
Print /@ primary;
$endgroup$
– John Doty
May 17 at 15:03
$begingroup$
Print /@ primary;
$endgroup$
– John Doty
May 17 at 15:03
3
3
$begingroup$
How about TableForm[primary] or Column[primary].
$endgroup$
– bill s
May 17 at 19:10
$begingroup$
How about TableForm[primary] or Column[primary].
$endgroup$
– bill s
May 17 at 19:10
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
Some things:
- For-loops are for effect; they always return
Null
, which doesn't print. - Your For-loop is mal-formed.
- Mathematica arrays are 1-based, not 0-based.
- To get printed output you need to use
Print
or some other function that produces an output cell.
Taking the above into consideration, the minimal correction to your code is:
For[i = 1, i <= 3, i++, Print[Part[primary, i]]]
But here is a much better way to do the same thing.
Scan[Print, primary]
This writes the each character in own output cell.
Update
Another very simple solution you might consider is
Column[primary]
This writes each character on its own line is a single output cell.
$endgroup$
add a comment |
$begingroup$
You might try the code as in the following transcript:
Mathematica 10.2.0 for Microsoft Windows (32-bit)
Copyright 1988-2015 Wolfram Research, Inc.
In[1]:= primary = "b", "a", "e";
In[2]:= Map[ Print @ InputForm @ # &, primary];
"b"
"a"
"e"
In[3]:=
which seems to be what you want.
The InputForm
is a print format which shows the double quotes delimiting the strings. The Print @ InputForm @ # &
is an idiomatic way of defining an anonymous function which prints an exmpression using the print format. The Map[]
applies the function to all the elements of the list. As usual in Mathematica there are several variant ways to do something like what you wanted to do. One variant would be to use Scan[]
instead of Map[]
which has the advantage that the trailing ;
would not be needed. Here is another variation using Do[]
In[3]:= Do[ Print @ InputForm @ s, s , primary]
"b"
"a"
"e"
In[4]:=
$endgroup$
2
$begingroup$
A slot free version isPrint@*InputForm /@ primary
.
$endgroup$
– Carl Woll
May 17 at 17:45
$begingroup$
@Carl Woll Yes, but@*
is not yet defined in Mathemathica 10.2.
$endgroup$
– Somos
May 17 at 17:49
2
$begingroup$
Are you sure? I think it was introduced in M10, and it does work in my copy of M10.
$endgroup$
– Carl Woll
May 17 at 18:02
$begingroup$
@CarlWoll Actually, you are correct, but I almost never useComposition
although it can be useful.
$endgroup$
– Somos
May 17 at 18:12
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "387"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f198572%2fprint-characters-from-list-with-a-for-loop%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Some things:
- For-loops are for effect; they always return
Null
, which doesn't print. - Your For-loop is mal-formed.
- Mathematica arrays are 1-based, not 0-based.
- To get printed output you need to use
Print
or some other function that produces an output cell.
Taking the above into consideration, the minimal correction to your code is:
For[i = 1, i <= 3, i++, Print[Part[primary, i]]]
But here is a much better way to do the same thing.
Scan[Print, primary]
This writes the each character in own output cell.
Update
Another very simple solution you might consider is
Column[primary]
This writes each character on its own line is a single output cell.
$endgroup$
add a comment |
$begingroup$
Some things:
- For-loops are for effect; they always return
Null
, which doesn't print. - Your For-loop is mal-formed.
- Mathematica arrays are 1-based, not 0-based.
- To get printed output you need to use
Print
or some other function that produces an output cell.
Taking the above into consideration, the minimal correction to your code is:
For[i = 1, i <= 3, i++, Print[Part[primary, i]]]
But here is a much better way to do the same thing.
Scan[Print, primary]
This writes the each character in own output cell.
Update
Another very simple solution you might consider is
Column[primary]
This writes each character on its own line is a single output cell.
$endgroup$
add a comment |
$begingroup$
Some things:
- For-loops are for effect; they always return
Null
, which doesn't print. - Your For-loop is mal-formed.
- Mathematica arrays are 1-based, not 0-based.
- To get printed output you need to use
Print
or some other function that produces an output cell.
Taking the above into consideration, the minimal correction to your code is:
For[i = 1, i <= 3, i++, Print[Part[primary, i]]]
But here is a much better way to do the same thing.
Scan[Print, primary]
This writes the each character in own output cell.
Update
Another very simple solution you might consider is
Column[primary]
This writes each character on its own line is a single output cell.
$endgroup$
Some things:
- For-loops are for effect; they always return
Null
, which doesn't print. - Your For-loop is mal-formed.
- Mathematica arrays are 1-based, not 0-based.
- To get printed output you need to use
Print
or some other function that produces an output cell.
Taking the above into consideration, the minimal correction to your code is:
For[i = 1, i <= 3, i++, Print[Part[primary, i]]]
But here is a much better way to do the same thing.
Scan[Print, primary]
This writes the each character in own output cell.
Update
Another very simple solution you might consider is
Column[primary]
This writes each character on its own line is a single output cell.
edited May 17 at 20:06
answered May 17 at 15:09
m_goldbergm_goldberg
90.4k873203
90.4k873203
add a comment |
add a comment |
$begingroup$
You might try the code as in the following transcript:
Mathematica 10.2.0 for Microsoft Windows (32-bit)
Copyright 1988-2015 Wolfram Research, Inc.
In[1]:= primary = "b", "a", "e";
In[2]:= Map[ Print @ InputForm @ # &, primary];
"b"
"a"
"e"
In[3]:=
which seems to be what you want.
The InputForm
is a print format which shows the double quotes delimiting the strings. The Print @ InputForm @ # &
is an idiomatic way of defining an anonymous function which prints an exmpression using the print format. The Map[]
applies the function to all the elements of the list. As usual in Mathematica there are several variant ways to do something like what you wanted to do. One variant would be to use Scan[]
instead of Map[]
which has the advantage that the trailing ;
would not be needed. Here is another variation using Do[]
In[3]:= Do[ Print @ InputForm @ s, s , primary]
"b"
"a"
"e"
In[4]:=
$endgroup$
2
$begingroup$
A slot free version isPrint@*InputForm /@ primary
.
$endgroup$
– Carl Woll
May 17 at 17:45
$begingroup$
@Carl Woll Yes, but@*
is not yet defined in Mathemathica 10.2.
$endgroup$
– Somos
May 17 at 17:49
2
$begingroup$
Are you sure? I think it was introduced in M10, and it does work in my copy of M10.
$endgroup$
– Carl Woll
May 17 at 18:02
$begingroup$
@CarlWoll Actually, you are correct, but I almost never useComposition
although it can be useful.
$endgroup$
– Somos
May 17 at 18:12
add a comment |
$begingroup$
You might try the code as in the following transcript:
Mathematica 10.2.0 for Microsoft Windows (32-bit)
Copyright 1988-2015 Wolfram Research, Inc.
In[1]:= primary = "b", "a", "e";
In[2]:= Map[ Print @ InputForm @ # &, primary];
"b"
"a"
"e"
In[3]:=
which seems to be what you want.
The InputForm
is a print format which shows the double quotes delimiting the strings. The Print @ InputForm @ # &
is an idiomatic way of defining an anonymous function which prints an exmpression using the print format. The Map[]
applies the function to all the elements of the list. As usual in Mathematica there are several variant ways to do something like what you wanted to do. One variant would be to use Scan[]
instead of Map[]
which has the advantage that the trailing ;
would not be needed. Here is another variation using Do[]
In[3]:= Do[ Print @ InputForm @ s, s , primary]
"b"
"a"
"e"
In[4]:=
$endgroup$
2
$begingroup$
A slot free version isPrint@*InputForm /@ primary
.
$endgroup$
– Carl Woll
May 17 at 17:45
$begingroup$
@Carl Woll Yes, but@*
is not yet defined in Mathemathica 10.2.
$endgroup$
– Somos
May 17 at 17:49
2
$begingroup$
Are you sure? I think it was introduced in M10, and it does work in my copy of M10.
$endgroup$
– Carl Woll
May 17 at 18:02
$begingroup$
@CarlWoll Actually, you are correct, but I almost never useComposition
although it can be useful.
$endgroup$
– Somos
May 17 at 18:12
add a comment |
$begingroup$
You might try the code as in the following transcript:
Mathematica 10.2.0 for Microsoft Windows (32-bit)
Copyright 1988-2015 Wolfram Research, Inc.
In[1]:= primary = "b", "a", "e";
In[2]:= Map[ Print @ InputForm @ # &, primary];
"b"
"a"
"e"
In[3]:=
which seems to be what you want.
The InputForm
is a print format which shows the double quotes delimiting the strings. The Print @ InputForm @ # &
is an idiomatic way of defining an anonymous function which prints an exmpression using the print format. The Map[]
applies the function to all the elements of the list. As usual in Mathematica there are several variant ways to do something like what you wanted to do. One variant would be to use Scan[]
instead of Map[]
which has the advantage that the trailing ;
would not be needed. Here is another variation using Do[]
In[3]:= Do[ Print @ InputForm @ s, s , primary]
"b"
"a"
"e"
In[4]:=
$endgroup$
You might try the code as in the following transcript:
Mathematica 10.2.0 for Microsoft Windows (32-bit)
Copyright 1988-2015 Wolfram Research, Inc.
In[1]:= primary = "b", "a", "e";
In[2]:= Map[ Print @ InputForm @ # &, primary];
"b"
"a"
"e"
In[3]:=
which seems to be what you want.
The InputForm
is a print format which shows the double quotes delimiting the strings. The Print @ InputForm @ # &
is an idiomatic way of defining an anonymous function which prints an exmpression using the print format. The Map[]
applies the function to all the elements of the list. As usual in Mathematica there are several variant ways to do something like what you wanted to do. One variant would be to use Scan[]
instead of Map[]
which has the advantage that the trailing ;
would not be needed. Here is another variation using Do[]
In[3]:= Do[ Print @ InputForm @ s, s , primary]
"b"
"a"
"e"
In[4]:=
edited May 17 at 17:47
answered May 17 at 17:31
SomosSomos
2,4151111
2,4151111
2
$begingroup$
A slot free version isPrint@*InputForm /@ primary
.
$endgroup$
– Carl Woll
May 17 at 17:45
$begingroup$
@Carl Woll Yes, but@*
is not yet defined in Mathemathica 10.2.
$endgroup$
– Somos
May 17 at 17:49
2
$begingroup$
Are you sure? I think it was introduced in M10, and it does work in my copy of M10.
$endgroup$
– Carl Woll
May 17 at 18:02
$begingroup$
@CarlWoll Actually, you are correct, but I almost never useComposition
although it can be useful.
$endgroup$
– Somos
May 17 at 18:12
add a comment |
2
$begingroup$
A slot free version isPrint@*InputForm /@ primary
.
$endgroup$
– Carl Woll
May 17 at 17:45
$begingroup$
@Carl Woll Yes, but@*
is not yet defined in Mathemathica 10.2.
$endgroup$
– Somos
May 17 at 17:49
2
$begingroup$
Are you sure? I think it was introduced in M10, and it does work in my copy of M10.
$endgroup$
– Carl Woll
May 17 at 18:02
$begingroup$
@CarlWoll Actually, you are correct, but I almost never useComposition
although it can be useful.
$endgroup$
– Somos
May 17 at 18:12
2
2
$begingroup$
A slot free version is
Print@*InputForm /@ primary
.$endgroup$
– Carl Woll
May 17 at 17:45
$begingroup$
A slot free version is
Print@*InputForm /@ primary
.$endgroup$
– Carl Woll
May 17 at 17:45
$begingroup$
@Carl Woll Yes, but
@*
is not yet defined in Mathemathica 10.2.$endgroup$
– Somos
May 17 at 17:49
$begingroup$
@Carl Woll Yes, but
@*
is not yet defined in Mathemathica 10.2.$endgroup$
– Somos
May 17 at 17:49
2
2
$begingroup$
Are you sure? I think it was introduced in M10, and it does work in my copy of M10.
$endgroup$
– Carl Woll
May 17 at 18:02
$begingroup$
Are you sure? I think it was introduced in M10, and it does work in my copy of M10.
$endgroup$
– Carl Woll
May 17 at 18:02
$begingroup$
@CarlWoll Actually, you are correct, but I almost never use
Composition
although it can be useful.$endgroup$
– Somos
May 17 at 18:12
$begingroup$
@CarlWoll Actually, you are correct, but I almost never use
Composition
although it can be useful.$endgroup$
– Somos
May 17 at 18:12
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f198572%2fprint-characters-from-list-with-a-for-loop%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
For
loops andPrint
statements aren't used in Mathematica in the same way as in other languages (well, you can, but it's usually not a good idea). See here andPrint
orEcho
.$endgroup$
– Roman
May 17 at 15:02
1
$begingroup$
Print /@ primary;
$endgroup$
– John Doty
May 17 at 15:03
3
$begingroup$
How about TableForm[primary] or Column[primary].
$endgroup$
– bill s
May 17 at 19:10