Sonia Hamilton wrote: > $ cat program > #!/bin/bash > while IFS=: read qnumber question answer ; do > echo "Question number $qnumber" > echo "$question" > echo "$answer" > echo > done < questions.txt
Excellent work Sonia. Here's the same program in Perl. ------------------- 1: #!/usr/bin/perl -w 2: use strict; 3: 4: push @ARGV, 'questions.txt'; # optional 5: 6: while(<>) { 7: my ($qnumber, $question, $answer) = split ':'; 8: 9: print "Question number $qnumber\n$question\n$answer\n"; 10: } ------------------- To make this work over any question file, we can remove the "push" on the fourth line, and call the program with the file as an argument: program questions.txt The first line says to use Perl for the script and to turn on warnings. The second line says to use the "strict" pragma which will help identify typographical errors (eg $qmunber instead of $qnumber) and a few other common mistakes. The fourth line tells Perl that we want to add the "questions.txt" file to our argument list (or we can just pass the filename in on the command line). The sixth line tells Perl to open the files passed in on the command line and to loop through them line by line. The seventh line splits the current line on ':' and assigns the parts to variables. The 9th line prints it all out. The output and calling conventions are the same. All the best, J -- ("`-''-/").___..--''"`-._ | Jacinta Richardson | `6_ 6 ) `-. ( ).`-.__.`) | Perl Training Australia | (_Y_.)' ._ ) `._ `. ``-..-' | +61 3 9354 6001 | _..`--'_..-_/ /--'_.' ,' | [EMAIL PROTECTED] | (il),-'' (li),' ((!.-' | www.perltraining.com.au | -- SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/ Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html