File: use-hashes.nqp

package info (click to toggle)
nqp 2024.09%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,972 kB
  • sloc: java: 28,087; perl: 3,479; ansic: 451; makefile: 202; javascript: 68; sh: 1
file content (36 lines) | stat: -rwxr-xr-x 496 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env nqp

# create a hash
my %h := nqp::hash(
  'k1', 0,
  'k2', 1,
);

# iterate over the hash
for %h {
    my $k := nqp::iterkey_s($_); 
    my $v := nqp::iterval($_);
    say("key $k => val $v");
}

my $k1 := 'k1';
my $k2 := 'k2';

# return value of a key
my $v := nqp::atkey(%h, $k1);
say("value of key $k1 is $v");

my $k3 := 'k3';
my $v3 := 3;

# add a new pair
nqp::bindkey(%h, $k3, $v3);

# check existence of a key
if nqp::existskey(%h, $k3) {
    say("key $k3 exists");
}