http://qs321.pair.com?node_id=168673

A really short little ditty to take a file delimited by '%' characters (on lines by themselves) then randomly select an entry, then print it to the .signature file.

Literally taken straight from my ~/scripts directory, all still hardcoded. Pick it to pieces, but remember it's just a hack :)

First few lines of my .signature.rot file. Note the first line is blank, and there needn't be a trailing newline at the end:

Leo: (July 23 - Aug. 22) When driving through the desert, you should never stop and eat mysterious piles of birdseed, even if "FREE BIRDSEED" signs are stuck in them. % "Reality is that which, when you stop believing in it, doesn't go away". -- Philip K. Dick % So, Lone Starr, now you see that evil will always triumph, because good is dumb. -- Dark Helmet % Thank you for pressing the self-destruct button. This ship will self-destruct in three minutes. % Do you make up these questions, Mr. Holden, or do they write them down for you? % Yes, questions. Morphology, longevity, incept dates. % "Begun, this clone war has." Donate to the House With No Sentence Structure. Dedicated to assisting grammatically disadvantaged Jedi for over 900 years.
#!/usr/bin/perl use IO::File; $/ = "%"; open(FILE, "/home/hagus/.signature.rot"); while (<FILE>) { $tmp = $_; $tmp =~ s/%//g; push @a, $tmp; } close(FILE); my $fh = new IO::File("/dev/urandom", "r"); my $foo = $fh->read($value, 100); $fh->close(); my $num = unpack("S*", $value); my $index = $num % (@a + 0); open(FILE, ">/home/hagus/.signature"); print FILE $a[$index] . "\n"; close(FILE);

Replies are listed 'Best First'.
Re: Signature rotation.
by Juerd (Abbot) on May 23, 2002 at 07:12 UTC

    $/ = "%";

    Why not "\n%\n"?

    Oh, and just because I like Tie::File and strict, and dislike using /dev/urandom, here's an alternative implementation:

    #!/usr/bin/perl -w use Tie::File; use IO::File; use Fcntl qw(O_RDONLY); use strict; chdir '/home/hagus'; tie my @fortunes, 'Tie::File', '.signatures.rot', mode => O_RDONLY, re +csep => "\n%\n"; IO::File->new('.signature', 'w')->print($fortunes[rand @fortunes]);

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

      Great! I like this.

      I've never used Tie before actually, so this is good sample code for me.

      Why not "\n%\n" ? Because I coded that in about 3 nanoseconds, and it didn't even occur to me. Maybe some C oriented portion of my brain baulked at using more than one character as a separator?!

      I think I had a reason for using /dev/urandom instead of rand. Who wants to remind me what it was? I've forgotten.

      --
      Ash OS durbatulk, ash OS gimbatul,
      Ash OS thrakatulk, agh burzum-ishi krimpatul!
      Uzg-Microsoft-ishi amal fauthut burguuli.