Scramble a string.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Scramble a string.
# 1  
Old 08-29-2008
Scramble a string.

Hi all,

I am trying to write a perl script that will take user input as a string and scramble the string and print the result.
Note: I cannot use shuffle function ....using for loop.

Code:
so here is example.
Enter a String: abcdef

Print the Result: debacf 

Enter a String: abcdef

print the Result: cdabef

Here is my perl code but it doesn't work exactly:
Code:
print "Enter a string:   ";
$sString = <STDIN>;

chomp($sString);

print "String = $sString\n";

$iLength = length($sString);
@array = split(//, $sString);

print "array = @array\n";

print "length = $iLength\n";
for ( $i = 1; $i <= $iLength; $i++ ){
$item = int(rand($#array));
print "@array[$item]\n";
}

Please any body help me.

thanks,
# 2  
Old 08-29-2008
If you have perl 5.8.0 or later your should have shuffle. If you cannot use shuffle, why not?
# 3  
Old 09-01-2008
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";

# 4  
Old 09-01-2008
I hadn't heard of shuffle so I googled a bit.

perlfaq4 - perldoc.perl.org
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

2. Shell Programming and Scripting

Insert String every n lines, resetting line counter at desired string

I need to read a text file and insert a string every n lines, but also have the line counter restart when I come across a header string. Line repeating working every 3 lines using code: sed '0~3 s/$/\nINSERT/g' < INPUT/PATH/FILE_NAME.txt > OUTPUT/PATH/FILE_NAME.txt I cannot seem to find... (1 Reply)
Discussion started by: Skonectthedots
1 Replies

3. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

4. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

5. Shell Programming and Scripting

grep exact string from files and write to filename when string present in file

I am attempting to grep an exact string from a series of files within a directory and append that output to the filename when it is present in the file. I've been after this all day with no luck. Thanks for your help in advance :wall:. (4 Replies)
Discussion started by: JC_1
4 Replies

6. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

7. Shell Programming and Scripting

to extract string from main string and string comparison

continuing from my previous post, whose link is given below as a reference https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569 consider there is create table commands in a file for eg: CREATE TABLE `Blahblahblah` ( `id` int(11) NOT NULL... (2 Replies)
Discussion started by: vivek d r
2 Replies

8. Shell Programming and Scripting

Scramble words

If I have a file like: one two three four five six seven eight nine ten eleven twelve thirteen How can I use bash to scramble the words randomly, so that the sequence becomes different? I am talking about something like: four eight eleven two one thirteen ... (1 Reply)
Discussion started by: locoroco
1 Replies
Login or Register to Ask a Question