The UNIX and Linux Forums  

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


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search, replace string in file1 with string from (lookup table) file2? gstuart Shell Programming and Scripting 9 06-08-2009 07:11 AM
libapache2-mod-scramble-ip 0.2 (Default branch) iBot Software Releases - RSS News 0 07-10-2008 07:50 PM
Scramble Eggs 1.1 (Default branch) iBot Software Releases - RSS News 0 05-04-2008 06:30 PM
appending string to text file based on search string malaymaru Shell Programming and Scripting 1 06-09-2006 09:53 AM
sed problem - replacement string should be same length as matching string. amangeles Shell Programming and Scripting 4 01-11-2006 06:11 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-29-2008
email-lalit email-lalit is offline
Registered User
  
 

Join Date: May 2008
Posts: 36
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 (permalink)  
Old 08-29-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,762
If you have perl 5.8.0 or later your should have shuffle. If you cannot use shuffle, why not?
  #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";
  #4 (permalink)  
Old 09-01-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
I hadn't heard of shuffle so I googled a bit.

perlfaq4 - perldoc.perl.org
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:57 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.

Content Relevant URLs by vBSEO 3.2.0