How can I get Perl string to keep its original formatting after editing it? -
i attempting write code encrypt letters basic cyclic shift cipher while leaving character not letter alone. trying through use of sub finds new value each of letters. when run code now,it formats result there single space between every encrypted letter instead of keeping original formatting. cannot result in lowercase letters.
sub encrypter { $letter = shift @_; if ($letter =~ m/^[a-za-z]/) { $letter =~ y/n-za-mn-za-m/a-za-z/; return $letter; } else { return lc($letter); } } print "input string encrypted: "; $input = <stdin>; chomp $input; print "$input # user input\n"; @inputarray = split (//, $input); $i = 0; @encryptedarray; ($i = 0; $i <= $#inputarray; $i++) { $encryptedarray[$i] = encrypter($inputarray[$i]); } print "@encryptedarray # output\n";
the problem how printing array.
change line:
print "@encryptedarray # output\n";
to:
print join("", @encryptedarray) . " # output\n";
here example illustrates problem.
#!/usr/bin/perl @array = ("a","b","c","d"); print "@array # output\n"; print join("", @array) . " # output\n";
output:
$ perl test.pl b c d # output abcd # output
according perl documentation on print:
the current value of $, (if any) printed between each list item. current value of $\ (if any) printed after entire list has been printed.
so 2 others ways be:
#!/usr/bin/perl @array = ("a","b","c","d"); $,=""; print @array, " #output\n";
or
#!/usr/bin/perl @array = ("a","b","c","d"); $"=""; print @array, " #output\n";
here related answer , here documentation explaining $"
, $,
.
Comments
Post a Comment