6
$\begingroup$

I am using Wolfram script with a Mac OS. I have a long while loop dependent on the length of a list. i.e.:

myList = {}
While[ Length[myList] < 100, myList = AppendTo[myList, 0]]

How do I get a progress bar to appear in my terminal during this while loop?

$\endgroup$
3
  • $\begingroup$ That code does not make sense because myList is not defined, While does not have a test and a body separated by coma and if you put the coma that loop is infinite cause you are not increasing the length of myList. $\endgroup$
    – Fortsaint
    Commented Jan 26, 2020 at 14:36
  • $\begingroup$ It's an example, the point of the question is about the progress bar. "Corrected code": myList = {}; While[ Length[myList] < 100, myList += {0,0}] $\endgroup$
    – PHPHacky
    Commented Jan 26, 2020 at 18:01
  • $\begingroup$ Still an infinite loop. $\endgroup$
    – Fortsaint
    Commented Jan 26, 2020 at 18:39

1 Answer 1

3
$\begingroup$

This solution is specific to Linux

k=1; 

While[ k<100, 

       k++; [email protected]; 

      (* progress indicator *) 
       Run[ "echo -n '" <> 
            StringRepeat["|",k] <>  
            StringRepeat["-", 100-k] <> 
            "\r'"]
     ]

but you might find a command line that overwrites previous outputs like echo -n ' \r' for other systems too.

$\endgroup$
6
  • $\begingroup$ hmm wolfram script doesn't seem to understand the return carriage. It prints on a new line every iteration. BTW I'm using a UNIX system so it should work $\endgroup$
    – PHPHacky
    Commented Jan 26, 2020 at 21:27
  • $\begingroup$ It is not wolfram that do not understand the return carriage, it is the shell of your operating system. As I wrote in the answer, you need to find command line that works for your OS. $\endgroup$
    – Fortsaint
    Commented Jan 28, 2020 at 14:48
  • $\begingroup$ Hmm using the above code and substituting the Run command for a WriteString command I get the expected output. This solution works for bash shells on Linux OS $\endgroup$
    – PHPHacky
    Commented Jan 29, 2020 at 15:19
  • $\begingroup$ @PHPHacky Great. I am on Debian 9 and only Run gives me the expected behaviour, not WriteString, What OS is yours? $\endgroup$
    – Fortsaint
    Commented Jan 29, 2020 at 15:25
  • $\begingroup$ macOS (which is Unix based) but I tested this on Linux as well. What shell are you using? $\endgroup$
    – PHPHacky
    Commented Jan 29, 2020 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.