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













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











$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











$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











$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















share|improve this question













share|improve this question




share|improve this question








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 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
    );



    );













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




        90.4k873203





















            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,4151111




            2,4151111







            • 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


















            draft saved

            draft discarded
















































            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

            Wikipedia:Vital articles Мазмуну Biography - Өмүр баян Philosophy and psychology - Философия жана психология Religion - Дин Social sciences - Коомдук илимдер Language and literature - Тил жана адабият Science - Илим Technology - Технология Arts and recreation - Искусство жана эс алуу History and geography - Тарых жана география Навигация менюсу

            Bruxelas-Capital Índice Historia | Composición | Situación lingüística | Clima | Cidades irmandadas | Notas | Véxase tamén | Menú de navegacióneO uso das linguas en Bruxelas e a situación do neerlandés"Rexión de Bruxelas Capital"o orixinalSitio da rexiónPáxina de Bruselas no sitio da Oficina de Promoción Turística de Valonia e BruxelasMapa Interactivo da Rexión de Bruxelas-CapitaleeWorldCat332144929079854441105155190212ID28008674080552-90000 0001 0666 3698n94104302ID540940339365017018237

            What should I write in an apology letter, since I have decided not to join a company after accepting an offer letterShould I keep looking after accepting a job offer?What should I do when I've been verbally told I would get an offer letter, but still haven't gotten one after 4 weeks?Do I accept an offer from a company that I am not likely to join?New job hasn't confirmed starting date and I want to give current employer as much notice as possibleHow should I address my manager in my resignation letter?HR delayed background verification, now jobless as resignedNo email communication after accepting a formal written offer. How should I phrase the call?What should I do if after receiving a verbal offer letter I am informed that my written job offer is put on hold due to some internal issues?Should I inform the current employer that I am about to resign within 1-2 weeks since I have signed the offer letter and waiting for visa?What company will do, if I send their offer letter to another company