Gossamer Forum
Home : General : Perl Programming :

Read Text Files

Quote Reply
Read Text Files
Hi,

I haven't been working with CGI/Perl for a couple of months and now I'm back creating this online store for a friend.

OK. Everything is working fine so far, except one thing which I cannot remember how to do. I hope you can help.

I keep all records of the products available in flat ASCII files with different values in the files. All different fields are seperated by a denominator (in my case \n%%\n). This can be read by my script and split into 7 fields.

One field however is a few lines long. Each line represent a bullet-point of features realting to that product. What I want to know is how do I read that into the script and then tell the script foreach line within that field make a bullet point...

I think I remember how to do this, but I'm not quite sure of the syntax. It's probably something like read the field into a @ and then split the @ foreach \n.

Anyway, please can anyone help me out here? Thanks!

Regards,
Wiliam Stephens
Quote Reply
Re: Read Text Files In reply to
If you are using \n%%\n as a delimeter, then your file is probably formated like this:
Code:
something
%%
something else
%%
more somethings

I wouldn't recommend this but if you must:
Code:
open(FILE,"/path/to/file") | | die "Can't open file";
@lines = (<FILE> );
close(FILE);

$line_count=0;
foreach $line(@lines) {
$line_count++;
if ($line_count > 12) {
push(@bullets,$line);
}
}
print qq|<UL type="circle" compact>\n|;
foreach $bullet(@bullets) {
print qq|<LI>$bullet</LI>\n|;
}
print "</UL>";

------------------
WFMY-TV Webmaster

[This message has been edited by Chris071371 (edited December 22, 1999).]
Quote Reply
Re: Read Text Files In reply to
Hi,

Thanks for your efforts.

However, after tweaking your code a little, I can't seem to get it to display anything at all. Is your code suggesting that only if the number of lines within a field exceds 12 then it should set them out as bullet points?

Why don't you recommend the format of my data files? What other format is best used?

This is the snippet of modified code:

Code:
$line_count=0;
foreach $product_long(@lines) {
$line_count++;
if ($line_count > 12) {
push(@bullets,$product_long);
}
}
print qq|<UL type="circle" compact>\n|;
foreach $bullet(@bullets) {
print qq|<LI>$bullet</LI>\n|;
}
print "</UL>";

So, hmm. As you can see, the variable $product_long is the one that stores the data field from the text file field where I want to format into bullets.

Regards,
Wiliam Stephens
Quote Reply
Re: Read Text Files In reply to
Am I correct about the layout of your DB? Is it like this?

Code:
field1
%%
field2
%%
field3
%%
field4
%%
field5
%%
field6
%%
field7
is
multiple
lines
Am I correct in assuming that? Also, I assumed that the multiline field was the last one. If so, I forgot to chomp. Add this line after
Code:
foreach $line(@lines) {
chomp ($line);

If your db is different than I am assuming, please post a snippet so I can be more clear on what I am dealing with.

------------------
WFMY-TV Webmaster
Quote Reply
Re: Read Text Files In reply to
Code:
GE Hotpoint Extra-Large Capacity Washer
%%
623114
%%
299
%%
Washing Machines
%%
HotPoint
%%
A marvellous little washing machine.
%%
2.7 Cu. Ft. Extra-Large Capacity
1 Wash/Spin Speed Combinations
3 Wash/Rinse Temperature Settings
3 Water Levels
7 Cycles: Heavy Soil, Medium Soil, Light
Soil, Auto Soak , Delicates, Permanent
Press and Knits
Rear Lid Opening
TufTub Basket

As you can see from above, you are right in assuming that the last field is the multi-line one, although other fields have the potential to be multi-lined, I do not want them to be bullet-pointed.

Thanks for your help.

Regards,
Wiliam Stephens
Quote Reply
Re: Read Text Files In reply to
open(FILE,"/path/to/file") &#0124; &#0124; die "Can't open file";
@lines = (<FILE> );
close(FILE);
$line_count=0;
foreach $line(@lines) {
chomp($line);
if ($line =~ /^\%\%/) {
$line_count++;
}
if (($linecount == 6) && ($line !~ /^\%\%/)) {
push(@bullets,$line);
}
}
print qq|<UL type="circle" compact>\n|;
foreach $bullet(@bullets) {
print qq|<LI>$bullet</LI>\n|;
}
print "</UL>";

------------------
WFMY-TV Webmaster
Quote Reply
Re: Read Text Files In reply to
Personally, I think the method you're doing in the flat file is a little cumbersome. What I would do is, make each item a pipe deliminated line, with everything on that one line. Then deliminate the features with %%.

Everything would then be on one line, easliy placed into fields, and easier to work with.

Example:

Code:
my $line = qq~GE Hotpoint Extra-Large Capacity Washer |623114|299|Washing Machines|HotPoint|A marvellous little washing machine.|2.7 Cu. Ft. Extra-Large Capacity %%1 Wash/Spin Speed Combinations %%3 Wash/Rinse Temperature Settings %%3 Water Levels %%7 Cycles: Heavy Soil, Medium Soil, Light%%Soil, Auto Soak , Delicates, Permanent%%Press and Knits %%Rear Lid Opening %%TufTub Basket ~;

my ($name, $model, $price, $type, $manufacturer, $description, $features) = split /\|/, $line;

my @features = split /%%/, $features;

print "<ul>\n";
foreach $line(@features) {
print "<li>$line\n";
}
print "</ur>\n";

Just my two cents Smile

--mark
Quote Reply
Re: Read Text Files In reply to
Thanks Chris and Mark. I will try both your suggestions now, although I think I'm going to incorporate features from both responses into my final version :-)

Regards,
Wiliam Stephens
Quote Reply
Re: Read Text Files In reply to
Right, I've put in your new code Chris and it doesn't display anything. The code actually works because the HTML tags UL appear but it doesn't disaply any data from the text file. Hmm.

Wiliam
Quote Reply
Re: Read Text Files In reply to
Hi Andy & Chris,

Finally, after tweaking around with all suggestions, I got it to work! Thanks a lot for your help. I really appreciate it. For the record, this is how I managed it:

Code:
open (DATA,"$DATA_DIR/$code.inf");
@indata = <DATA>;
close (DATA);

$fields = join('',@indata);

@fields = split(/$delimiter/,$fields);

$product_title = $fields[0];
$product_code = $fields[1];
$product_price = $fields[2];
$product_cat = $fields[3];
$product_manufac = $fields[4];
$product_info = $fields[5];
$product_features = $fields[6];

my @features = split /\n/, $product_features;

print qq|<UL>\n|;
foreach $fields(@features) {
print qq|<LI TYPE="square">$fields\n|;
}
print qq|</UL>\n|;

And it works like a charm. Thanks a maillion for alln your efforts!

Regards,
Wiliam Stephens