Hi I would to print a string while adding the dots to the end rather than reprinting the string every time before it prints out the string again and again. I want it to print but only adding the dots to the already printed out string.
reboot = '### rebooting the mmp';
display(reboot)
for i = 1 : 15
reboot = strcat(reboot,'.')
pause(1);
end
How would i do this?
Rather than printing out the entire string every time, you can just print out a new dot each time through the loop.
To make this work, you'll want to use fprintf to print the dot rather than disp since disp will automatically append a newline to the end and fprintf will not so all of the dots end up on the same line.
% Print the initial message without a trailing newline
fprintf('### rebooting the mmp');
% Print 5 dots all on the same line with a 1-second pause
for k = 1:5
fprintf('.')
pause(1)
end
% We DO want to print a newline after we're all done
fprintf('\n')
fprintf(reboot)
for i=1:15
fprintf('.')
pause(1)
end
Related
Input:
<--TD: 2. Set inputs 'i_open1' = "0" and 'i_open2' = "0" and i_cntrl_pwr_down>
I want to check for last space of the line and split it from last space.
Expected output:
<--TD: 2. Set inputs 'i_open1' = "0" and 'i_open2' = "0" and
i_cntrl_pwr_down >
How can I do that in Perl?
The question is a tad unintelligible. Expected output adds an extra space before the >, which I think is just a misprint. This is my suggestion anyway:
my $str=q(<--TD: 2. Set inputs 'i_open1' = "0" and 'i_open2' = "0" and i_cntrl_pwr_down>);
$str =~ s/(.*) /$1\n /;
print $str;
Try it online!
i am simply count total no of character .
$str_len = length($line3);
if($str_len > 72){}
then i am simply searching for last charcter ending with d and space and chacter starting with i (d i)
if((#split_line = ~/[\s+.]+|_$/) and #split_line=~ /\d$|^ i/ ){
n then unpacking it
but its not a correct way to do it .
I am reading a file using fileread() which returns me the entire file. Now I need to read line by line and convert them into process the data. Can I know how I would be able to detect the newline character in Matlab? I tried '\n' and '\r\n' and it doesn't work.
Thanks in advance
For special acharacters either use the char function with the character code (http://www.asciitable.com/) or sprintf (my preferred way for better readability.
For example you are looking for sprintf('\n') or sprintf('\r\n')
char(13) is carriage return \r
char(10) is new line \n
You can read the file line by line (see fgetl):
fid = fopen ( 'file', 'r' );
% Check that it opened okay
if fid ~= -1
while ( true )
line = fgetl ( fid );
% Check for end of file
if line == -1; break; end
%Do stuff with line;
end
fclose ( fid );
end
This is a extract from my code :
foreach my $nouveau_code (#tableau_codes_utilises) {
my $code_found = 0;
foreach my $ligne_code_prospect (#tableau_liste_codes_prospects) {
//SOME CODE
}
print "HERE IS $nouveau_code\n";
if ( $code_found == 0 ) {
print "le code $nouveau_code n'a pas été trouvé\n";
STDOUT->autoflush;
}
}
And the result of an iteration where $code_found is equal to 0 is (code is hidden here, but is alphanumeric) :
HERE IS PAR****
n'a pas été trouvé
I do not understand why the second print (in the if sequence) does not print the whole line. I guess there is a buffering problem but I cannot find what is going on.
Any help would be appreciated.
At a guess, $nouveau_code has come from a file that originated on a Windows system and you are processing it on Linux?
If you use chomp in such a circumstance then it will remove the trailing newline, but not the carriage return, and printing the value will reset the screen cursor to the beginning of the line before printing anything else
The simple fix is to replace chomp with s/\R//, which will remove the CR as well as the LF on the end of the line
I suspect what is happening here is that your second data item contains backspaces (BS) or a carriage return (CR). So
your first item runs and prints out the first time, but since code_found=1 it doesn't go into the if.
your second item runs and the CR or BS in the data obscure the fact that the HERE IS was printed
your second item does not have a code found so the second print happens and the first part of it is overwritten by the same CR or BS sequence in your variable
The easy way to confirm this is to redirect your output and look at it like so:
script > check.log
vim check.log
and you should see the extra data in there. less would work fine in place of vim also.
I want to know that how to get the last printed text in QBasic.
Like if the program prints several lines, then how to get the last line printed.
Like this-
Print "aaaaaaa"
Print "bbbbbbb"
Then the program will get the last printed line i.e. bbbbbbb
Something like this maybe?
str$ = "aaaaaaa"
PRINT str$
str$ = "bbbbbbb"
PRINT str$
PRINT "last printed line:"; str$
Alternatively, as explained here, you can retrieve characters from screen memory by using PEEK at segment &HB800 , so something like this
DEF SEG = &HB800
mychar = PEEK(1) 'etc
You'd have to keep track of on which line was last printed to know where exactly you need to PEEK, so that will probably get very complicated very quickly...
For that reason I recommend you to rethink what it is exactly that you are trying to accomplish here because "screen scraping" like this is usually just a bad idea.
Given that last printed string did not end with semicolon, this code should do the trick:
FOR char.num = 1 TO 80
last.line$ = last.line$ + chr$(SCREEN(CSRLIN - 1, char.num))
NEXT char.num
PRINT "Content of last line printed to is:"; last.line$
Explanation: CSRLIN returns the current line of the cursor. SCREEN(y, x) returns the ascii-code of the character at y, x position (line, row) on the screen. Each time a string not ending with semicolon is printed to the screen, it is printed on the current line (y position) of the cursor, which is then incremented by one.
I realise this question already has an accepted answer but I have my own solution where instead of trying to work out what PRINT last PRINTed you instead use you own PRINT SUB in this example MYPRINT. While its not perfect and it only takes strings (hence STR$(123) and uses SHARED variables which are not necessarily advisable it is nicer than poking in the memory.
DECLARE SUB MYPRINT (text$)
DIM SHARED lastprint$
MYPRINT ("Hello, World!")
MYPRINT (STR$(123))
MYPRINT ("Hi Again")
MYPRINT (lastprint$)
SUB MYPRINT (text$)
PRINT (text$)
lastprint$ = text$
END SUB
The output:
Hello, World!
123
Hi Again
Hi Again
My program (which happens to be in Perl, though I don't think this question is Perl-specific) outputs status messages at one point in the program of the form Progress: x/yy where x and yy are a number, like: Progress: 4/38.
I'd like to "overwrite" the previous output when a new status message is printed so I don't fill the screen with status messages. So far, I've tried this:
my $progressString = "Progress\t$counter / " . $total . "\n";
print $progressString;
#do lots of processing, update $counter
my $i = 0;
while ($i < length($progressString)) {
print "\b";
++$i;
}
The backspace character won't print if I include a newline in $progressString. If I leave out the newline, however, the output buffer is never flushed and nothing prints.
What's a good solution for this?
Use autoflush with STDOUT:
local $| = 1; # Or use IO::Handle; STDOUT->autoflush;
print 'Progress: ';
my $progressString;
while ...
{
# remove prev progress
print "\b" x length($progressString) if defined $progressString;
# do lots of processing, update $counter
$progressString = "$counter / $total"; # No more newline
print $progressString; # Will print, because auto-flush is on
# end of processing
}
print "\n"; # Don't forget the trailing newline
Say
$| = 1
somewhere early in your program to turn autoflushing on for the output buffer.
Also consider using "\r" to move the cursor back to the beginning of the line, rather than trying to explicitly count how many spaces you need to move back.
Like you said, don't print out a newline while your progress counter is running or else you will print out your progress on a separate line instead of overwriting the old line.
I know it's not quite what you asked for, but possibly better. I happened on this same problem and so rather than deal with it too much went to using Term::ProgressBar which looks nice too.
You can also use the ANSI escape codes to directly control the cursor. Or you can use Term::ReadKey to do the same thing.
I had to tackle something similar to this today.
If you don't mind reprinting the entire line, you could do something like this:
print "\n";
while (...) {
print "\rProgress: $counter / $total";
# do processing work here
$counter++;
}
print "\n";
The "\r" character is a carriage return-- it brings the cursor back to the beginning of the line. That way, anything you print out overwrites the previous progress notification's text.