Jump to content

print to indirect filehandle


Recommended Posts

ok, I'm messing with the code block you put up. but it does not loop properly.

$input1 = "input1.txt";
$input2 = "input2.txt";
open (FH1, "< $input1") or die "Can't open $file for read: $!";
open (FH2, "< $input1") or die "Can't open $file for read: $!";
@array1 = <FH1>;
@array2 = <FH2>;
close FH1 or die "Cannot close $file: $!";
for ($i = 0; $i <= 100; $i++) {
$filename = @array1[i];
  $redirect_name = @array2[i];
open FH3,"> $filename" || die "Can't open $_\n";
print "<?\n";
print "header(\"HTTP/1.1 301 Moved Permanently\");\n";
print "header(\"Location: http://www.overclockersclub.com/reviews/$name\");\n";
print "//add close header for IE\n";
print "header (\"Connection: close\");\n";
print "?>\n";
}

Share this post


Link to post
Share on other sites

So long as you have figured that what I posted was not intended to be exact copy-pastable PERL code you probably just need to look up how to get the size of an array.

 

Posting the code would have also helped.

 

edit:

- Looks like you need to also look up how to address the array (as in get values out of the array) :P It's not an @.

- The open() for FH2 shouldn't be $input1 again either. It should be $input2 there.

- You also seem to have forgotten to close FH2.

- You've got $redirect_name but later on you're using $name.

Edited by markiemrboo

Share this post


Link to post
Share on other sites

I did.

 

$input1 = "input1.txt";
$input2 = "input2.txt";
open (FH1, "< $input1") or die "Can't open $file for read: $!";
open (FH2, "< $input2") or die "Can't open $file for read: $!";
@array1 = <FH1>;
@array2 = <FH2>;
close FH1 or die "Cannot close $file: $!";
close FH2 or die "Cannot close $file: $!";
chdir tmp;
for ($i = 0; $i <= 10; $i++) {
$filename = @array1[$i];
$name = @array2[$i];
open FH3,"> $filename" || die "Can't open $_\n";
print FH3 "<?\n";
print FH3 "header(\"HTTP/1.1 301 Moved Permanently\");\n";
print FH3 "header(\"Location: http://www.overclockersclub.com/reviews/$name\");\n";
print FH3 "//add close header for IE\n";
print FH3 "header (\"Connection: close\");\n";
print FH3 "?>\n";
close FH3 or warn "Cannot close $file: $!";
}
print @array1

Share this post


Link to post
Share on other sites

My guess then is that you didn't have 11 entries of files in input1.txt for testing.

 

Cleaned it up for you somewhat........

 

my @array1 = ();
my @array2 = ();

my $input1 = "input1.txt";
my $input2 = "input2.txt";

open (FH1, "< $input1") or die "Can't open $input1 for read: $!";
  @array1 = <FH1>;
close (FH1) or die "Cannot close $file: $!";

open (FH2, "< $input2") or die "Can't open $input2 for read: $!";
  @array2 = <FH2>;
close (FH2) or die "Cannot close $file: $!";

#chdir("tmp");

my $arraysize = @array1;
for ($i = 0; $i < $arraysize; $i++)
{
  my $filename = $array1[$i];
  my $name = $array2[$i];

  # remove new line
  $name =~ s/\n//g;

  open(FH3,"> $filename") || die "Can't open $filename: $!\n";


my $the_string = <<END;
<?
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.overclockersclub.com/reviews/${name}");
header("Connection: close");
?>
END

  print FH3 "$the_string";
  close(FH3) or warn "Cannot close $filename: $!";
}

Share this post


Link to post
Share on other sites

Thanks dude!

 

Nextly, the purpose of this little thing is to automate populating input1. Problem is the spaces. If I take them out perl screams that i'm trying to use _$. The underscore is part of the filename I need.

$name = "forsa_7900_gt";
for ($count=1; $count<11; $count++)
{
print "$name\ _\ $count.htm\n";
}

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...