Forum Moderators: coopster & phranque

Message Too Old, No Replies

perl subroutine reading through a text file while...

doesn't seem to work quite right...argh.

         

jeremy goodrich

3:30 pm on May 2, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[perl]

The following is a subroutine I'm using for a script to process a bunch of data, one chunk at a time. The problem is the data amounts both need to be looped over, so I'm constructing a new script to process each file, 500 at a time, and then write the output.

This subroutine takes in $testdata, a file handle, and $c a number at which to start adding the data to the data array, '@emailsText' and $timetowrite, which is the upper limit of the set of data I'm trying to get into the array.

It's returning numbers, instead of data pieces...does anybody have any ideas on this one?

[perl]
sub openText {
($testdata,$timetowrite,$c) = (@_);
my ($email, $blank);
open STUFF, "$testdata" or die "noop";
while (<STUFF>) {
($key, $value)= split (/ /, $_);
# $email = $_;
if ($c le $key) {
push @emailsText, $value;
# print "added email to the array\n";
$_++; }
elsif ($timetowrite eq $key) {
close (STUFF);
return @emailsText; }

elsif ($blank eq $key) {
close (STUFF);
return @emailsText;
}
$_++;
}
close (STUFF);
return 0;
}
[/perl]

Damian

6:28 pm on May 2, 2002 (gmt 0)

10+ Year Member



Maybe you need to quote the @emailsTExt array ? ( with quotes "" or brackets() )

Here's my thinking:

[perl]
@array = ("hello","world");

$noquotes = @array;
$withquotes = "@array";

print "Array syntax, NO quotes = $noquotes"; # prints 2
print "<br>Array syntax, WITH quotes = $withquotes"; # prints the words
[/perl]

jeremy goodrich

7:03 pm on May 2, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the reply, Damian. Though it seems I got this part to work well enough....now, when it loops through this routine, it doesn't stop at the end of the file (that $testdata is a reference for).

This lame piece of code is "supposed" to check to see if the line is blank:
if ($blank == $key) {
close (STUFF);
return @emailsText;
}

However, that's not working...the rest, seems to work fine.

(modified sub routine follows) Any thoughts?

[perl]
sub openText {
($testdata,$timetowrite,$c) = (@_);
my $blank = '';
open STUFF, "$testdata" or die "noop";
foreach (<STUFF>) {
($key, $value)= split (/ /, $_);
# $email = $_;
if ($c <= $key ) {
push @emailsText, $value;
print "added $value to the array\n"; }
if ($timetowrite == $key) {
close (STUFF);
return @emailsText; }

if ($blank == $key) {
close (STUFF);
return @emailsText;
}
}
close (STUFF);
return 0;
}
[/perl]

amoore

7:38 pm on May 2, 2002 (gmt 0)

10+ Year Member



You might want to 'chop' or 'chomp' your lines ($_) before you split them so that you get rid of the newline characters. That's probably keeping your blank test from working. Also, you could just depend on your foreach statement ending when you get to the end of the file. return your array instead of 0 in that case.