Discussion:
Randomize lines
After Hours
2008-01-25 16:01:19 UTC
Permalink
I have an unusual need to randomize a text file with about 500 lines
(defined by CR line endings).

Is there a method I've not discovered in BBEdit that will randomize
such a text file?

thx.
--
------------------------------------------------------------------
Have a feature request? Not sure the software's working correctly?
If so, please send mail to <***@barebones.com>, not to the list.
List FAQ: <http://www.barebones.com/support/lists/bbedit_talk.shtml>
List archives: <http://www.listsearch.com/BBEditTalk.lasso>
To unsubscribe, send mail to: <bbedit-talk-***@barebones.com>
Matthew Fischer
2008-01-25 16:47:10 UTC
Permalink
Post by After Hours
I have an unusual need to randomize a text file with about 500
lines (defined by CR line endings).
Is there a method I've not discovered in BBEdit that will randomize
such a text file?
You can do it with a Unix filter. Here's one in PERL (which I found
on the web at some point). Just save it in your Unix Filters folder,
then select the lines you want to randomize and run the filter.

#!/usr/bin/perl -w

sub fys {
my $array=shift;
my $i;
for ( $i=@$array; --$i; ) {
my $j = int rand ($i+1);
next if $i == $j;
@$array[$i,$j]=@$array[$j,$i];
}
}

while (<>) {
push(@lines, $_);
}
fys(\@lines);
foreach(@lines) {
print $_;
}
--
------------------------------------------------------------------
Have a feature request? Not sure the software's working correctly?
If so, please send mail to <***@barebones.com>, not to the list.
List FAQ: <http://www.barebones.com/support/lists/bbedit_talk.shtml>
List archives: <http://www.listsearch.com/BBEditTalk.lasso>
To unsubscribe, send mail to: <bbedit-talk-***@barebones.com>
Bryan R Harris
2008-01-25 16:58:26 UTC
Permalink
Post by Matthew Fischer
Post by After Hours
I have an unusual need to randomize a text file with about 500
lines (defined by CR line endings).
Is there a method I've not discovered in BBEdit that will randomize
such a text file?
You can do it with a Unix filter. Here's one in PERL (which I found
on the web at some point). Just save it in your Unix Filters folder,
then select the lines you want to randomize and run the filter.
#!/usr/bin/perl -w
sub fys {
my $array=shift;
my $i;
my $j = int rand ($i+1);
next if $i == $j;
@$array[$i,$j]=@$array[$j,$i];
}
}
while (<>) {
}
print $_;
}
Or a little shorter:

**************************************
#!/usr/bin/perl -w

@content = <>;
while (@content) { print splice(@content,rand(@content),1); }
**************************************

- B
--
------------------------------------------------------------------
Have a feature request? Not sure the software's working correctly?
If so, please send mail to <***@barebones.com>, not to the list.
List FAQ: <http://www.barebones.com/support/lists/bbedit_talk.shtml>
List archives: <http://www.listsearch.com/BBEditTalk.lasso>
To unsubscribe, send mail to: <bbedit-talk-***@barebones.com>
Paul Cezanne
2008-01-29 12:07:19 UTC
Permalink
I needed to do this also, but for about 4,000 lines. Frankly, I just used Excel. I dropped a random number into a column, sorted on that column, and then deleted the column.

Worked like a charm.


-----Original Message-----
From: BBEdit-Talk List on behalf of After Hours
Sent: Fri 1/25/2008 11:01 AM
To: BBEdit-Talk List
Subject: Randomize lines

I have an unusual need to randomize a text file with about 500 lines
(defined by CR line endings).

Is there a method I've not discovered in BBEdit that will randomize
such a text file?

thx.
--
------------------------------------------------------------------
Have a feature request? Not sure the software's working correctly?
If so, please send mail to <***@barebones.com>, not to the list.
List FAQ: <http://www.barebones.com/support/lists/bbedit_talk.shtml>
List archives: <http://www.listsearch.com/BBEditTalk.lasso>
To unsubscribe, send mail to: <bbedit-talk-***@barebones.com>
--
------------------------------------------------------------------
Have a feature request? Not sure the software's working correctly?
If so, please send mail to <***@barebones.com>, not to the list.
List FAQ: <http://www.barebones.com/support/lists/bbedit_talk.shtml>
List archives: <http://www.listsearch.com/BBEditTalk.lasso>
To unsubscribe, send mail to: <bbedit-talk-***@barebones.com>
Errol Sayre
2008-01-29 15:00:53 UTC
Permalink
IF you're comfortable with php, it would be quite easy to do a
randomize on the file.

<?php
if ($argc > 1)
{
ini_set('auto_detect_line_endings', true);
$lines = file($argv[1]);
$count = count($lines);
while ($count > 0)
{
$index = rand(0, count($lines));
echo $lines[$index];
array_splice($lines, $index, 1);
$count = count($lines);
}
}
else
{
echo 'Usage: php -f randomize.php Infile.txt'."\n";
}
?>

The php "file" function automatically works with your local file
extension, reads the file 1 line per array item, then outputs them
randomly including the original line ending. This script coupled with
output redirection (i.e. "php -f randomize.php Input.txt >
Output.txt") can do what you want very quickly.
Post by Paul Cezanne
I needed to do this also, but for about 4,000 lines. Frankly, I just
used Excel. I dropped a random number into a column, sorted on that
column, and then deleted the column.
Worked like a charm.
-----Original Message-----
From: BBEdit-Talk List on behalf of After Hours
Sent: Fri 1/25/2008 11:01 AM
To: BBEdit-Talk List
Subject: Randomize lines
I have an unusual need to randomize a text file with about 500 lines
(defined by CR line endings).
Is there a method I've not discovered in BBEdit that will randomize
such a text file?
thx.
--
------------------------------------------------------------------
Have a feature request? Not sure the software's working correctly?
List FAQ: <http://www.barebones.com/support/lists/bbedit_talk.shtml>
List archives: <http://www.listsearch.com/BBEditTalk.lasso>
--
------------------------------------------------------------------
Have a feature request? Not sure the software's working correctly?
List FAQ: <http://www.barebones.com/support/lists/bbedit_talk.shtml>
List archives: <http://www.listsearch.com/BBEditTalk.lasso>
--
------------------------------------------------------------------
Have a feature request? Not sure the software's working correctly?
If so, please send mail to <***@barebones.com>, not to the list.
List FAQ: <http://www.barebones.com/support/lists/bbedit_talk.shtml>
List archives: <http://www.listsearch.com/BBEditTalk.lasso>
To unsubscribe, send mail to: <bbedit-talk-***@barebones.com>
pete boardman
2008-01-30 13:43:32 UTC
Permalink
A BBEdit Unix filter, if you've installed newLISP:

#!/usr/bin/env newlisp
(map println (randomize (parse (read-file ((main-args) 2)) "\n" 0)))
(exit)
--
------------------------------------------------------------------
Have a feature request? Not sure the software's working correctly?
If so, please send mail to <***@barebones.com>, not to the list.
List FAQ: <http://www.barebones.com/support/lists/bbedit_talk.shtml>
List archives: <http://www.listsearch.com/BBEditTalk.lasso>
To unsubscribe, send mail to: <bbedit-talk-***@barebones.com>
Loading...