perl

Fun with Perl #3 – Hashes

Posted on Updated on

Managed to finish my work a bit early so I get to play around with Perl again … for a little bit. I’m trying to get my head around object orientation in this language but it looks … strange still … so I need more time to get used to that first.

Today I looked at Hashes because I never really use this tool in any language. The closest I have ever used is probably a dictionary in Python and/or a map in C++. Basically, a Hash in Perl is a dictionary in Python with a key and value definition but the syntax is a little different. The idea is very easy to understand.

In Perl you make a hash like this:

%hash = ("UK", 1, "HK", 2, "CND", 3);

and in Python a dictionary, which works the same way, is created like this:

names = {“UK”: 1, “HK”: 2, “CND”: 3}

the strings UK, HK and CND are keys, which have int values each. Printing out the Hash/dictionary contents will look like below:

UK - 1
HK - 2
CND - 3

Fun with Perl #2 – Arrays & hard reference

Posted on Updated on

I moved to a new team this week so I haven’t had time to play around with Perl as much as I wanted .. but, I have a bit of free time to at least post something on what I thought was kinda cool but very basic.

$name = 'bill';

above is a reference to a string ‘bill’

$fooref = \$name;

is a hard reference which when run prints the following

SCALAR(0x7fa5d3006ce8)

So that was cool.. what about arrays? thats easy.

@array = ('Benji', 'Daniel', 'Ando');

and thats it. I figured to wrap everything together in 3 different subroutines/methods and run them all at the same time in some order I can see each result at the same time. So I modified my code and my result became the source below.

sub answer { 
	$name = 'bill';
	return \$name;
}

sub reference_answer { 
	$fooref = answer();
        print $fooref, "\n";
}
#! /usr/bin/perl


# サブルーチンの定義
# sub サブルーチン名 { 処理 }
# (sub + 半角スペース + サブルーチン名 + ブロック)

sub answer { 
	$name = 'bill';
	return \$name;
}

sub reference_answer { 
	$fooref = answer();
	print $fooref, "\n";
}

# &array_sub;
sub array_sub {

	# 配列を作る
	@array = ('Benji', 'Daniel', 'Ando');

	# 配列の中身を出力
	foreach $count(@array) {
		print $count, "\n";
	}	

	# 中身のハードリファレンスを出力
	foreach $count(@array) {
		print \@count, "\n";
	}
}

print "reference_answer --", reference_answer();
print "array_sub --" array_sub();

when run:

SCALAR(0x7fa5d3006ce8)
reference_answer -- 1Benji
Daniel
Ando
ARRAY(0x7fa5d300a3e0)
ARRAY(0x7fa5d300a3e0)
ARRAY(0x7fa5d300a3e0)

time to get back to work and stop playing around ..
sorry if my variable names and output results look very messy. Next time I’ll make something cool and easier to read.

Fun with Perl

Posted on Updated on

I’ve been wanting to learn Perl for a long time but never had the chance. So today after finishing my assignment and a bit of time to spare I decided to have a little fun with it and see just what kind of cool things I can do with it.

This is just a simple for loop inside another for loop to print a 10 by 10 grid. Useful for later if I want to iterate then print out a 2D array.

#!/usr/local/bin/perl
my $count;
 
$count;
$new_count;
 
for ($count = 0; $count < 10; $count++) {
        print $count;
    for ($new_count = 0; $new_count; 10; $new_count++) {
        print $new_count;
    }
    print "\n";
}