-
Notifications
You must be signed in to change notification settings - Fork 349
Expand file tree
/
Copy pathch-1.pl
More file actions
33 lines (30 loc) · 737 Bytes
/
ch-1.pl
File metadata and controls
33 lines (30 loc) · 737 Bytes
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
#!/usr/bin/env perl
# Week 180 Task 1
# Write a script to find out the first unique character in the given string
# and print its index (0-based).
use strict;
use warnings;
use Test::More;
ok(first_unique_character("Perl Weekly Challenge") == 0, "First");
ok(first_unique_character("Long Live Perl") == 1, "Second");
ok(first_unique_character("LOLOLx") == 5, "Last");
ok(!defined(first_unique_character("LOLO")), "None");
done_testing();
sub first_unique_character {
my $s = shift;
my $len = length $s;
my $index = 0;
while ( $index < $len) {
my $c = substr $s, $index, 1;
my $number = () = $s =~ /$c/gi;
if ( $number == 1){
last;
}
$index++;
if ($index == $len) {
undef $index;
last;
}
}
return $index;
}