Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Eights and nines

Posted by TedH (TedH), 15 June 2005
Hi, hmmm...okay, what does Perl have against 08 and 09?

In the scripts I'm putting together a folder is created each month with 'yearmonth' as it's name (January 2005 = 200501).

I can scan for the folders and separate the 2005 and the 01 into a variable each ($aa is the year, $bb is the month).

Now it pleased me no end that I managed to do that without coming to a forum or phoning up somebody. So the next step was doing fine. I could use if and elsif to swap stuff around and have 01 print as Jan etc. Great thought I, so did the rest of the year.

Then Perl decides to kick out 08 and 09 as illegal! I'm not using the date/time function for this as this doesn't actually relate to that function.

Code:
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
print "Content-type: text/html\n\n";

$bb="01"; # example of separation

 if ($bb == 01) {print "Jan\n";}
 elsif ($bb == 02) {print "Feb\n";}
 elsif ($bb == 03) {print "Mar\n";}
 elsif ($bb == 04) {print "Apr\n";}
 elsif ($bb == 05) {print "May\n";}
 elsif ($bb == 06) {print "Jun\n";}
 elsif ($bb == 07) {print "Jul\n";}
 elsif ($bb == 08) {print "Aug\n";}
 elsif ($bb == 09) {print "Sep\n";}
 elsif ($bb == 10) {print "Oct\n";}
 elsif ($bb == 11) {print "Nov\n";}
 elsif ($bb == 12) {print "Dec\n";}
 else {print "No match\n";}


and get this response (no matter what I try):
Illegal octal digit '8' at c:\IGO\CGI-BIN\TDA\T1.PL line 14, at end of line
Illegal octal digit '9' at c:\IGO\CGI-BIN\TDA\T1.PL line 15, at end of line

Now I'm sure that Larry Wall had a very good reason for doing this - but it doesn't half make things difficult for the rest of us...

I bet this has to do with binary doesn't it?

I sure come up with some doozies don't I?

What to do - Ted

Posted by admin (Graham Ellis), 15 June 2005
If you put a zero in front of a number, it's taken as Octal  

Full reply to follow

Posted by TedH (TedH), 15 June 2005
So a long winded way would be to assign each number to a variable ($a=01, etc.), then instead of having the numbers in if/else statements, insert the variables.

Tried it and it worked. I suppose then to make the code smaller I could make it work from arrays? Yeah?

Posted by John_Moylan (jfp), 15 June 2005
Could try quoting the numbers, perl would then parse these as strings and automagically convert them back to numbers if you numerically compared them

Code:
if ($bb == '01') {print "Jan\n";}
elsif ($bb == '02') {print "Feb\n";}
elsif ($bb == '03') {print "Mar\n";}
elsif ($bb == '04') {print "Apr\n";}


I may be talking rubbish, but it compiled (perl -c) without warnings.

Note: I realise this is test code but it really looks like its screaming out for a hash, then you can use exists() to see if the key is there.
http://www.perl.com/doc/manual/html/pod/perlfunc/exists.html

Posted by admin (Graham Ellis), 15 June 2005
Here's how I would do it ... shorter but harder to follow ... a list of month names ...!

Code:
@months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);

$bb = "01";
$mword = $months[$bb-1];
print "Month $bb is $mword\n";

$bb = "08";
$mword = $months[$bb-1];
print "Month $bb is $mword\n";

$bb = "12";
$mword = $months[$bb-1];
print "Month $bb is $mword\n";


earth-wind-and-fire:~/jun05 grahamellis$ perl ted
Month 01 is Jan
Month 08 is Aug
Month 12 is Dec
earth-wind-and-fire:~/jun05 grahamellis$

Posted by TedH (TedH), 15 June 2005
Great, both work. The shorter version from Graham solves this problem.

JFP, the quotes did do the stuff and can certainly be used on some less complicated stuff that doesn't have as much input.

Thanks once again.

........

Graham, I couldn't get the images off the camera so am looking for an alternative (anything but Kodak).

- Ted



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho