Logo

Programming-Idioms

History of Idiom 5 > diff from v7 to v8

Edit summary for version 8 by :

Version 7

2015-08-21, 11:28:52

Version 8

2015-08-21, 11:29:09

Idiom #5 Create a 2D Point data structure

Declare a container type for two floating-point numbers x and y

Idiom #5 Create a 2D Point data structure

Declare a container type for two floating-point numbers x and y

Code
# For named member access
my $point = { x=>1.5, y=>6.3 };
print "Coordinates: x=$point->{x}, y=$point->{y}\n";

# Or an array using index 0, 1 to hold x, y
my $point = [ 1.5, 6.3 ];
print "Coordinates: x=$point->[0], y=$point->[1]\n";
Code
my $point = [ 1.5, 6.3 ];
Comments bubble
For lists, perl uses arrays. For structures (like a 2D point) hashes are more commonly used.
Comments bubble
This array uses indices 0, 1 to hold x, y.

No need to declare a type beforehand.

For lists, perl prefers using arrays (over using hashes).