Need help in sort variable in perl

Hi,

I need help in sorting variables in perl

i have two variables and if those two variables are equal then its good. I have a problem here

example:

variable1= number2 number1
variable2=number1 number2

in my case above both the variables are also equal but the condition is not satisfying so its failing

how can i sort variable 1 to change it from number2 number1 to number1 number2

Thanks in advance!!!

$
$ perl -le '$x = "426153"; print "Before sort: \$x = $x"; $x = join "",sort split//,$x; print "After  sort: \$x = $x"'
Before sort: $x = 426153
After  sort: $x = 123456
$
$

tyler_durden

Hi,

Thanks for the reply

but the code you have given is not working for my condition

here is my requirement

variable1 = david3 david1

variable2 = david1 david3

i need to change variable1 like david1 david3

i need to use this as dynamic thing

please help me on this....

It's a very simple change of the above script.

Right now, it splits on // i.e. nothing, so splits apart every charatcter. And joins together on "" i.e. nothing, so puts them back together with nothing inbetween.

/ / and " " would split/sort on spaces and put them back together with spaces inbetween.

Of course, using split / /, $x would hold 'david3' and 'david1' as two elements of a list. But that alone would not be sufficient. Upon sorting this list, the output would be a simple alphabetic sort meaning if the list were to contain 'david3' and 'david10' (in this order), the simply sorted list would contain 'david10' and 'david3' (in this order).

Either write your own sub-routine to sort alpha-numeric strings or use the Sort::Naturally module of perl.

An example:

#! /usr/bin/perl -w
use strict;
use Sort::Naturally 'nsort';

my $var = 'david10 david3';
my @x = split / /, $var;

print join (' ', (nsort @x));

HI Corona688,

the code is working fine but i need some more help

i have one more entry in variable name i want name to be like that and need to sort number thing

for example

variable1= name number2 number1
variable2= name number1 number2

i need to have name to be not sorted but i need number2 and number1 sorted

Please help me on this