sort a word "system" in perl/shell without using built in
functions
output should be emssty
Answer Posted / vipul dalwala
======= PERL SCRIPT (sortword.pl) =========
#!/usr/bin/perl
my $word = $ARGV[0];
$sortword = "";
$lastchar = "";
while($word =~ /(.)/g)
{
$lastchar = $1;
if( $sortword ) {
$flag = "";
$newsortword = "";
while($sortword =~ /(.)/g) {
if( $lastchar gt $1 || $flag
eq "charcovered") {
$newsortword =
$newsortword.$1;
$flag = "greater" if($flag
ne "charcovered")
}
else {
$newsortword =
$newsortword.$lastchar.$1;
$flag = "charcovered";
}
}
if( $flag ne "charcovered" ) {
$newsortword =
$newsortword.$lastchar;
}
$sortword = $newsortword;
}
else {
$sortword = $lastchar;
}
}
print $sortword."\n";
======= PERL SCRIPT =========
Run the script as:
sortword.pl "system"
Is This Answer Correct ? | 1 Yes | 0 No |
Post New Answer View All Answers
Hi, I am a accountant. I am preparing a balance sheet but because of staff shortage and time pressures I cant complete it on time. There is lot of common data with last years which I need not retype and I can manage by editing last year’s balance sheet ? Is their any software on net where I can do this easily??
How the interpreter is used in Perl?
What does `$result = f() .. g()' really return?
What is perl programming?
How many types of operators are used in the Perl?
How to read a single line from a file in perl?
Which has highest precedence in between list and terms? Explain?
How to add elements in a hash in perl?
Assuming both a local($var) and a my($var) exist, what's the difference between ${var} and ${"var"}?
What is stdin in perl?
What is hash?
What is the use of -t?
How to code in perl to implement the tail function in unix?
How do I replace every character in a file with a comma?
What does localtime() do in perl?