birdinforest wrote:
I am a student and enrolled Unix programing this semester. There are two questions relating to perl I can not work out in the last exam ( Actually I have write out my code, however the exam system marked it as "wrong"). Please help me to point out the fault. Thanks.QUESTION 1 Write a perl script called perl_has_number.pl which takes one argument which is any string. The script should output "true" if the string contains any digits, and "false" otherwise. MY CODE: #!/usr/bin/perl -w if ($ARGV[0]=~ /\d+/) { print "true\n"; } else { print "false\n"; } QUESTION 2 Write a perl script called perl_digits.pl which takes any number of arguments. The script should return the number found by deleting all non-digits. Example: "./perl_digits.pl and1 pan2d 30rr and 4" will return 12304 MY CODE: #!/usr/bin/perl -w sub outcom { $outcome=""; foreach $a (@ARGV) { if ($a=~/\d+/) { $outcome=$outcome.$&; } } return $outcome; } $digital=outcom(@ARGV); print $digital . "\n"; In my view, both code could give out right results. Could you help me to find the fault?
You should find out from your examiners why they are "wrong". Perhaps they want you to use tr/// instead of m//:
#!/usr/bin/perl -l print $ARGV[0] =~ tr/0-9// ? "true" : "false"; #!/usr/bin/perl -l ($_ = "@ARGV") =~ tr/0-9//cd; print; John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order. -- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
