Okay, so it could be a more useful sigfile tool, but it is nifty to play around with. ("How many sigs do I have quoted from such-and-such a newsgroup?" "How many of my sigs contain the word 'f*ck'?" etc.) Maybe, some day, I'll write more useful stuff along these lines.

What really impressed me when I was writing this is how easy it was to write. Took me less than five minutes. I like Perl.

(It's useful to know that my sigs are stored in a %-separated text file; sort of like a fortune file.)

Update: Changed "map {chop} @sigs" to "chomp @sigs". Thanks blakem!

#! /usr/bin/perl -w # sigrep -- grep sigs for a string use strict; my $pattern = $ARGV[0]; my @sigs = (); { local $/ = '%'; open SIGS, "<sigs" or die "Cannot open sigs file: $!\n"; @sigs = <SIGS>; chomp @sigs; close SIGS; } my @matches = grep /$pattern/, @sigs; foreach (@matches) { print "---\n$_\n"; } print "---\n";
--
:wq

Replies are listed 'Best First'.
Re: Searching a collection of signatures
by blakem (Monsignor) on Nov 07, 2001 at 11:25 UTC
    Nice, though I do have a few suggestions about this line:
    map {chop} @sigs;
    First of all, its always safer to use chomp, since it will only remove whatever your OS uses for newlines. If there isn't a newline after the last sig in your file, your chop version will remove the last char in the sig, chomp wont.
    map {chomp} @sigs;
    Second we're calling map in void context which is expensive. map generates a new array for us, that we don't need or use -- i.e. @newarray = map {chomp} @sigs; Such "void maps" are better written as for loops...
    chomp for @sigs;
    But as it turns out, chomp can take a list as an argument, so the above line can be shortened to:
    chomp @sigs;

    -Blake

      In fact, I tried chomp first, because I'm not trying to remove newlines, just trailing %s, which chop will happily eat. That said, I can probably just

      s/%$//m for @sigs;
      or some such instead.

      --
      :wq
        After rereading the chomp docs, I realized that chomp will behave like the above regex when $/ is set to '%'...
        # One way.... { local $/ = '%'; chomp @sigs; # chomp uses the value of $/ to figure out what to rem +ove } # or another s/%$// for @sigs;

        -Blake

Re: Searching a collection of signatures
by mr_mischief (Monsignor) on Nov 08, 2001 at 01:33 UTC
    I have a random signature picker for my email and newsgroup postings. It uses '%' at the start of a line so it will work with regular fortune files. It defaults to a file ~/.taglines if no arguments are given on the command line, or it will allow you to give it a file to read from.

    It also will allow a semicolon at the beginning of a line to act as a comment. I did that so I could keep file version data at the top of the file, and it'l let me extend it in the future to maybe include metainfo such as extended quote attributions, definitions or etymology for a word, or editorial quips which are only included when over four lines is considered proper or the extra detail is really wanted.

    If we could make yours and mine both work with the extended info, it'd let people search for fortunes within a larger file attributed to a particular person and such.

    Here's mine:
    #!/usr/bin/perl -w use strict; my $homedir = $ENV{HOME}; my $file; if ( $ARGV[0] ) { $file = $ARGV[0]; } else { $file = "$homedir/.taglines"; } open(TL, "$file") || die "$file could not be read: $!\n"; my $tags = 0; while(<TL>) { $tags++ if /^%$/; } my $which_tag = int(rand($tags)); $tags = 0; seek(TL, 0,0); while(<TL>) { if(/^%$/) { $tags++; if($tags > $which_tag) { exit(0); } } elsif(/^;/) { } elsif($which_tag == $tags) { print("$_"); } } exit(0);