-
Notifications
You must be signed in to change notification settings - Fork 323
/
Copy pathch-1.pl
46 lines (37 loc) · 994 Bytes
/
ch-1.pl
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
37
38
39
40
41
42
43
44
45
#!usr/bin/perl -w
# Created for perl weekly challenge - 180 - 1
use strict;
use warnings;
use feature 'say';
#example calls
# findFirstUnique("Perl Weekly ChallengePerl Weekly Challenge");
# findFirstUnique("Perl Weekly Challenge");
# findFirstUnique("Long Live Perl");
sub findFirstUnique
{
my $string = shift;
my $returnVal = -1;
my @characters = split(//, $string);
my $i = 0;
foreach my $char (@characters)
{
# skip spaces
if($char ne ' ')
{
#count the characters in the string
if( 1 == ($string =~ s/$char//g ) )
{
# if it is one then it is unique
say( "Output: $i as '$char' is the first unique character");
$returnVal = $i;
last;
}
}
$i++;
}
if( $returnVal == -1 )
{
say( "Output: The given string does not contain unique characters!");
}
return $returnVal;
}