Perl Cd Bookshelf [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Perl Cd Bookshelf [Electronic resources] - نسخه متنی

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید



8.7. Randomizing All Lines


8.7.1. Problem




You want to copy a file and randomly
reorder its lines.

8.7.2. Solution


Read all lines into an array, shuffle the array using List::Util's
shuffle function, and write the shuffled lines
back out:

use List::Util qw(shuffle);
while (<INPUT>) {
push(@lines, $_);
}
@lines = shuffle(@lines);
foreach (@reordered) {
print OUTPUT $_;
}

8.7.3. Discussion


The easiest approach is to read all lines into memory and shuffle
them there. Because you don't know where lines start in the file, you
can't just shuffle a list of line numbers and then extract lines in
the order they'll appear in the shuffled file. Even if you
did know the byte offsets of the start of each
line, it would probably still be slower because you'd be
seek ing around in the file instead of sequentially
reading it from start to finish.

If you have a version of Perl older than v5.8, you can download the
List::Util module from CPAN.

8.7.4. See Also


The documentation for the standard List::Util module; Recipe 2.6; Recipe 2.7; Recipe 4.18



8.6. Picking a Random Line from a File8.8. Reading a Particular Line in a File




Copyright © 2003 O'Reilly & Associates. All rights reserved.

/ 875