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


Please Help Members By Posting Answers For Below Questions

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??

1759


How the interpreter is used in Perl?

533


What does `$result = f() .. g()' really return?

571


What is perl programming?

562


How many types of operators are used in the Perl?

582






How to read a single line from a file in perl?

546


Which has highest precedence in between list and terms? Explain?

442


How to add elements in a hash in perl?

513


Assuming both a local($var) and a my($var) exist, what's the difference between ${var} and ${"var"}?

541


What is stdin in perl?

547


What is hash?

555


What is the use of -t?

548


How to code in perl to implement the tail function in unix?

531


How do I replace every character in a file with a comma?

531


What does localtime() do in perl?

567