The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #3 (permalink)  
Old 09-01-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
The simplest way is to loop through the array swapping each item with another item in a random position. Note that the array is indexed from 0, not 1.


Code:
for ( $i = 0; $i < $iLength; $i++ ){
        $random = int(rand($#array));
        $temp=$array[$random];
        $array[$random]=$array[$i];
        $array[$i]=$temp;
}
for ( $i = 0; $i < $iLength; $i++ ){
        print "$array[$i]";
}
print "\n";