Solved randomize the characters in the text file

December 16, 2011 at 02:03:48
Specs: Windows XP
I like to have every char or word in the text file should be
randomized ( scrambling words or mixed chars well within the text file). I request for a program in perl or python or Sed. Is it possible?

example:
Input text-1 = "a abcde fghijk"
output text-1 = "aecdb gfijhk a"



See More: randomize the characters in the text file

Report •


✔ Best Answer
December 21, 2011 at 11:45:46
Sorry, it took so long. I don't normally read posts tagged batch as I am a Unix guy.

I use the perl module List::Util's shuffle function which randomizes a perl array. List::Util is standard in perl 5.8. Download it from http://cpan.org if you don't have it.

This was developed on a Linux box, but should be portable:

#!/usr/bin/perl
use strict;
use warnings;
use List::Util 'shuffle';

my $in_file = 'myfile.txt';
my $out_file = 'outfile.txt';
open(IN, '<', $in_file) or die "Could not open file $in_file: $! +";

open(OUT, '>', $out_file) or die "Could not open file $out_file: + $!";

while ( <IN> )
   {
   chomp $_;
   my @myline = split; # create word array
   my @shuffled_array = shuffle(@myline);

   my $i=0;
   while ($i <= $#shuffled_array)
      {
       my $shuffledline = $shuffled_array[$i] ;
       my @mychars = split(//, $shuffledline); # create character array
       my @shuffled_chars = shuffle(@mychars);
       my $x=0;
       while ($x <= $#shuffled_chars)
          {
          print OUT $shuffled_chars[$x] ;
          $x++;
          }
      $i++;
      print OUT " "; # need a space after each word
      }
   print OUT "\n";   # need a New Line
   }

close IN;
close OUT;



#1
December 16, 2011 at 11:44:29
1 - input line
2 - parse into words
3 - randomize word order
3 - for each word parse into characters
4 - randomize characters to make words
5 - output words

Report •

#2
December 16, 2011 at 19:15:23
With reference to #1=
Since I am newbie to programming, will you kindly give me workable program for personal use for - which I shall be ever thankful.
-sriranga(79yrs old)
.

Report •

#3
December 16, 2011 at 20:24:22
Unfortnately I am not proficient in python, perl, or sed. I am a little younger than you (67). I spend about 30 years programming in Basic. My steps will work for most programming languages.

Report •

Related Solutions

#4
December 17, 2011 at 00:24:33
Is it so difficult for experts to give solution requested for? AS such I closed the issue.Thanks.

Report •

#5
December 17, 2011 at 16:44:18
Perhaps you should be more patient and see if someone comes along with the particular expertise you require. Helpers just do this for free in their spare time to try to help others, so you need to be a little less demanding.

Always pop back and let us know the outcome - thanks


Report •

#6
December 21, 2011 at 11:45:46
✔ Best Answer
Sorry, it took so long. I don't normally read posts tagged batch as I am a Unix guy.

I use the perl module List::Util's shuffle function which randomizes a perl array. List::Util is standard in perl 5.8. Download it from http://cpan.org if you don't have it.

This was developed on a Linux box, but should be portable:

#!/usr/bin/perl
use strict;
use warnings;
use List::Util 'shuffle';

my $in_file = 'myfile.txt';
my $out_file = 'outfile.txt';
open(IN, '<', $in_file) or die "Could not open file $in_file: $! +";

open(OUT, '>', $out_file) or die "Could not open file $out_file: + $!";

while ( <IN> )
   {
   chomp $_;
   my @myline = split; # create word array
   my @shuffled_array = shuffle(@myline);

   my $i=0;
   while ($i <= $#shuffled_array)
      {
       my $shuffledline = $shuffled_array[$i] ;
       my @mychars = split(//, $shuffledline); # create character array
       my @shuffled_chars = shuffle(@mychars);
       my $x=0;
       while ($x <= $#shuffled_chars)
          {
          print OUT $shuffled_chars[$x] ;
          $x++;
          }
      $i++;
      print OUT " "; # need a space after each word
      }
   print OUT "\n";   # need a New Line
   }

close IN;
close OUT;


Report •

#7
December 22, 2011 at 00:22:17
thanks for the perl program.
tested in winxp wherein"ActivePerl 5.14.2 Build 1402" had been installed.
Myfile contains "a abcde fghijk" and tested in cmd.

outputfile contains "¿»aï kifhgj cebad"
2nd outputfile contains "ijhfkg adbce ¿»ïa "
3rd outputfile contains "jkfhgi »aï¿ bcade "

It is noticed that "¿»aï " appeared in all three outputfiles as indicated above even though it is NOT in myfile.txt ?

Whether your script is supported utf8 for indic lang for which sample given below:?
Kannada script(utf8)= "ಕನ್ನಡವನ್ನು ಕಲಿಯಿರಿ"


Report •

#8
December 22, 2011 at 09:26:48
I cannot duplicate your output. It looks like there is something weird with your data. I tried pasting your data string into my linux data file and the first character "a" disappears. I would delete the data and renter it and see what happens.

As I am not a window's programmer, I don't know much about UTF. There is perl support for it at http://cpan.org, but you'll have to do the research:

http://search.cpan.org/~chansen/Uni...


Report •

#9
December 22, 2011 at 22:10:41
I tried first delete and re-typed and tested - same problem continues in the outputfile file. deleted old Myfile.txt created another new file "Myfile.txt and tested - to my surprise did not reappeared the unwanted chars and was correct - like that tested for two or thrice output was normal. thanks for your valuable hint. This is new experience for me!
Reg:utf= You need not be windows programmer. In case, if it works in Linux then it is ok. Kindly test it and confirm whether it will work in linux or not after suitable modification in the source code by adding encoding=utf-8? This is my humble logic. I am not programmer nor developer -how to write.

Report •

#10
December 23, 2011 at 00:23:30
Nails,
thanks for the valuable perl program.
I am informed that
" You can add
use encoding "utf-8";
before
use warnings; "
Accordingly I added the same to perl code and then tested - it works for Kannada script. this is for your information.Thanks to the informer.

Report •


Ask Question