Open a file and reading in not a problem but....

B

Billy

All,

I have a html file that contains a simple comma delimitated list of items:

~1 Red Apple, 1 Green Apple, 1 Orange,
%item:4%,%item:5%,%item:6%,%item:7%,%item:8%,%item:9%,%item:10^

This list begins with ~ and ends with ^.

I can open the file but never using perl before I am looking for a bit of
help.
Below is what I use to make sure I can even access the file... this part
doesn't give any error but doesn't print anything either..

#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
print "Content-type: text/html\n\n";
open (test, "trialfile.html") || die ("Could not open file. <br> $!");
while (<>) {

if (/~/) {
print;
}
}
close (test);

It opens the file and doesn't print anything except a blank page. What I am
looking for is to have it print everything after ~ but before any % or ^.

I thought it would at least print the ~ but it doesn't....

I know some people who have been programming a while might think this is
easy but for me it is not so any help would be appreciated.

Thank you,

Billy McCain
 
A

Andrew Hamm

Billy said:
I have a html file that contains a simple comma delimitated list of
items:

~1 Red Apple, 1 Green Apple, 1 Orange,
%item:4%,%item:5%,%item:6%,%item:7%,%item:8%,%item:9%,%item:10^

This list begins with ~ and ends with ^.

That doesn't really look like HTML to me, but then again, that's not
important. It's just a data file...
I can open the file but never using perl before I am looking for a
bit of help.
Below is what I use to make sure I can even access the file... this
part doesn't give any error but doesn't print anything either..

#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
print "Content-type: text/html\n\n";
open (test, "trialfile.html") || die ("Could not open file. <br> $!");

This looks reasonable however I do not use CGI so if you are expecting
failure here I can't comment on what die is achieving.
while (<>) {

Here's your real mistake.

while(<test>) {

causes it to read from the test file. That should solve your problem.
reading from <> reads from "standard input" which, from my very limited
understanding of CGI, is probably nothing depending on whether it's using
the POST method or .... the other method (told you I don't know CGI)
 
T

Tintin

Billy said:
All,

I have a html file that contains a simple comma delimitated list of items:

~1 Red Apple, 1 Green Apple, 1 Orange,
%item:4%,%item:5%,%item:6%,%item:7%,%item:8%,%item:9%,%item:10^

Strange HTML file.
This list begins with ~ and ends with ^.

I can open the file but never using perl before I am looking for a bit of
help.
Below is what I use to make sure I can even access the file... this part
doesn't give any error but doesn't print anything either..

#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
print "Content-type: text/html\n\n";
open (test, "trialfile.html") || die ("Could not open file. <br> $!");
while (<>) {

if (/~/) {
print;
}
}
close (test);

#!/usr/bin/perl
use strict;
use warnings;

use CGI::Carp qw(fatalsToBrowser);

open TEST, "trailfile.html" or die "Could not open trailfile.html $!\n";
print "Content-Type: text/html\n\n"; # assuming it really is HTML

while (<TEST>) {
print if (/~/);
}
 
C

Craig Dunn

Strange HTML file.

....snip...

open TEST, "trailfile.html" or die "Could not open trailfile.html $!\n";
print "Content-Type: text/html\n\n"; # assuming it really is HTML

while (<TEST>) {


I beleive you also wanted to only print after ~ and before ^ or %, so this
bit would become something like

if (/~/) {
s/~([^%\^]+).*/$1/;
print;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top