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
Can I tell a prospective employee that everyone in the team is leaving?
Does French have the English "short i" vowel?
Why is the Eisenstein ideal paper so great?
What does kpsewhich stand for?
Why was this character made Grand Maester?
Is it truly impossible to tell what a CPU is doing?
Public transport tickets in UK for two weeks
Can you output map values in visualforce inline using a string key?
Can I install a back bike rack without attachment to the rear part of the frame?
Is it legal to have an abortion in another state or abroad?
How can I make an argument that my time is valuable?
How do I superimpose two math symbols?
Making a electromagnet
USPS Back Room - Trespassing?
Is superuser the same as root?
Security vulnerabilities of POST over SSL
Are there any German nonsense poems (Jabberwocky)?
What did the 'turbo' button actually do?
Python program for a simple calculator
Natural Armour and Weapons
How to melt snow without fire or body heat?
Beginner looking to learn/master musical theory and instrumental ability. Where should I begin?
The art of clickbait captions
How does the EU Emissions Trading Scheme account for increased emissions outside the EU?
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
New contributor
$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
New contributor
$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
New contributor
$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
New contributor
New contributor
edited May 17 at 14:58
m_goldberg
90.3k873203
90.3k873203
New contributor
asked May 17 at 14:27
Chelsea KimChelsea Kim
161
161
New contributor
New contributor
$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
);
);
Chelsea Kim is a new contributor. Be nice, and check out our Code of Conduct.
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.3k873203
90.3k873203
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,2351111
2,2351111
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 |
Chelsea Kim is a new contributor. Be nice, and check out our Code of Conduct.
Chelsea Kim is a new contributor. Be nice, and check out our Code of Conduct.
Chelsea Kim is a new contributor. Be nice, and check out our Code of Conduct.
Chelsea Kim is a new contributor. Be nice, and check out our Code of Conduct.
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