use warnings;
use strict;
my $global="abcdefghijklmnopqrstuvwxyz";
my @globe=split//,$global;
Having had to maintain other people's code, I'm a big fan of sensible
names for variables. A "globe" is a sphere - what does this have to do
with the contents of this variable?
@chars or @letters would be a bit more sensible.
But then you only use this array once, so it's not really needed (see
below)
my $final;
my $len=scalar @globe;
Note that the "scalar" is redundant here - the right hand side of the
assignment is already in scalar context. $len is never used anyway,
so really the whole statement is redundant.
my $r;
print "$global\n";
foreach my $i (@globe)
{
if ((($r % 10)==0) & ($r>0)) ^
oops!
{
$final=$final.".".$i;
$r++
}
else
{
$final="$final"."$i";
$r++;
}
}
More neatly written as:
my $count = 0;
my $final = '';
for my $char ( split //, $global ) {
if ( $count and $count % 10 == 0 ) {
$final .= '.';
}
$final .= $char;
++$count;
}
(untested)
but see below for a better way of doing it.
print "\n$final";
this is a very crude example , but you are still adviced to follow what
other have mentioned,
I know you said it's crude, but there's not much point in enabling
warnings if you're going to ignore them;
Possible precedence problem on bitwise & operator at - line 12.
Use of uninitialized value in modulus (%) at - line 11.
Use of uninitialized value in numeric gt (>) at - line 11.
Use of uninitialized value in string at - line 18.
Warnings show that things aren't working as you intend, even if the
end result is right.
Why split the string into individual characters? Perl is much better
operating on full strings than individual characters.
For example:
#!/usr/bin/perl
use warnings;
use strict;
my $string = join '', 'A' .. 'Z', 'a' .. 'z';
for ( my $pos = 9; $pos < length $string; $pos += 10 ) {
$string = substr( $string, 0, $pos )
. "."
. substr( $string, $pos );
}
NB: the "spec" is ambiguous; is a "." wanted after every 10 characters
of the input, or at every 10th location in the output? My code does
the latter.