Freelancers Network
 
skill list top cap
Homepage
Join the Freelancer's Network
Update your details
Find a freelancer
Post a project
Find a project
Projects Archive
Post a job
Find a job
Jobs Archive
See Dan's Pages
See Andy's Pages
Link to this site
Resources
Join/Leave Forum
Forum Messages
+Additions+ Adverts
Advertising
Contact Us
Subscribe to our newsletter - enter your email address and hit return
Freelancers.net is owned and operated by Andy Stowell and Dan Winchester
skill list end cap
guru web hostcom

Find me again on Freelancers.net

RE: FN-FORUM: perl advice

date posted 1st June 2003 18:46

[EMAIL REMOVED] wrote:
> Hi,
> I am looking at a perl code example in a manual and I am confused...
[...]
> push @digits, (0 ..9, a .. f)[$value & 15];
[...]
> Does [$value & 15]; in the push line add another value to the list in
> digits aswell as 0 .. 9,a ..f on each while loop. In other words I am
> not sure what you end up with in @digits before it is printed. It is
> supposed to convert a decimal number to hex. I understand what $value
> & 15 does but not the square brackets or (0 ..9, a .. f).

The (0..9,a..f) constructs a set, with values
(0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f) which is then indexed by the [$value & 15]
(effectively the remainder when $value is divided by 16). It's the classic
way of converting from one base to another by using long division and
writing down the remainders - e.g.

For 1237 into base 16

1237 / 16 = 77 remainder 5 - this 5 would get mapped into (0..9,a..f) and
get pushed onto @digits as 5
77 / 16 = 4 remainder 13 - this 13 would get mapped into (0..9,a..f) and get
pushed onto @digits as d
4 / 16 = 0 remainder 4 - this 4 would get mapped into (0..9,a..f) and get
pushed onto @digits as 4

The last function merely prints out @digits in reverse order - i.e. 4d5 in
our example.

I'll leave for homework changing this to convert to any base (think about
changing the hard coded values of 15 and 16 in lines 3 and 4 of the source
code, and modifying the set as necessary)...

Cheerio!
Ross
--
[EMAIL REMOVED]
DJSE Limited, Willow End, Mawthorpe, Alford, Lincolnshire, LN13 9LT
T: 01507 466 797
F: 01507 463 731
M: 07970 801 661
http://www.djse-ltd.co.uk




Messages by Day
June 30th 2003
June 29th 2003
June 28th 2003
June 27th 2003
June 26th 2003
June 25th 2003
June 24th 2003
June 23rd 2003
June 22nd 2003
June 21st 2003
June 20th 2003
June 19th 2003
June 18th 2003
June 17th 2003
June 16th 2003
June 15th 2003
June 14th 2003
June 13th 2003
June 12th 2003
June 11th 2003
June 10th 2003
June 9th 2003
June 8th 2003
June 7th 2003
June 6th 2003
June 5th 2003
June 4th 2003
June 3rd 2003
June 2nd 2003
June 1st 2003


Messages by Month
December 2003
November 2003
October 2003
September 2003
August 2003
July 2003
June 2003
May 2003
April 2003
March 2003
February 2003
January 2003


Messages by Year
2008
2007
2006
2005
2004
2003
2002
2001
2000