Gossamer Forum
Quote Reply
Random Perl Global
Hi!,

Bringing this back up from a old post as its more relevant to this forum.

I'm trying to add a random number via a global tag but no luck as I know nowt about programming. The output needs letters and numbers and be as accurate (random) as possible.

So far I have code below but it doesn't work (errors):

<%random%>

sub {
# Displays random number/letter.
my @dig = ('0..9', 'a..z', 'A..Z');
srand(time ^ $$);
for (my $i = 0; $i++ < 8;) {
$RAND .= $dig[rand(@dig)];
}
}

any help with this would be great and I'm sure other could use this too.

Quote Reply
Re: [Canoon] Random Perl Global In reply to
That code looks familiar Tongue

Last edited by:

RedRum: Sep 27, 2001, 1:13 PM
Quote Reply
Re: [RedRum] Random Perl Global In reply to
Hi RR,

Yes that is the same code but was in the wrong forum for my question.

What's - site_html_templates.pl I don't see this in links SQL directory?
Quote Reply
Re: [Canoon] Random Perl Global In reply to
Sorry I was talking about Links2....anyway....

That code should work as a global.

What is happening when you put <%random%> into a template?

Last edited by:

RedRum: Sep 27, 2001, 1:15 PM
Quote Reply
Re: [Canoon] Random Perl Global In reply to
Oh

Between

}
}

You need to add

return $RAND;
Quote Reply
Re: [RedRum] Random Perl Global In reply to
Thanks,

I have:

sub {
# Displays random numbers/letters.
my @dig = ('0..9', 'a..z', 'A..Z');
srand(time ^ $$);
for (my $i = 0; $i++ < 8;) {
$RAND .= $dig[rand(@dig)];
}
return $RAND;
}


And the error:

Unable to compile 'random'. Reason: Global symbol "$RAND" requires explicit package name at (eval 7) line 6. Global symbol "$RAND" requires explicit package name at (eval 7) line 8.

Last edited by:

Canoon: Sep 27, 2001, 1:35 PM
Quote Reply
Re: [Canoon] Random Perl Global In reply to
Ack can't believe I missed that.

Under

my @dig = ('0..9', 'a..z', 'A..Z');

add...

my $RAND;
Quote Reply
Re: [RedRum] Random Perl Global In reply to
That works but...

the output is pretty weird:

A..Z0..90..9a..zA..Za..za..zA..Z

and slight variations of that on each page refresh.

Could it go: 4798dfhy47 etc?
Quote Reply
Re: [RedRum] Random Perl Global In reply to
Oops :)

Try:

my @dig = qw('0'..'9', 'a'..'z', 'A'..'Z');

Quote Reply
Re: [RedRum] Random Perl Global In reply to
HeHe! this gets more fun

Now I have:

'A'..'Z''0'..'9','A'..'Z''0'..'9','a'..'z','a'..'z','a'..'z','a'..'z',

Shocked
Quote Reply
Re: [Canoon] Random Perl Global In reply to
Try:

random =>
Code:
sub {
my @random = (0 .. 9, 'a' .. 'z', 'A' .. 'Z');
my $rand = '';
for (1 .. 8) {
$rand .= $random[rand @random];
}
return $rand;
}

Cheers,

Alex
--
Gossamer Threads Inc.

Last edited by:

Alex: Sep 27, 2001, 3:25 PM
Quote Reply
Re: [Alex] Random Perl Global In reply to
Thanks a lot.

The result output is: z0....9,9,a..

but would be much better if I could get something like: ab10cv023fy6 as this will be used to authenticate a links submission.

Random number on add page > sent via email > updates changed by me with authenticated matching number on record.

Thanks for you help.
Quote Reply
Re: [Canoon] Random Perl Global In reply to
Ooops, try the edited version. =)

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Random Perl Global In reply to
Still not there I'm afraid.

Error:

Unable to compile 'random'. Reason: Bareword "z" not allowed while "strict subs" in use at (eval 7) line 2.
Bareword "a" not allowed while "strict subs" in use at (eval 7) line 2.
Quote Reply
Re: [Canoon] Random Perl Global In reply to
Third time a charm.. ;)

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Random Perl Global In reply to
A charm indeed! :)

Thanks to both for all suggestions and codes.


Final code for other who are interested...

Build/Templates Globals:

Rand_Numlett

sub {
# Displays random num/lett on page load.
my @random = (0 .. 9, 'a' .. 'z', 'A' .. 'Z');
my $rand = '';
for (1 .. 8) {
$rand .= $random[rand @random];
}
return $rand;
}


In template:

<%Rand_Numlett%>

Quote Reply
Re: [Alex] Random Perl Global In reply to
Haha good to see I'm not the only one....Angelic

Last edited by:

RedRum: Sep 28, 2001, 3:49 AM
Quote Reply
Re: [Alex] Random Perl Global In reply to
I want to use the global for a bannercode, but the code has 3 parts, and each part needs the same random-number.

The global brings me 3 different random-numbers when I use the global 3 times on the same site.

--
Michael Skaide

http://www.cycle24.de