Thank you in advance. I am writing a short program, a game, that tests your knowledge of GNU/Linux commands and programs (with a whatis entry). I want it to put like flash cards: present the name of the command, wait for the user, then print the description, wait for the user, ad infinitum. The code below shows the first attempt (SLATHERED WITH COMMENTS):
#!/usr/bin/perl #bashflash #tests your memory of bash/gnu-linux commands and cli programs #USAGE: #cd to the directory containing the binarys of the programs #you want to test your knowledge of (DO NOT NEED TO BE ROOT) #pass perl bashflash -`whatis --wildcard *` # use strict; use warnings; #The following loop utilizes a shift-and-push loop #to shift the zeroth element of @ARGV out of the array, into a scalar, #then push the scalar back into the array as the final element. #effectively, this process lets one cycle through the output of #`whatis --wildcard *` indefinitely, *while separating the #command names from their descriptions, like flashcards.* #*In it's current implementation, @ARGV stores the output of `whatis` #word by word: such that elements are delineated by spaces. my $currentcard; #this variable holds onto strings shifted off of @ARG +V #, prints them, then pushes it's contents #back onto the end of ARGV while (1) { #infinite loop START $currentcard = shift; #shift first element #of @ARGV into $currentcard print $currentcard; push (@ARGV, $currentcard); #push $currentcard #(previously element 0) back onto #@ARGV as the last element <STDIN>; #Wait for user } #infinite loop END exit; #the script never gets here, #but I always explicitly exit on the end of my Perl. #and then an empty comment line #
This program puts this to STDOUT:
sage@bash:/bin$ perl bashflash -`whatis --wildcard *` bashflash: nothing appropriate. kmod: nothing appropriate. ntfsck: nothing appropriate. ntfsdump_logfile: nothing appropriate. ntfsmftalloc: nothing appropriate. ntfsmove: nothing appropriate. ntfstruncate: nothing appropriate. ntfswipe: nothing appropriate. plymouth: nothing appropriate. plymouth-upstart-bridge: nothing appropriate. running-in-container: nothing appropriate. static-sh: nothing appropriate. -bash (1) - GNU Bourne-Again SHell bunzip2 (1) - a block-sorting file compressor, v1.0.6 busybox (1) - The Swiss Army Knife
Note, each line after "-bash" had to be summoned by the return key, meaning that (as explained in the comments) @ARGV has stored the command line argument into new elements by word. (This is my current understanding, please correct me.)
#1, the negative testing while
my @currentcard; #this variable holds onto strings shifted off of @ARG +V #, prints them, then pushes it's contents #back onto the end of ARGV while (1) { #infinite loop START $_ = shift; while (m#^(?!-\s)$#) { push (@currentcard, $_); $_ = shift; } print @currentcard; push (@ARGV, @currentcard); #push $currentcard #(previously element 0) back onto #@ARGV as the last element <STDIN>; #Wait for user } #infinite loop END
my @currentcard; #this variable holds onto strings shifted off of @ARG +V #, prints them, then pushes it's contents #back onto the end of ARGV while (1) { #infinite loop START $_ = shift; my $witch = 1; while ($witch) { push (@currentcard, $_); $_ = shift; if ($_ =~ m#^(-\s)$#) { --$witch; } } print @currentcard; push (@ARGV, @currentcard); #push $currentcard #(previously element 0) back onto #@ARGV as the last element <STDIN>; #Wait for user } #infinite loop END
Above: an attempt, two of many, to get the "flash card" output I desire. My plan is to instead push the shifted elements of @ARGV onto an Array in a regexp-conditional loop, to attempt to match the "- " in the last element of Array, if it does match "- ", then the code should exit the loop and print everything it collected up to and including the "- ", then pushing it's content back onto the end of @ARGV, and waiting for the return key press. Or the logical equivalent, test positive for iteration with the lack of "- ", and keep adding to Array until shift adds a "- ". As above.
I have tried dozens of variations with positive and negative regular expressions, I even tried /.*/, but every time I try to group the output such that iterations of print are delimited by "- ", just I get blank lines.
Am I using push and shift incorrectly?
Prithee, great archons of perl, behelpeth me.
In reply to Seeking regexp @ARGV arrays Wisdom by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |