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!
 
I have another problem. :-(

Here is what my sub routine 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,$var15) = split(/\|/,$line);

if ( $var1 !~ /[a-zA-Z0-9]+/) {
$Field1 = &quot;<tr bgcolor=\&quot;$vars_styles{'AltColor1'}\&quot;><td width=35% valign=top><FONT SIZE=\&quot;2\&quot; FACE=\&quot;$vars_styles{'FontFace'}\&quot;><B>$var1</b></font><BR><BR></font></td>&quot;;
} else {
$Field1 = &quot;&quot;;
}

print<<THIS;
<table border=0 cellpadding=0 cellspacing=0 width=&quot;$vars_styles{'TableWidth'}&quot;><TR><td bgcolor=&quot;$vars_gen{'TableBorderColor'}&quot;>
<table border=0 cellpadding=4 border=0 cellspacing=1 width=100%>
<TR bgcolor=&quot;$vars_styles{'TableHeaderStripBackgroundColor'}&quot;><TD align=center colspan=2>
<FONT SIZE=&quot;$vars_styles{'TextSize'}&quot; FACE=&quot;$vars_styles{'FontFace'}&quot; COLOR=&quot;$vars_styles{'TableHeaderStripTextColor'}&quot;><B>Guestbook Entries</B></FONT>
</td></tr>
$Field1
</table>
THIS

}
exit;

}

When I go to the gb.cgi file, the web page looks like:

gb.gif


If that image doesn't work, go to:
Please tell me what's wrong.

Also here is the sub where I call the entries file in case someone wants to see it:

sub intro {

$Copyright = &Copyright;

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;<BR>
<CENTER>
THIS
&get_entries;
print<<THIS;
<BR>
$Copyright
</CENTER>
</BODY>
</HTML>
</HTML>

THIS

}

Thank you. :-) - Go there!
 
You've got the &quot;Guestbook Entries&quot; table information printing inside your foreach loop, so obviously you have three entires in your guestbook??? So it's printing that table for every entry in your guestbook. Try moving it out of the foreach loop.
Steve Kiehl
webmaster@nanovox.com
 
There are at least two remaining problems.
One - your HTML table structure is wacked. When you output $Field, it has no table
row or table definition around it.
<tr><td>$Field</td></tr>

Two - you are calling [red]exit;[/red] at the end of this sub. This exits the entire
program! If you want to explicitly return from your sub, then use 'return',
not 'exit'.

Additionally, using 'die' when you fail to open a file, hides the failure from you in a
CGI context. I use a very short sub, to print the error to the browser and then die.

open(IPF,&quot;<some_file&quot;) or ooops(&quot;Failed to open input file, $!&quot;);
sub oooops { print &quot;<p>@_</p>&quot;; die; }

That way, the error may not showup on the page but it will be viewable in the page source.


print<<THIS;
<table border=0 cellpadding=0 cellspacing=0 width=&quot;$vars_styles{'TableWidth'}&quot;>
<TR>
<td bgcolor=&quot;$vars_gen{'TableBorderColor'}&quot;>
<table border=0 cellpadding=4 border=0 cellspacing=1 width=100%>
<TR bgcolor=&quot;$vars_styles{'TableHeaderStripBackgroundColor'}&quot;>
<TD align=center colspan=2>
<FONT SIZE=&quot;$vars_styles{'TextSize'}&quot; FACE=&quot;$vars_styles{'FontFace'}&quot;
COLOR=&quot;$vars_styles{'TableHeaderStripTextColor'}&quot;><B>Guestbook Entries</B></FONT>
</td>
</tr>
[red]$Field1[/red]
</table>
THIS
}
[red]exit;[/red]



HTH


keep the rudder amid ship and beware the odd typo
 
It still doesn't work, and I have no idea what I'm doing wrong... :-(

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,$var15) = split(/\|/,$line);

if ( $var1 !~ /[a-zA-Z0-9]+/) {
$Field1 = &quot;<tr bgcolor=\&quot;$vars_styles{'AltColor1'}\&quot;><td width=35% valign=top><FONT SIZE=\&quot;2\&quot; FACE=\&quot;$vars_styles{'FontFace'}\&quot;><B>$var1</b></font><BR><BR></font></td>&quot;;
} else {
$Field1 = &quot;&quot;;
}

print<<THIS;
<table border=0 cellpadding=0 cellspacing=0 width=&quot;$vars_styles{'TableWidth'}&quot;>
<TR>
<td bgcolor=&quot;$vars_gen{'TableBorderColor'}&quot;>
<table border=0 cellpadding=4 border=0 cellspacing=1 width=100%>
<TR bgcolor=&quot;$vars_styles{'TableHeaderStripBackgroundColor'}&quot;>
<TD align=center colspan=2>
<FONT SIZE=&quot;$vars_styles{'TextSize'}&quot; FACE=&quot;$vars_styles{'FontFace'}&quot;
COLOR=&quot;$vars_styles{'TableHeaderStripTextColor'}&quot;><B>Guestbook Entries</B></FONT>
</td>
</tr>
$Field1
</table>
THIS
}
exit;

}

My variable file looks like:

&quot;

Calvin|<A HREF=&quot;mailto:calvin@puremadnezz.com&quot;>calvin@puremadnezz.com</A>|<A HREF=&quot;
Yes, there is a space there.

I'm sorry for this long topic. - Go there!
 
Please be more descriptive with the error. What symptoms are you having? Do you get anything or nothing or something but the format is wrong or the content is wrong?

If the code above is still what you are trying to run, then you are STILL printing $Field inside a table without a table row or table definition and you are still calling exit at the bottom of your subroutine. The EXIT will EXIT before the rest of your HTML is sent to the browser and would make the page incomplete. Use return there or nothing. You can just let the sub routine finish and it will automatically go back to the next line after the line that called the sub.

</td>
</tr>
<tr><td> [red]$Field1[/red]</td></tr>
</table>
THIS
}
[red]exit;[/red] # loose this completely.

HTH


keep the rudder amid ship and beware the odd typo
 
It's not printing $var1. I removed exit; and it's still not working.

...

<B>Guestbook Entries</B></FONT>
</td>
</tr>
<tr><td>$Field1</td></tr>
</table></td></tr></table>
THIS
}

... - Go there!
 
Sorry to leave you hanging.......
Is this logic backwards?
if ( $var1
[red]!~[/red] /[a-zA-Z0-9]+/)
{
$Field1 = &quot;<tr bgcolor=\&quot;$vars_styles{'AltColor1'}\&quot;>
<td width=35% valign=top><FONT SIZE=\&quot;2\&quot; FACE=\&quot;$vars_styles{'FontFace'}\&quot;>
<B>$var1</b></font><BR><BR></font></td>&quot;;
}
else
{
$Field1 = &quot;&quot;;
}


It appears that you are checking to see if $var1 does NOT contain letters or numbers. If it does not ( it may be null), then, build the table row stuff with $var1 containing nothing. Therefore, $Field has HTML tags but no content. ELSE, $var1 does contain alpha or numerics, so set it to null. ?????? huh?

maybe change the '!~' to '=~'


keep the rudder amid ship and beware the odd typo
 
Thanks! That worked! It works good now. I made a few modifications to some subroutines and it works perfectly:

sub intro {

$Copyright = &Copyright;

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;<BR>
<CENTER>
<table border=0 cellpadding=0 cellspacing=0 width=&quot;$vars_styles{'TableWidth'}&quot;>
<TR>
<td bgcolor=&quot;$vars_styles{'TableBorderColor'}&quot;>
<table border=0 cellpadding=4 border=0 cellspacing=1 width=100%>
<TR bgcolor=&quot;$vars_styles{'TableHeaderStripBackgroundColor'}&quot;>
<TD align=center colspan=2>
<FONT SIZE=&quot;$vars_styles{'TextSize'}&quot; FACE=&quot;$vars_styles{'FontFace'}&quot;
COLOR=&quot;$vars_styles{'TableHeaderStripTextColor'}&quot;><B>Guestbook Entries</B></FONT>
</td>
</tr>
THIS
&get_entries;
print<<THIS;
</table></td></tr></table>
<BR><BR>
$Copyright
</CENTER>
</BODY>
</HTML>
</HTML>

THIS

}

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,$var15) = split(/\|/,$line);

if ( $var1 =~ /[a-zA-Z0-9]+/) {
$Field1 = &quot;<tr bgcolor=\&quot;$vars_styles{'AltColor1'}\&quot;>
<td width=35% valign=top><FONT SIZE=\&quot;2\&quot; FACE=\&quot;$vars_styles{'FontFace'}\&quot;>
<B>$var1</b></font><BR><BR></font></td>&quot;;
} else {
$Field1 = &quot;&quot;;
}

print<<THIS;
$Field1
THIS
}

}

I just wanted to know one more thing. For if ( $var1 =~ /[a-zA-Z0-9]+/) { is there a way to do that so it will check if any characters are there?

Thanks again!! :-D - Go there!
 
What do you mean by any characters? If you just mean &quot;is it non-null&quot;, this will work if ($var1 ne &quot;&quot;). If you mean something like &quot;any non-whitespace character&quot; try this if ($var1 !~ /\A\w*\Z/). Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
if ($var1 =~ /\w/) { print &quot;Found a word char&quot;; }

or

if (length($var1) > 0) { print &quot;var1 had something in it.&quot;; }

or...... TIMTOWTDI ;-)


keep the rudder amid ship and beware the odd typo
 
OOPS! You're right! \w is a &quot;word char&quot; not a &quot;whitespace char&quot;! I always do that! :~/ The second example should have been if ($var1 !~ /\A\s*\Z/). It means &quot;$var1 is not ALL whitespace (or null)&quot;. It will allow some spaces, tabs, etc., but it must have at least one non-whitespace character.

Generally I'd rather use $var ne &quot;&quot; than length($var) > 0. I think it's probably more efficient, since it doesn't do the function call. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
ne means &quot;not equal to&quot;, in a string comparison. It's the opposite of eq. 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