CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Random Picture Loading

 



Alain
New User

Feb 16, 2002, 9:32 AM

Post #1 of 10 (2106 views)
Random Picture Loading Can't Post

Hi,

I've in one directory all my pictures (only gif and jpg). I would like to show on my starting page which is also perl generated a random picture from this directory. So everytime a user visits my page the picture should be randomly selected. The pictures have even similar names. e.g.

05d2.albumimage.jpg

940d.albumimage.jpg

12r3.albumimage.jpg

...

The pictures are in a database and the first four characters mean its ID number. Can anyone give me tips how I could do this?


Paul
Enthusiast

Feb 16, 2002, 10:54 AM

Post #2 of 10 (2103 views)
Re: [Alain] Random Picture Loading [In reply to] Can't Post

How about just doing something like:

[perl]
opendir DIR, "/some/dir" or die $!;
my @img = grep { /\.(jpg|gif)$/i } readdir(DIR);
closedir DIR;

my $rand = $img[rand @img];
[/perl]

Then $rand has the image name.


(This post was edited by RedRum on Feb 16, 2002, 10:55 AM)


Jasmine
Administrator / Moderator

Feb 16, 2002, 11:55 AM

Post #3 of 10 (2096 views)
Re: [Alain] Random Picture Loading [Golf] [In reply to] Can't Post

Sorry, I couldn't help it -- I really, really tried. And yes, I should be flogged for putting this in the beginners forum. 48 characters


Code
map{$_->[rand@$_]}[grep{/\.(jpg|gif)$/i}glob"*"];


But if you really want to use it, you can keep it a little more flexible and legible:

[perl]
my $img = random_image( '/path/to/image/dir/' );

sub random_image{

my $dir = shift;

return
map { $_->[ rand @$_ ] }
[ grep { /\.(jpg|gif)$/i } glob "$dir*" ];

}
[/perl]

If anyone's interested in knowing how this works, here is a simplified explanation:

grep { /\.(jpg|gif)$/i } glob "$_[0]*"

[url=http://www.perldoc.com/perl5.6.1/pod/func/grep.html]grep is an operator that uses an expression to search a list. The expression in this case is { /\.(jpg|gif)$/i } (files that end jpg and gif files only). This expression needs to know what directory to look in, and [url=http://www.perldoc.com/perl5.6.1/pod/func/glob.html]glob takes care of that by using the wildcard * to look at all files that directory. The brackets around this entire expression create an anonymous array, which is passed to the [url=http://www.perldoc.com/perl5.6.1/pod/func/map.html]map line because map also works on a list.


map { $_->[ rand @$_ ] }

So this line is now working on the results of the grep line (an anonymous array). All it's doing is selecting and returning a random element in the anonymous array.


If you have any questions, or need more clarification, ask away. Smile


(This post was edited by Jasmine on Feb 16, 2002, 12:42 PM)


mhx
Enthusiast / Moderator

Feb 16, 2002, 3:07 PM

Post #4 of 10 (2087 views)
Re: [Jasmine] Random Picture Loading [Golf] [In reply to] Can't Post


Quote

Code
map{$_->[rand@$_]}[grep{/\.(jpg|gif)$/i}glob"*"];



I like that one! I was really wondering for a moment why you're mapping on a single element...
But why are you wasting so many characters? Wink

[perl]
map$_->[rand@$_],[grep/\.(jpg|gif)$/i,<*>];
[/perl]

Also, sorry for posting this in the beginners area... Crazy

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



Jasmine
Administrator / Moderator

Feb 16, 2002, 7:41 PM

Post #5 of 10 (2082 views)
Re: [mhx] Random Picture Loading [Golf] [In reply to] Can't Post

Good one! That was actually my first golf attempt (after months of lurking), so practice will help me shorten them like you did Smile


(This post was edited by Jasmine on Feb 16, 2002, 11:32 PM)


uatt
User

Feb 21, 2002, 7:19 AM

Post #6 of 10 (2066 views)
Re: [Jasmine] Random Picture Loading [Golf] [In reply to] Can't Post

This part is still hard to be understand by a biginner

map{$_->[rand@$_]}

especially tratment in annoynamous array..

thanks for some explanation..


(This post was edited by uatt on Feb 21, 2002, 7:21 AM)


mhx
Enthusiast / Moderator

Feb 21, 2002, 8:30 AM

Post #7 of 10 (2059 views)
Re: [uatt] Random Picture Loading [Golf] [In reply to] Can't Post


Code
map { $_->[rand@$_] } ['red', 'green', 'blue', 'yellow'];


First of all, an anonymous array holding some color strings is created. The anonymous array constructor [ .. ] returns a reference to the newly created array. So this reference is the only item in the list that gets passed to map.

But why would you use map if you list would always consist of only one item? Because you can access that item with the $_ variable. Since the array reference isn't explicitly stored in another variable, this is the only way to access the anonymous array twice.

We need to access the anonymous array twice because we need its size for the rand function and we need to dereference a random element. To get the size of the anonymous array, we explicitly convert the array reference $_ into an array by prefixing the reference with an @. Since rand forces scalar context on its argument, @$_ will return the size of the anonymous array referenced by $_. The rand function will now return a random index into the array. Using the array dereference operator -> on the anonymous array reference $_ again, we can dereference that random item and return it. The random item will be the return value of the map function.

You could write the above more readable (but less golfy) as:


Code
$ref = ['red', 'green', 'blue', 'yellow']; 
$ref->[rand @$ref];


or even without an anonymous array:


Code
@ary = ('red', 'green', 'blue', 'yellow'); 
$ary[rand @ary];


Hope this makes things a little clearer. Smile

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



(This post was edited by mhx on Feb 21, 2002, 9:27 AM)


kojr
Novice

Feb 21, 2002, 8:47 AM

Post #8 of 10 (2053 views)
Re: [mhx] Random Picture Loading [Golf] [In reply to] Can't Post

It is all clear....

Thanks....


Paul
Enthusiast

Feb 21, 2002, 9:08 AM

Post #9 of 10 (2051 views)
Re: [mhx] Random Picture Loading [Golf] [In reply to] Can't Post

>>
$ary = ('red', 'green', 'blue', 'yellow');
$ary[rand @ary];
<<

That would need to be:

@ary = ('red', 'green', 'blue', 'yellow');
$ary[rand @ary];

-OR-

$ary = [qw(red green blue yellow)];
$ary[rand @$ary];


mhx
Enthusiast / Moderator

Feb 21, 2002, 9:26 AM

Post #10 of 10 (2049 views)
Re: [RedRum] Random Picture Loading [Golf] [In reply to] Can't Post


In Reply To
That would need to be:

@ary = ('red', 'green', 'blue', 'yellow');
$ary[rand @ary];


Sure, you're absolutely right, I'll change that. Seems I was a bit too focussed on the formatting problems I had... Crazy

Thanks,
-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives