[Chester LUG] Any perl gurus out there ?

David Holden dh at iucr.org
Mon Nov 24 12:17:04 UTC 2008


On Sunday 23 November 2008, Stuart Burns wrote:
> <a href="http://www.site1.com/"><Img src="1.jpg"></a>

Just realized how dependent I am on perl modules, but I think this does it 
(tested once!) -see below.

Stuart did you say you had a sparc system going free?


Dave.



#!/usr/local/bin/perl 

# REQUIREMENTS
use 5.006;
use strict;
use warnings;
use version; our $VERSION = qv('0.0.1');
use Carp;
use vars qw();

# GENERAL MODULES

# LOCAL MODULES

# INITIALIZE
my $maxpics = 20;      # number of pics to output
my $file    = 't1.dat';    # name the file

my @l    = readfile($file);
my @pics = getpics(@l);

# output pics
foreach my $pic (@pics)
{
    print $pic . "\n";
}

sub readfile
{
    my ($file) = @_;
    my @l = ();
    open( my $fh, '<', $file ) || die "cannot open file \"$file\": $!";
    while ( my $line = <$fh> )
    {
        chomp($line);
        my ( $p1, $p2 ) = split /\|/, $line;
        my $h = [];
        push @$h, $p1, $p2;
        push @l, $h;

    }
    close $fh or die "unable to close: $!";
    return @l;
}

sub getpics
{
    my @l       = @_;
    my @rv      = ();
    my $c       = 1;
    my %seen    = ();
    my $numpics = $maxpics;
    $numpics = $#l if ( $numpics > $#l );
    while ( $c <= $numpics )
    {
        my $rand = int rand $numpics;
        if ( not $seen{$rand} )
        {
            my $h = $l[$rand];
            my ( $p1, $p2 ) = @$h;
            push @rv, qq{<a href="$p2"><Img src="$p1"></a>};
            $c++;
        }
        $seen{$rand}++;
    }
    return @rv;
}
1;


-- 
Dr. David Holden.

See: <http://www.gnu.org/philosophy/no-word-attachments.html>
regarding Word or PowerPoint. GPG key available on request.
-------------------------------------------------------------




More information about the Chester mailing list