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: Intermediate:
Need help with file print

 



newera
Novice

May 24, 2005, 10:07 AM

Post #1 of 7 (874 views)
Need help with file print Can't Post

Want to use the following script to print a random URL to a file then display it in a browser......but......when we run it it prints the new file as 2 separate lines. Can't figure it out.
Prints the file like this:

http://wm-mw.org/cgi-bin/v.cgi/dm621
/index_en.html

Here's the code:


Code
#! /usr/bin/perl -- 
require "/home/bruce/public_html/cgi-bin/wm/configure.cgi";
&configure;

$language = "$ENV{'QUERY_STRING'}";

open (LINKS, "$linkfile");

srand(); # kick rand
$nlines=@file=<LINKS>; # inhale file & get # of lines

$link = $file[int rand $nlines];
$page = "/index_$language.html";
$coopurl = "$link$page";
close (LINKS);

$linkfile2 = "/home/bruce/data/coopurls2.txt";

open (FILE, ">$linkfile2");
flock(FILE, 2);
print FILE "$coopurl";
flock(FILE, 8);
close (FILE);

open(NUMBER1,"/home/bruce/data/coopurls2.txt");
$newlink = <NUMBER1>;
close(NUMBER1);

print "Location: $newlink\n\n"; # print a random line

exit;


Any ideas?


KevinR
Veteran


May 24, 2005, 11:38 AM

Post #2 of 7 (873 views)
Re: [newera] Need help with file print [In reply to] Can't Post

when you slurp in the lines of the file into @file they have a newline character on the end. Just chomp the variable $link before printing it to file:


Code
$link = $file[int rand $nlines];  
chomp($link);

-------------------------------------------------


(This post was edited by KevinR on May 24, 2005, 11:41 AM)


KevinR
Veteran


May 24, 2005, 11:46 AM

Post #3 of 7 (869 views)
Re: [KevinR] Need help with file print [In reply to] Can't Post

this section of code:


Code
srand();                        # kick rand  
$nlines=@file=<LINKS>; # inhale file & get # of lines

$link = $file[int rand $nlines];


could just be written like this:


Code
@file=<LINKS>;          # inhale file & get # of lines  
$link = $file[int(rand(@file))];


srand is only for older versions of perl (before version 5 I believe). @file will be interpolated in scalar context and return the number of elements in the array in this line:

$link = $file[int(rand(@file))];
-------------------------------------------------


newera
Novice

May 24, 2005, 11:47 AM

Post #4 of 7 (868 views)
Re: [KevinR] Need help with file print [In reply to] Can't Post

It does work putting the chomp for the $link.

Thanks Kevin!


(This post was edited by newera on May 24, 2005, 11:52 AM)


KevinR
Veteran


May 24, 2005, 11:53 AM

Post #5 of 7 (865 views)
Re: [newera] Need help with file print [In reply to] Can't Post

I edited my first reply, if you had read the first reply I posted it was not correct, I chomped the wrong variable. Reread my first post, it should solve the problem by chomping $link
-------------------------------------------------


KevinR
Veteran


May 24, 2005, 11:53 AM

Post #6 of 7 (864 views)
Re: [newera] Need help with file print [In reply to] Can't Post

OK... I see you got it Smile
-------------------------------------------------


davorg
Thaumaturge / Moderator

May 25, 2005, 4:58 AM

Post #7 of 7 (858 views)
Re: [KevinR] Need help with file print [In reply to] Can't Post

The array indexing brackets know they need an integer, so they implicitly truncate the expression to an integer. Therefore you don't need the explicit call to int


Code
$link = $file[rand(@file)];


This message was brought to you by the Campaign for the Conservation of Keystrokes.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


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

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