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













3












$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.










share|improve this question









New contributor



Chelsea Kim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$











  • $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




    $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
















3












$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.










share|improve this question









New contributor



Chelsea Kim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$











  • $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




    $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














3












3








3





$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.










share|improve this question









New contributor



Chelsea Kim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$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






share|improve this question









New contributor



Chelsea Kim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question









New contributor



Chelsea Kim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question








edited May 17 at 14:58









m_goldberg

90.3k873203




90.3k873203






New contributor



Chelsea Kim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked May 17 at 14:27









Chelsea KimChelsea Kim

161




161




New contributor



Chelsea Kim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




Chelsea Kim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • $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




    $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






  • 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











2 Answers
2






active

oldest

votes


















8












$begingroup$

Some things:



  1. For-loops are for effect; they always return Null, which doesn't print.

  2. Your For-loop is mal-formed.

  3. Mathematica arrays are 1-based, not 0-based.

  4. 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.



multi_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.



one_cell






share|improve this answer











$endgroup$




















    2












    $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]:=





    share|improve this answer











    $endgroup$








    • 2




      $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







    • 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 use Composition although it can be useful.
      $endgroup$
      – Somos
      May 17 at 18:12












    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.









    draft saved

    draft discarded


















    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









    8












    $begingroup$

    Some things:



    1. For-loops are for effect; they always return Null, which doesn't print.

    2. Your For-loop is mal-formed.

    3. Mathematica arrays are 1-based, not 0-based.

    4. 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.



    multi_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.



    one_cell






    share|improve this answer











    $endgroup$

















      8












      $begingroup$

      Some things:



      1. For-loops are for effect; they always return Null, which doesn't print.

      2. Your For-loop is mal-formed.

      3. Mathematica arrays are 1-based, not 0-based.

      4. 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.



      multi_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.



      one_cell






      share|improve this answer











      $endgroup$















        8












        8








        8





        $begingroup$

        Some things:



        1. For-loops are for effect; they always return Null, which doesn't print.

        2. Your For-loop is mal-formed.

        3. Mathematica arrays are 1-based, not 0-based.

        4. 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.



        multi_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.



        one_cell






        share|improve this answer











        $endgroup$



        Some things:



        1. For-loops are for effect; they always return Null, which doesn't print.

        2. Your For-loop is mal-formed.

        3. Mathematica arrays are 1-based, not 0-based.

        4. 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.



        multi_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.



        one_cell







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 17 at 20:06

























        answered May 17 at 15:09









        m_goldbergm_goldberg

        90.3k873203




        90.3k873203





















            2












            $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]:=





            share|improve this answer











            $endgroup$








            • 2




              $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







            • 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 use Composition although it can be useful.
              $endgroup$
              – Somos
              May 17 at 18:12
















            2












            $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]:=





            share|improve this answer











            $endgroup$








            • 2




              $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







            • 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 use Composition although it can be useful.
              $endgroup$
              – Somos
              May 17 at 18:12














            2












            2








            2





            $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]:=





            share|improve this answer











            $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]:=






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 17 at 17:47

























            answered May 17 at 17:31









            SomosSomos

            2,2351111




            2,2351111







            • 2




              $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







            • 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 use Composition although it can be useful.
              $endgroup$
              – Somos
              May 17 at 18:12













            • 2




              $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







            • 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 use Composition 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











            Chelsea Kim is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

            Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

            Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form