TASK #1 › Locate Memory
Task
Write a script to declare a variable or constant and print it’s[sic] location in the memory.
My solution
One good thing about Perl (and most modern languages) is that you don't really need to worry about memory management. Perl will automatically reclaim memory when a variable is no longer used.
For this task, I take the reference to the variable (for example SCALAR(0x559184ba5890)
and use regular expression to display the location (the bit between the parenthesis).
Minified
In 22 characters, this can be minified to the below example.
» perl -E 'say\$a=~/(0x[0-9a-f]+)/'
0x555668d2a3f8
Example
» ./ch-1.pl
0x555668d2a3f8
TASK #2 › Bell Numbers
Task
Write a script to display top 10 Bell Numbers.
My solution
In tackling this task, the animated gif is very helpful. I use the @bell
array to reproduce this. I start by seeding the table with a single value, and then use the copy and add functions to generate the subsequent values. Finally I use a foreach loop to display the last number in each row to display the list of bell numbers.
Example
» ./ch-2.pl
1
2
5
15
52
203
877
4140
21147
115975
Top comments (0)