|
|
Mozy Online Backup: 2GB Free. Automatic. Secure.
Subject: Re: Perl 'Expert' Quiz-of-the-Week #13 - msg#00012
List: lang.perl.qotw.discuss
Was this page helpful?
Thread at a glance:
Previous Message by Date:
click to view message preview
Re: Perl 'Expert' Quiz-of-the-Week #13
"Brent Dax" <brentdax-a09SyBuiYrA@xxxxxxxxxxxxxxxx>:
> Can you provide a tarball of test data for those of us who don't use
> this system?
I put one at
http://perl.plover.cmo/qotw/misc/e013/sample.tgz
The result of 'scan' on this folder is:
1 01/31 craigm-37P8GvhMPymIwRZHo2/mJg@xxxxxxxxxxxxxxxx Unique hosting
service<<IMV INTERNET DEVELOPERS
4 10/15 lchaki-HQcOjwhNS1d/Anqf9SJyXg@xxxxxxxxxxxxxxxx New era of online
publishing<<ADVANCED CONTROL
28 04/16 Broadcast Intervi Inclusion in the 1996 Yearbook<<Dear Webmaster:
57 10/20 To:ThinkerLA@aol. Re: The Thinker<<I've already told you twice to
142 02/15 (Hmmm.Good@domain "Sex Muffins"<<Discover the secret to a wild an
857 07/18 "Andy" Need Good People<<*Earn $2000 - $5000 weekly-st
1428 08/12 "Carla21666@point RESEARCH - BURN FAT WHILE YOU SLEEP!! (356011)<
5714 01/15 Send-A-Song "Songmail" -- Unique Gift Idea!<<Tired of sendi
If I do 'sortby', and then 'scan', I get:
1 04/16 Broadcast Intervi Inclusion in the 1996 Yearbook<<Dear Webmaster:
4 07/18 "Andy" Need Good People<<*Earn $2000 - $5000 weekly-st
28 10/15 lchaki-HQcOjwhNS1d/Anqf9SJyXg@xxxxxxxxxxxxxxxx New era of online
publishing<<ADVANCED CONTROL
57 08/12 "Carla21666@point RESEARCH - BURN FAT WHILE YOU SLEEP!! (356011)<
142 02/15 (Hmmm.Good@domain "Sex Muffins"<<Discover the secret to a wild an
857 01/15 Send-A-Song "Songmail" -- Unique Gift Idea!<<Tired of sendi
1428 10/20 To:ThinkerLA@aol. Re: The Thinker<<I've already told you twice to
5714 01/31 craigm-37P8GvhMPymIwRZHo2/mJg@xxxxxxxxxxxxxxxx Unique hosting
service<<IMV INTERNET DEVELOPERS
If I do "sortby -f from" and then 'scan', I get:
1 07/18 "Andy" Need Good People<<*Earn $2000 - $5000 weekly-st
4 04/16 Broadcast Intervi Inclusion in the 1996 Yearbook<<Dear Webmaster:
28 08/12 "Carla21666@point RESEARCH - BURN FAT WHILE YOU SLEEP!! (356011)<
57 01/15 Send-A-Song "Songmail" -- Unique Gift Idea!<<Tired of sendi
142 01/31 craigm-37P8GvhMPymIwRZHo2/mJg@xxxxxxxxxxxxxxxx Unique hosting
service<<IMV INTERNET DEVELOPERS
857 02/15 (Hmmm.Good@domain "Sex Muffins"<<Discover the secret to a wild an
1428 10/15 lchaki-HQcOjwhNS1d/Anqf9SJyXg@xxxxxxxxxxxxxxxx New era of online
publishing<<ADVANCED CONTROL
5714 10/20 To:ThinkerLA@aol. Re: The Thinker<<I've already told you twice to
(The 'from' field of #57 here is actually
"cgn-vB5rODKJlosybS5Ee8rs3A@xxxxxxxxxxxxxxxx (Send-A-Song)".)
Next Message by Date:
click to view message preview
[SPOILER] Expert QOTW #13
Here is a solution for the expert QOTW #13. My methodology was that I
found out to what each file needs to be renamed to. Then I found the
renaming loops[1] and renamed the files inside them by renaming the first
file to a temporary name.
Regards,
Shlomi Fish
[1] - Since each file is renamed to one other file and each file is
renamed from one other file, one can show that they are renamed in loops:
A -> B -> C -> D -> A
#!/usr/bin/perl -w
use strict;
use Mail::Box;
use Mail::Box::MH;
use Getopt::Long;
my $field = "subject";
my $reverse = 0;
my $result = GetOptions("f" => \$field, "r" => \$reverse);
my $dir_path = shift || "$ENV{HOME}/Mail/inbox";
my $folder = Mail::Box::MH->new('folder' => $dir_path);
my $idx = 0;
my @messages;
while (my $msg = $folder->message($idx++))
{
# print $msg->messageId(), "\n";
my $filename = $msg->filename();
$filename =~ /\/([^\/]*)$/;
$filename = $1;
my $f;
if ($field eq "from")
{
$f = $msg->from();
}
elsif ($field eq "to")
{
$f = $msg->to();
}
else
{
$f = $msg->subject();
}
push @messages, { 'filename' => $filename, 'field' => $f };
}
undef($folder);
my @sorted_indexes =
sort {
($messages[$a]->{'field'} cmp $messages[$b]->{'field'}) ||
($a <=> $b)
} (0 .. $#messages);
if ($reverse)
{
@sorted_indexes = reverse(@sorted_indexes);
}
my @messages_moved = (0) x @messages;
my $temp_filename = "a6Hy0";
while (-e "$dir_path/$temp_filename")
{
$temp_filename++;
}
my $i;
chdir($dir_path);
sub myrename
{
my ($from, $to) = @_;
print "Renaming $from to $to\n";
rename($from, $to);
}
for($i=0;$i<@messages;$i++)
{
if (!$messages_moved[$i])
{
# Check if we move the message to itself
# if so - do nothing except mark this message
# as moved
if ($sorted_indexes[$i] == $i)
{
$messages_moved[$i] = 1;
}
else
{
myrename($messages[$i]->{'filename'}, $temp_filename);
my ($prev_idx, $next_idx);
$prev_idx = $i;
$next_idx = $sorted_indexes[$prev_idx];
while ($next_idx != $i)
{
myrename(
$messages[$next_idx]->{'filename'},
$messages[$prev_idx]->{'filename'}
);
$messages_moved[$prev_idx] = 1;
$prev_idx = $next_idx;
$next_idx = $sorted_indexes[$prev_idx];
}
$messages_moved[$prev_idx] = 1;
myrename($temp_filename, $messages[$prev_idx]->{'filename'});
}
}
}
----------------------------------------------------------------------
Shlomi Fish shlomif-8zc6hDYHBFIOpacJJkBjfT4kX+cae0hd@xxxxxxxxxxxxxxxx
Home Page: http://t2.technion.ac.il/~shlomif/
An apple a day will keep a doctor away. Two apples a day will keep two
doctors away.
Falk Fish
Previous Message by Thread:
click to view message preview
Re: Perl 'Expert' Quiz-of-the-Week #13
"Brent Dax" <brentdax-a09SyBuiYrA@xxxxxxxxxxxxxxxx>:
> Can you provide a tarball of test data for those of us who don't use
> this system?
I put one at
http://perl.plover.cmo/qotw/misc/e013/sample.tgz
The result of 'scan' on this folder is:
1 01/31 craigm-37P8GvhMPymIwRZHo2/mJg@xxxxxxxxxxxxxxxx Unique hosting
service<<IMV INTERNET DEVELOPERS
4 10/15 lchaki-HQcOjwhNS1d/Anqf9SJyXg@xxxxxxxxxxxxxxxx New era of online
publishing<<ADVANCED CONTROL
28 04/16 Broadcast Intervi Inclusion in the 1996 Yearbook<<Dear Webmaster:
57 10/20 To:ThinkerLA@aol. Re: The Thinker<<I've already told you twice to
142 02/15 (Hmmm.Good@domain "Sex Muffins"<<Discover the secret to a wild an
857 07/18 "Andy" Need Good People<<*Earn $2000 - $5000 weekly-st
1428 08/12 "Carla21666@point RESEARCH - BURN FAT WHILE YOU SLEEP!! (356011)<
5714 01/15 Send-A-Song "Songmail" -- Unique Gift Idea!<<Tired of sendi
If I do 'sortby', and then 'scan', I get:
1 04/16 Broadcast Intervi Inclusion in the 1996 Yearbook<<Dear Webmaster:
4 07/18 "Andy" Need Good People<<*Earn $2000 - $5000 weekly-st
28 10/15 lchaki-HQcOjwhNS1d/Anqf9SJyXg@xxxxxxxxxxxxxxxx New era of online
publishing<<ADVANCED CONTROL
57 08/12 "Carla21666@point RESEARCH - BURN FAT WHILE YOU SLEEP!! (356011)<
142 02/15 (Hmmm.Good@domain "Sex Muffins"<<Discover the secret to a wild an
857 01/15 Send-A-Song "Songmail" -- Unique Gift Idea!<<Tired of sendi
1428 10/20 To:ThinkerLA@aol. Re: The Thinker<<I've already told you twice to
5714 01/31 craigm-37P8GvhMPymIwRZHo2/mJg@xxxxxxxxxxxxxxxx Unique hosting
service<<IMV INTERNET DEVELOPERS
If I do "sortby -f from" and then 'scan', I get:
1 07/18 "Andy" Need Good People<<*Earn $2000 - $5000 weekly-st
4 04/16 Broadcast Intervi Inclusion in the 1996 Yearbook<<Dear Webmaster:
28 08/12 "Carla21666@point RESEARCH - BURN FAT WHILE YOU SLEEP!! (356011)<
57 01/15 Send-A-Song "Songmail" -- Unique Gift Idea!<<Tired of sendi
142 01/31 craigm-37P8GvhMPymIwRZHo2/mJg@xxxxxxxxxxxxxxxx Unique hosting
service<<IMV INTERNET DEVELOPERS
857 02/15 (Hmmm.Good@domain "Sex Muffins"<<Discover the secret to a wild an
1428 10/15 lchaki-HQcOjwhNS1d/Anqf9SJyXg@xxxxxxxxxxxxxxxx New era of online
publishing<<ADVANCED CONTROL
5714 10/20 To:ThinkerLA@aol. Re: The Thinker<<I've already told you twice to
(The 'from' field of #57 here is actually
"cgn-vB5rODKJlosybS5Ee8rs3A@xxxxxxxxxxxxxxxx (Send-A-Song)".)
Next Message by Thread:
click to view message preview
Re: Perl 'Expert' Quiz-of-the-Week #13
>>>>> "Mark" == Mark Jason Dominus
>>>>> <mjd-ZxR0713JXSrQT0dZR+AlfA@xxxxxxxxxxxxxxxx> writes:
Mark> The keyboard in the Internet place here in Honolulu is very small.
That's why I use my own laptop even in Honolulu. :)
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn-DiZw3cqpylQkmLvzuZlaBw@xxxxxxxxxxxxxxxx>
<URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
|
|