Download presentation
Presentation is loading. Please wait.
Published byMartin Thompson Modified over 8 years ago
1
(Perl5) Data-structures Mark Overmeer MARKOV Solutions FOSDEM, 6 Feb 2011
2
Content ● Scalars ● Lists ● Arrays ● Hashes ● Subroutines ● File-handles ● References ● Autovivification ● Complex structures ● Stashes
3
Scalars ● $s = 3integer ● $s = 3.1415float ● $s = “aap”string ● if($s)boolean ● $s = \3reference ● $s = Table->newobject
4
Scalars ● the only way to keep data ● unlimited string sizes ● content type can change during the program run ● can contain multiple values at the same time (dualvar/$!) ● consume at least 28 bytes, each
5
Scalar expensive? ● File of 100.000 lines x 72 chars = 100.000 x (28 bytes + 72 bytes) = 10MB
6
Scalar expensive? ● File of 100.000 lines x 72 chars = 100.000 x (28 bytes + 72 bytes) = 10MB ● 4GB = 35€, computer lives 2 year: File in RAM costs: 1.5 n€/s 5.1 μ€/h
7
False ● Scalars have a truth value. ● False are: undef 00.0 “”“0” ● mistake:die “no filename” unless $fn; ● mistake:$x = $temperature || 20;
8
Lists/Arrays ● List: sequence of values print 1.02, $x, 42, “foo”; my $z = (1, 2, 3); ● Array: storage for values @x = (1.02, $x, 42, “foo”); my $z = @x; ● Perl5 sometimes confused: wantarray, @_
9
Context ● void context: no need for results 3; print; @y;$y = 1, 3; ● scalar context: one result expected $x = 3;1 < $x; $x = @y;$i < @y; ● list context: multiple results expected @x = 3;print 3, @x; @x = @y;chop @x;
10
Array ● Arrays are self-extending ● $[ = 1 $# = 100 ● for(my $i = 0; $i < @a; $i++) { print $a[$i]; } for(my $i = $[ ; $i <= $#a ; $i++) { print $a[$i]; } foreach my $x (@a) { print $x; }
11
Array ● my @x; is equal to my @x = (); ● my @x = 1..8;print @x; $x[3] = 42;print $x[3]; @x[1, 3, -1] = 6..8;print @x[1, 3, -1]; ● List: my ($a,$b) = ($b,$a);(getpwnam $x)[3,-1];
12
Array ● shift @xpush @x pop @xunshift @x my @z = splice @x, 3, 2, “foo”, “bar”; ● my $z = delete $x[4]; undef $x[4]; ● warning at @x[1];
13
Hash ● my $z = delete $age{mark}; ● $age{bill} = undef; if(exists $age{bill})... if(defined $age{bill}) … ● print %age; print scalar %age; print scalar keys %age; ● %h = () ; versus undef %h;
14
Hash ● Array knows order, hash doesn't ● Hash 20% slower but much more useful ● Hashes are self-extending as well. ● Hash keys are shared. ● my %age = (mark => 44, henk => 23); print $age{mark};
15
Hash ● my %h = ()equivalent to my %h; ● my %h = (a => 1, b => 2); print %h; $h{'time'} = 3;print $h{'time'}; $h{time}= 3;print $h{time}; $h{(time)}= 3; ● @h{'tic', 'tac', 'toe'} = 1..3; print @h{ qw/foo bar/ }
16
Hash ● (a => 1, b => 2, c => 'd') ('a', 1, 'b', 2, 'c', 'd') qw/a 1 b 2 c d/; ● my %string2errno = reverse %errno2string; ● %h = %y; ● @h{ keys %y } = values %y;
17
Subroutines ● Get a list:func(1, 2, 3); ● With prototype you can remove the parens: sub func($$$); func 1, 2, 3; also with imported functions. ● &func; dangerous and outdated: Perl4!!!
18
Subroutines ● Result of last expression is returned. Do not use 'return' at the end of a function. ● Subroutines can return a list. ● !defined wantarray: void context !wantarray: scalar context wantarray: list context
19
Subroutines ● sub f($$) {my ($x, $y) = @_; {my $x = shift @_;{my $x = shift; my $y = shift @_; my $y = shift; {my ($x, $y) = (shift, shift); {my ($x) = shift;# silly
20
File-handles ● Expired interface: open X, “>$filename” or die; print X “Hello, World!\n”; ● More recent Perl: open my($x), “>:shift-jis”, $fn or die; print $x “Hello, World!\n”; $x->print(“Hello, World\n”); ● my $old = select STDERR; print “Text\n”;# to STDERR select $old;
21
Basic variables ● scalar$x ● array@x$x[1]@x[3,1] ● hash%x$x{foo}@{'foo','bar' } ● sub&x&x(1, “foo”) ● stashX*X
22
References ● eq pointers ● Pass reference, not the data. Most useful for arrays and hashes. ● my $x = \42; print $x; # SCALAR(0x121512) print $$x;# 42 ● crucial for object-oriented programming
23
Zip arrays ● Say, zip merges elements of two arrays zip(@x, @y)# no zip(\@x, \@y)# yes ● Backslash creates a reference, also a scalar! sub zip($$) {my ($x, $y) = @_; print @$x;# array where $x points to $y->[4];# or $$y[4]
24
References ● References are not easy: – an extra sigil – complex data structures data-structuren – even more meanings for [ ] and { }
25
References ● Ref-array: my @x = (1.0, “foo”, 42); $x[1]; my $rx = \@x; $$rx[1] or $rx->[1]; my $rx = [ 1.0, “foo”, 42 ]; print @$rx;$rx->[1]; ● print $rx;# ARRAY(0x12151)
26
References ● Ref-hash: my %h = (a => 1, b => 2); $h{a}; my $rh = \%h;$$rh{a} or $rh->{a} my $rh = { a => 1, b => 2 }; keys %$rh; ● Not possible (yet):@rh->[1, 2] still old syntax:@$rh[1,2] ● print $rh;# HASH(0x1412325)
27
Accolades ● read assistent:print ${a}[3]; = $a[3] ● hash:print $a{bar}; ● block:if(...) {... } ● anon. sub:sort { $a $b } @k ● anon. hash: my $x = { a=>1, b=>2 }; ● quotes:qw{... }
28
Autovivification ● “create when needed”: undef adapts itself ● Difference between my $i;$i = $i + 1; my $i;$i += 1; ● as number: 0my $s;$s++; as string: “”my $w;$w.= “more”; as array: [ ]my $x;$x->[3] = 8; ● as hash: { }my $y;$y->{aap} = 6;
29
Multi-level ● my %wallet; $wallet{brand} = 'vendex'; $wallet{cards} = [ 'transport', 'bank' ]; print “@{$wallet{cards}}\n”; $wallet{money}{notes} = [ 5, 10, 10, 25]; push @{$wallet{money}{notes}}, 50, 5; my $n = delete $wallet{money}{notes}[0];
30
hashes of hashes of hashes ● my %wallet; $wallet{money}{coins} = { 1 => 34, 2 => 3, 5 => 0, 10 => 2 }; my $coins = $wallet{money}{coins}; $coins->{$_}++ foreach 1, 1, 10, 5; while(my ($w, $m) = each %$coins) { $value += $w * $m; }
31
Simple? ● Draw a picture (data design) and keep it! ● Use helper references ● Use readible names ● More readible with Object-oriented: $wallet{money}{coins}{$_}++ for 1,1,5; $wallet->geld('coins')->add(1,1,5); ● Data::Dumper: print Dumper(\%wallet);
32
Anonymous Subroutines ● Execute function now: f(1,2,3) ● Separate definition from application: my $r = \&f; $r->(1,2,3); my $r = sub { print “@_\n” }; ● Example Tk: $mw->Button( callback => sub {exit} );
33
Overview my $x $r=\$x $r=\3 $$r my @x $x[1] @x[1,2] $r=\@x $r=[1,2] $r->[0] @$r my %x $x{a} @x{@k} $r=\%x $r={a=>1} $r->{a} %$r sub x x(1,'a') $r=\&x $r=sub {} $r->(1) open X print X $r=*X print $r
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.