aboutsummaryrefslogtreecommitdiff
path: root/challenge-074/perlboy1967/perl/ch-2.pl
blob: 124fdaa412e8798ba3cfe3e4b3d85659b607d41e (plain)
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
#!/usr/bin/perl

# Perl Weekly Challenge - 074
# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-074/
#
# Task 2 - FNR Character 
#
# Author: Niels 'PerlBoy' van Dijke

use strict;
use warnings;

use Data::Dumper;
use Tie::IxHash;

my @test = qw(ababc xyzzyx abcabdeabefaf);

my ($S) = @ARGV;
$S //= $test[rand(scalar @test)];

my $O;
tie my %cf, 'Tie::IxHash';

foreach my $c (split(//, $S)) {
  $cf{$c}++;
  my @c = grep { $_ if $cf{$_} == 1 } keys %cf;
  $O .=  (scalar @c ? $c[-1] : '#');
}

printf "Input: \$S = '%s'\n", $S;
printf "Output:     '%s'\n", $O;