Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question 3

Status
Not open for further replies.

PhoenixDown

Programmer
Joined
Jul 2, 2001
Messages
279
Location
CA
I'm making a variable database file (I was going to use hash format) which will store data like:

somthing|somthing|somthing|somthing

How do I take the content between the pipes? :-) - Go there!
 
#reading data file
$filename = "your_data_file.dat";
open(DATA,&quot;<$filename&quot;);
@line = <DATA>;
close(DATA);
foreach $line (@line) {
$line =~ s/\n//g;
($var1,$var2,$var3,$var4) = split(&quot;|&quot;,$line);
# doing your action here
}

Please try,
Enjoy!
htruong
 
htruong's code looks fine - what doesn't work with that code? I would change 2 things:

1. if you're going to use $filename in the open, then $filename should be an absolute path to the file, like:

$filename = &quot;/path/to/your_data_file.dat&quot;;

2. in the foreach, instead of using a substitute to remove the newline, just use chomp, like

chomp($line);

the substitute should work fine, but that's exactly what chomp is for.

So again Cloud2, what is it that doesn't work - or I should say, what happens when you try to run it?
Hardy Merrill
Mission Critical Linux, Inc.
 
Try;
Code:
#reading data file
$filename = &quot;your_data_file.dat&quot;;
open(DATA,&quot;<$filename&quot;) || die &error($!);
 @line = <DATA>;
close(DATA);
foreach $line (@line) {
  chomp($line);
  ($var1,$var2,$var3,$var4) = split(&quot;|&quot;,$line);
  # doing your action here
}

sub error
{
my ($error) = shift;
print &quot;Content-type: text/html \n\n&quot;;
print &quot;An error occured opening the file. It was: $error&quot;;
}


That should do the job ;)

Andy
 
Try this

($var1,$var2,$var3,$var4) = split(&quot;\|&quot;,$line);

or does that only matter when you're splitting on a regular expression? Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Split requires a regular expression. I suspect that, regardless of which delimiter you choose, it's still treated as a regular expression, similar to the way you can use m&quot;...&quot; for a match regex.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
if that's the case you definitely need to escape the | then - it means &quot;or&quot; in a regular expression Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Full coding of your_file.cgi

#!/usr/bin/perl

print &quot;Content-type:text/html\n\n&quot;;

$filename = &quot;your_data_file.dat&quot;;
open(DATA,&quot;<$filename&quot;) | die &quot;can not open file&quot;;
@line = <DATA>;
close(DATA);
foreach $line (@line) {
$line =~ s/\n//g;
($var1,$var2,$var3,$var4) = split(&quot;\|&quot;,$line);
print &quot;Col1: $var1<br>\n&quot;;
print &quot;Col2: $var2<br>\n&quot;;
print &quot;Col3: $var3<br>\n&quot;;
print &quot;Col4: $var4<br>\n&quot;;
}
exit;

Again!... enjoy.
htruong



 
It still didn't work. I edited yours a bit and here's what it looks like:

sub get_entries {

open(DATA,&quot;<$vars_gen{'CGIPath'}/variables/vars_entries.cgi&quot;) or die (&quot;Unable to open guestbook entry file.&quot;);
@line = <DATA>;
close(DATA);
foreach $line (@line) {
$line =~ s/\n//g;
($var1,$var2,$var3,$var4,$var5,$var6,$var7,$var8,$var9,$var10,$var11,$var12,$var13,$var14) = split(&quot;\|&quot;,$line);
print &quot;Col1: $var1<br>\n&quot;;
print &quot;Col2: $var2<br>\n&quot;;
print &quot;Col3: $var3<br>\n&quot;;
print &quot;Col4: $var4<br>\n&quot;;
print &quot;Col5: $var5<br>\n&quot;;
print &quot;Col6: $var6<br>\n&quot;;
print &quot;Col7: $var7<br>\n&quot;;
print &quot;Col8: $var8<br>\n&quot;;
print &quot;Col9: $var9<br>\n&quot;;
print &quot;Col10: $var10<br>\n&quot;;
print &quot;Col11: $var11<br>\n&quot;;
print &quot;Col12: $var12<br>\n&quot;;
print &quot;Col13: $var13<br>\n&quot;;
print &quot;Col14: $var14<br>\n&quot;;
}
exit;

}

I use $get_entries = &get_entries; to call that subroutine because to print HTML, I use:

print<<THIS;
HTML
THIS


When I go to the main file it does somthing wrong and displays it incorrectly. It looks somthing like:


Col1: H
Col2: e
Col3: l
Col4: l
Col5: o
Col6: |
Col7: S
Col8: i
Col9: x
Col10: |


What is wrong with it? :-(
[/b] - Go there!
 
Oh! ... just replace this line:
($var1,$var2,$var3,$var4) = split(&quot;\|&quot;,$line);
to:
($var1,$var2,$var3,$var4) = split(/\|/,$line);

Good luck!
htruong
 
Thanks. That worked!!

I have another question. When I go back to the guestbook main page the variable content doesn't go in the right spot. My sub routine looks like:

sub intro {

$Copyright = &Copyright;
$get_entries = &get_entries;

print<<THIS;

<HTML>
<HEAD>
<TITLE>Guestbook</TITLE>
$vars_gen{'Header'}
</HEAD>
<BODY BGCOLOR=&quot;$vars_styles{'BGColor'}&quot; LINK=&quot;$vars_styles{'LinkColor'}&quot; ALINK=&quot;$vars_styles{'ActiveLinkColor'}&quot; VLINK=&quot;$vars_styles{'VisitedLinkColor'}&quot; TEXT=&quot;$vars_styles{'TextColor'}&quot;>
<BR>
<FONT FACE=&quot;$vars_styles{'FontFace'}&quot; SIZE=&quot;$vars_styles{'TextSize'}&quot;><A HREF=&quot;&quot;>$vars_gen{'IntroText'}</A></FONT><BR>
<BR>
<CENTER>
$get_entries
<BR>
$Copyright
</CENTER>
</BODY>
</HTML>
</HTML>

THIS

}

Instead of printing the database under the $vars_gen{'IntroText'}</, it prints it above it. How do I fix that? - Go there!
 
Nevermind.

I just replaced:

<BR>
<CENTER>
$get_entries
<BR>
$Copyright
</CENTER>


With:

<CENTER>
THIS
&get_entries;
print<<THIS;
<BR>
$Copyright
</CENTER>


And I removed $get_entries = &get_entries;.

However I do have another question. How do I make my script check if there's anything between the pipes using if statements?

It's going to be like:

<TABLE BORDER=&quot;0&quot; WIDTH=&quot;$vars_gen{'TableWidth'}&quot;>

<TR><TD>$var1</TD></TR>

</TABLE>


It would look really funny on the tables if someone didn't enter somthing in a field, lol

Thank you! :-D - Go there!
 
Check your programing skill!
if (...) {
.....
} else {
.....
}
 
Once you've parsed the line into your variables you can just say:
Code:
if ( $var1 ) {
   ...
} else {
   ...
}
If there's nothing between two pipe symbols the variable will be null, which perl interprets as false. If there is something, the variable won't be null, which perl interprets as true. Note that this WON'T work if you expect one of the values to be zero, because perl will interpret that as false even though it is not null. In that case, you could use if (length($var1)) instead.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Not quite, you don't need eq AND !~. Also, unless you only care about the first character, you need a repetition. Here's what you meant:
Code:
if ( $var1 !~ /[a-zA-Z0-9]+/) {
   $Field1 = &quot;<TR><TD>$var1</TD></TR>&quot;;
} else {
   $Field1 = &quot;&quot;;
}
(The + means 1 or more.)

BTW, you really SHOULD indent your code! :-)
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top