Extract a wind reading
Before you start this advanced track
Arrays, hashes, and regular expressions. Expect sigils, capture groups, and scalar/list context.
Guided code reading and debugging. Runnable coding challenges are available in JavaScript, Python, and SQL.
Understand it, then try it.
Perl regular expressions can capture a useful part of a larger string. The =~ operator applies a pattern, and parentheses capture the matching digits. In list context that capture can be assigned to a scalar variable. Here the pattern follows a literal field name, leaving the unit outside the captured number.
use strict;use warnings;my $reading = "wind=18km/h";my ($speed) = $reading =~ /wind=(\d+)/;print "$speed km/h\n";Each mission is a complete Perl 5 script. Run one with perl. The scripts enable strict and warnings and use only built-in features; no modules, files, or external commands are needed.
Track the values line by line. Predict the result before running this example in your own editor.