loading...

re: Daily Challenge #15 - Stop gninnipS My sdroW! VIEW POST

FULL DISCUSSION
 

Perl one-liner:

my $string = "Good luck, Happy coding!"; my $res = join " ", map { (length($_) >= 5) ? scalar reverse($_) : $_ } split / /, $string; say $res;

A bit more cleaned up, taking user input:

#!/usr/bin/perl

use v5.24;
use strict;
use warnings;
use Carp;

# Challenge says only letters and spaces, but this won't be picky
sub reverse_over_4 {
    my $string = shift or croak "Error: missing argument to function.";
    my $res    = join " ",
        map { ( length($_) > 4 ) ? scalar reverse($_) : $_ } split / /,
        $string;
    return $res;
}

print "Enter string: ";
my $input = <STDIN>;
chomp $input;
say reverse_over_4($input);

See runnable demo on PerlBanjo

 

Note that ($_) is not needed for length and reverse.

 

Yeah, true, that's just my personal preference 🙂

code of conduct - report abuse