Forum Moderators: coopster & phranque

Message Too Old, No Replies

Script not recognizing new line in while loop

         

hannamyluv

8:24 pm on Jan 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Okay here's the deal, I have a script that I use to do some static page content management page creation. I copied it over to a subfolder in the cgi-bin so that I could make a few changes and use it for content management on another similar but different looking page. Point being that, other than a few cosmetic changes and that one script is in the top level cgi-bin and the other is in a subfolder of the cgi-bin, they are the same basic file.

In the top level folder, the script snippet below does fine. Opens a text file that holds a list of pages that are one the site so the can be worked into the new page. In the subfolder, the it opens the text file, parses the first line and then seems to fail to recognize the new line and just fizzles out.


open LINKLIST, "$text" or die $!;

while (<LINKLIST>) {
local $/ = undef;
@catfile = split/\t/,$_;
$artname = $catfile[0];
$artlink = $catfile[1];
$alphalist{$artname} = $artlink;
}

I am just a perl hack, so I can't even fathom why this might happen. I have tried reuploading the text file several times and that has not helped. Would anybody know?

perl_diver

9:13 pm on Jan 19, 2006 (gmt 0)

10+ Year Member



is there only one line in the $text file?

bennymack

9:59 pm on Jan 19, 2006 (gmt 0)

10+ Year Member



local $/ = undef;
doesn't seem to be necessary.

Lets revise this code snippet a bit:


use strict;
use warnings;

open my $LINKLIST, '<',$text or die $!;

while (<$LINKLIST>) {
my($artname,$artlink) = split/\t/,$_;
$alphalist{$artname} = $artlink;
}
close $LINKLIST;

--Untested--

balam

4:46 am on Jan 20, 2006 (gmt 0)

10+ Year Member



I'll agree with bennymack, the "local [...]" line is unnecessary. And it could be the root of the problem, given that that command undefines the default record separator of "newline."

This has been tested & works:



use strict;
use warnings;


my($text) = 'testfile.txt';
my(%alphalist);


open (LINKLIST, "<$text") or die $!;


while (<LINKLIST>) {
chomp;
my($artname, $artlink) = split;
$alphalist{$artname} = $artlink;
}


close(LINKLIST);

perl_diver

7:09 am on Jan 20, 2006 (gmt 0)

10+ Year Member



well, if it's a single line of text, most of the code is not needed:

open LINKLIST, "$text" or die "$!";
chomp (my @catfile = split(/\t/,<LINKLIST>));
$alphalist{$catfile[0]} = $catfile[1];
close LINKLIST;

balam

2:25 pm on Jan 20, 2006 (gmt 0)

10+ Year Member



> Opens a text file that holds a list of pages

hannamyluv

3:38 pm on Jan 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I tried the more condensed code with the undef removed and it is still doing it.

I read an article that said it may be an FTP issue in that the FTP is changing the file, but it said no more than that. Does anybody have any experience with this happening?

bennymack

5:52 pm on Jan 20, 2006 (gmt 0)

10+ Year Member



Could you describe
fizzles out
in a bit more detail?

Also, have you verified that the file in question is correct? As in, more than one line and tab separated values.

hannamyluv

6:38 pm on Jan 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have verified that it is the correct file. I even tried using the file that I use on the cgi-bin script, the one that works. That file works in the cgi-bin script but fizzels in the cgi-bin/subfolder script.

This is what happens. The file looks like this:

Article A(tab)http://www.example.com/art1.htm
Article B(tab)http://www.example.com/art2.htm
Article C(tab)http://www.example.com/art3.htm

I added a print line to see what it was picking up and this is what it will look like.

print "$artname - $artlink\n";

Prints this:

Article A - http://www.example.com/art1.htm Article B

And then the while loop stops working.

perl_diver

6:58 pm on Jan 20, 2006 (gmt 0)

10+ Year Member




> Opens a text file that holds a list of pages

A list could still be a single line of text:

foo\tbar\tbaz\tboink

but the OP cleared that up, it is multiple lines.

I have no idea why the problem is occuring that he describes. Having the file in a sub folder of the cgi-bin should make no difference that I am aware of.

perl_diver

6:59 pm on Jan 20, 2006 (gmt 0)

10+ Year Member



the only thing I can think of is that if the left side, like Article A is the same for all lines then the hash key is just getting overwritten, but I don't think thats the problem.