CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Display and Print delimited file

 



okalani
Deleted

Nov 29, 2000, 10:46 PM

Post #1 of 12 (1147 views)
Display and Print delimited file Can't Post

I have a database that has these fields that are already pipe delimited:

123|Joe|Smoe|email|address|phone

I want to be able to:

1. Change the delimiter to other delimiters I select.
2. Select/rearrange the order of the fields.
3. Display the the datbase so I can save it to my local computer.

Can someone show me the best way to approach this? I was able to set the choosen delimiter but was unable to print out the database fields in the order that I wanted.

Any assistance will be greatly appreciated. Thanks

Ikaika


sleuth
Enthusiast / Moderator

Nov 30, 2000, 12:27 PM

Post #2 of 12 (1147 views)
Re: Display and Print delimited file [In reply to] Can't Post

 
Code to do that:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


open(data, "<data.db") &#0124; &#0124; print "$!";
while(<data> )
{
($id,$fname,$lname,$email,$address,$phone)=split(/\|/, $_);
push(@new,"$id::$fname::$lname::$phone::$email::$address\n");
}
close(data);
print "@new";
</pre><HR></BLOCKQUOTE>

Or take new and re-write it back to the origional data base.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


open(data2, ">data.db") &#0124; &#0124; print "$!";
print data2, @new;
close(data2);
</pre><HR></BLOCKQUOTE>

Sleuth


okalani
Deleted

Nov 30, 2000, 1:15 PM

Post #3 of 12 (1147 views)
Re: Display and Print delimited file [In reply to] Can't Post

Thanks for the response! I guess I should have been more specific. What I want to do is use the form below to get the information from the database in the order I select, the type of delimiter I choose by using the form. Also print out only the fields that I want printed:

This is the form that I will be using:

<FORM ACTION="myDB.cgi" METHOD="post">
<DIV ALIGN="center">
<CENTER>
<TABLE>
<TR>
<TD COLSPAN="3" ALIGN="center" VALIGN="top"><FONT FACE="Arial" SIZE="3"><B>Delimiter</B><BR>
<FONT SIZE="2">pipe ("|") <INPUT TYPE="RADIO" NAME="delimiter" VALUE="|" CHECKED><BR>
comma (",") <INPUT TYPE="RADIO" NAME="delimiter" VALUE=","><BR>
</FONT></FONT><FONT SIZE="2" FACE="Arial">Tab <INPUT TYPE="radio" NAME="delimiter" VALUE="\n"></FONT></TD>
</TR>
<TR>
<TD ALIGN="right"><B><U><FONT SIZE="3">Fields</FONT></U></B></TD>
<TD ALIGN="center"><B><U><FONT SIZE="3">Export</FONT></U></B></TD>
<TD ALIGN="center"><B><U><FONT SIZE="3">Order</FONT></U></B></TD>
</TR>
<TR>
<TD ALIGN="right"><FONT SIZE="2" FACE="VERDANA"><B>First Name</B></FONT></TD>
<TD ALIGN="center"><INPUT TYPE="radio" NAME="a_fname"></TD>
<TD ALIGN="center"><INPUT TYPE="text" SIZE="2" MAXLENGTH="2" NAME="b_fname"></TD>
</TR>
<TR>
<TD ALIGN="right"><FONT SIZE="2" FACE="VERDANA"><B>Last Name</B></FONT></TD>
<TD ALIGN="center"><INPUT TYPE="radio" NAME="a_lname"></TD>
<TD ALIGN="center"><INPUT TYPE="text" SIZE="2" MAXLENGTH="2" NAME="b_lname"></TD>
</TR>
<TR>
<TD ALIGN="right"><FONT SIZE="2" FACE="VERDANA"><B>E-mail</B></FONT></TD>
<TD ALIGN="center"><INPUT TYPE="radio" NAME="a_email"></TD>
<TD ALIGN="center"><INPUT TYPE="text" SIZE="2" MAXLENGTH="2" NAME="b_email"></TD>
</TR>
<TR>
<TD ALIGN="right"><FONT SIZE="2" FACE="VERDANA"><B>Phone</B></FONT></TD>
<TD ALIGN="center"><INPUT TYPE="radio" NAME="a_phone"></TD>
<TD ALIGN="center"><INPUT TYPE="text" SIZE="2" MAXLENGTH="2" NAME="b_phone"></TD>
</TR>
<TR>
<TD ALIGN="right"><FONT SIZE="2" FACE="VERDANA"><B>Address</B></FONT></TD>
<TD ALIGN="center"><INPUT TYPE="radio" NAME="a_address"></TD>
<TD ALIGN="center"><INPUT TYPE="text" SIZE="2" MAXLENGTH="2" NAME="b_address"></TD>
</TR>
<TR>
<TD ALIGN="center" COLSPAN="3"><FONT FACE="Arial" SIZE="3"><INPUT TYPE="SUBMIT" NAME="action" VALUE="Export"></FONT></TD>
</TR>
</TABLE>
</CENTER>

</DIV>

<CENTER>
</FORM>

Ikaika


sleuth
Enthusiast / Moderator

Nov 30, 2000, 3:07 PM

Post #4 of 12 (1147 views)
Re: Display and Print delimited file [In reply to] Can't Post

 
I see where you are going, I myself didn't know how to do what you wanted, so I figured it out for my own bennifit, and yours also, the following posts will be first, the html, then another just of the code I wote. I have tested this all. You requested a way for people to choose the out put, that I FEEL is very unpracticle, and for the average user, will be too time consuming and difficult to figure out. I'll give you the code & stuff for they way you asked, and also the slightly easier way that I think you should decide to use. I can also mail this code to you zipped if you want instead, so it's not soo messy, but for future refference of other users, I'll also post the code.

Be Right Back,

Sleuth


sleuth
Enthusiast / Moderator

Nov 30, 2000, 3:24 PM

Post #5 of 12 (1147 views)
Re: Display and Print delimited file [In reply to] Can't Post

 Ok I Screwed Up, I forgot about that ID number you have b4 each record, So I had to redo the html, then I found that I made more changes than needed, plus I delete the posts I made here because the html was wrong from before, then I figured out something that would work well, and in all the proccess I lost the origional html that you wanted me to use, I made changes to the html you posted so it would work, and I don't want to make thoses changes all over again, I'd rather you just use this html, it's better anyway, it's easier to understand.

Let me know,

Slueth

[This message has been edited by sleuth (edited 11-30-2000).]


sleuth
Enthusiast / Moderator

Nov 30, 2000, 3:25 PM

Post #6 of 12 (1147 views)
Re: Display and Print delimited file [In reply to] Can't Post

HTML:


<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
</head>

<p><FORM ACTION="http://station2/cgi-bin/myDB.cgi" METHOD="post">
<DIV ALIGN="center">
<CENTER>
<TABLE width="231">
<TR>
<TD COLSPAN="2" ALIGN="center" VALIGN="top" width="208"><FONT FACE="Arial" SIZE="3"><B>Delimiter</B><BR>
<FONT SIZE="2">pipe ("|") <INPUT TYPE="RADIO" NAME="delimiter" VALUE="|" CHECKED><BR>
comma (",") <INPUT TYPE="RADIO" NAME="delimiter" VALUE=","><BR>
</FONT></FONT><FONT SIZE="2" FACE="Arial">Tab <INPUT TYPE="radio" NAME="delimiter" VALUE=" "></FONT></TD>
</TR>
<TR>
<TD ALIGN="right" width="144">
<p align="center"><B><U><FONT SIZE="3">Field Order</FONT></U></B></p>
</TD>
<TD ALIGN="center" width="85"><B><U><FONT SIZE="3">Export</FONT></U></B></TD>
</TR>
<TR>
<TD ALIGN="right" width="144">
<p align="center"><select size="1" name="o1">
<option value="1">First Name</option>
<option value="2">Last Name</option>
<option value="3">E-mail</option>
<option value="4">Phone</option>
<option value="5">Address</option>
</select></p>
</TD>
<TD ALIGN="center" width="85"><input type="checkbox" name="e1" value="ON" checked></TD>
</TR>
<TR>
<TD ALIGN="right" width="144">
<p align="center"><select size="1" name="o2">
<option value="1">First Name</option>
<option value="2" selected>Last Name</option>
<option value="3">E-mail</option>
<option value="4">Phone</option>
<option value="5">Address</option>
</select></p>
</TD>
<TD ALIGN="center" width="85"><input type="checkbox" name="e2" value="ON" checked></TD>
</TR>
<TR>
<TD ALIGN="right" width="144">
<p align="center"><select size="1" name="o3">
<option value="1">First Name</option>
<option value="2">Last Name</option>
<option value="3" selected>E-mail</option>
<option value="4">Phone</option>
<option value="5">Address</option>
</select></p>
</TD>
<TD ALIGN="center" width="85"><input type="checkbox" name="e3" value="ON" checked></TD>
</TR>
<TR>
<TD ALIGN="right" width="144">
<p align="center"><select size="1" name="o4">
<option value="1">First Name</option>
<option value="2">Last Name</option>
<option value="3">E-mail</option>
<option value="4" selected>Phone</option>
<option value="5">Address</option>
</select></p>
</TD>
<TD ALIGN="center" width="85"><input type="checkbox" name="e4" value="ON" checked></TD>
</TR>
<TR>
<TD ALIGN="right" width="144">
<p align="center"><select size="1" name="o5">
<option value="1">First Name</option>
<option value="2">Last Name</option>
<option value="3">E-mail</option>
<option value="4">Phone</option>
<option value="5" selected>Address</option>
</select></p>
</TD>
<TD ALIGN="center" width="85"><input type="checkbox" name="e5" value="ON" checked></TD>
</TR>
<TR>
<TD ALIGN="center" COLSPAN="2" width="208"><FONT FACE="Arial" SIZE="3"><INPUT TYPE="SUBMIT" NAME="action" VALUE="Export"></FONT></TD>
</TR>
</TABLE>
</CENTER>

</DIV>

<CENTER>
</FORM>
</center>


sleuth
Enthusiast / Moderator

Nov 30, 2000, 3:27 PM

Post #7 of 12 (1147 views)
Re: Display and Print delimited file [In reply to] Can't Post

CODE:

print "Content-type: text/html\n\n";
#################################################################
sub parse_form {
my (@pairs, %in);
my ($buffer, $pair, $name, $value);

if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
}
elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
}
else {
&cgierr('You cant run this script from telnet/shell.');
}

PAIR: foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);

$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

($value eq "---") and next PAIR;
exists $in{$name} ? ($in{$name} .= "~~$value") : ($in{$name} = $value);
}
return %in;
}
%in =&parse_form;
sub dummyproof{
$o1=$in{'o1'};
$o2=$in{'o2'};
$o3=$in{'o3'};
$o4=$in{'o4'};
$o5=$in{'o5'};
$allnumbers="$o1$o2$o3$o4$o5";
if ($allnumbers !~ /1/ &#0124; &#0124; $allnumbers !~ /2/ &#0124; &#0124; $allnumbers !~ /3/ &#0124; &#0124; $allnumbers !~ /4/ &#0124; &#0124; $allnumbers !~ /5/){
print qq~Error: <br> Please Make Sure That The Order Of The Fields You Have Requested Is Unique For Each Field.~;
exit;
}
}
#################################################################
&dummyproof;
open(data, "<mydb.db");
while(<data> )
{
(@res)=split(/\|/, $_);
if ($in{'e1'}){$one = "$res[$in{'o1'}]$in{'delimiter'}";}
if ($in{'e2'}){$two = "$res[$in{'o2'}]$in{'delimiter'}";}
if ($in{'e3'}){$three = "$res[$in{'o3'}]$in{'delimiter'}";}
if ($in{'e4'}){$four = "$res[$in{'o4'}]$in{'delimiter'}";}
if ($in{'e5'}){$five = "$res[$in{'o5'}]$in{'delimiter'}";}
print "$one$two$three$four$five\n";
}
close(data);


sleuth
Enthusiast / Moderator

Nov 30, 2000, 3:28 PM

Post #8 of 12 (1147 views)
Re: Display and Print delimited file [In reply to] Can't Post

Did this on win 32 so I forgot to add

#!/usr/bin/perl at the top, don't forget about it.

Sleuth


okalani
Deleted

Nov 30, 2000, 5:19 PM

Post #9 of 12 (1147 views)
Re: Display and Print delimited file [In reply to] Can't Post

Thanks Sleuth!

I was way off base in the way I was trying to write the code.

Couple of problems that I'm running into with this code. For some reason it's not displaying the first field. If I put a pipe in front of the first field in the database, than it would print. I looked at the codes and it seem that it should print out all the fields properly.

Also, when the records are displayed, it places it all on one line, which may confuse the end user. I tryed changing the content type so it would display the data in plain text, but it does prompt you to save it (which is not a bad idea), and when I open the file, again, its all on one line.

Any suggestions?

Ikaika


okalani
Deleted

Dec 1, 2000, 12:38 AM

Post #10 of 12 (1147 views)
Re: Display and Print delimited file [In reply to] Can't Post

That's exactly what I did. I played around with it earlier and found it to be the numbering scheme, in the html form and in the code section:

if ($allnumbers !~ /0/ | | $allnumbers !~ /1/ | | $allnumbers !~ /2/ | | $allnumbers !~ /3/ | | $allnumbers !~ /4/){

All I did was start the number from 0 to 4 rather than 1 to 5 and it correct the problem.

As for the for the display of the records, I couldn't get to display the way I wanted to, so I just wrote some extra code to zip it for download.

Thanx
Ikaika


sleuth
Enthusiast / Moderator

Dec 1, 2000, 10:17 AM

Post #11 of 12 (1147 views)
Re: Display and Print delimited file [In reply to] Can't Post

 
Yea, just change this line

print "$one$two$three$four$five\n";

To what ever the heck you want, like this

print qq~
$one <br>
$two <br>
$three <br>
$four <br>
$five <br>
~;

Anything, it's flexable.

And the thing about printing the fields improperly, I went by what you showed your format to be in the ex.

123|Joe|Smoe|email|address|phone

That's what I went by, if your data base isn't formated like that, then tell me exactly what the format is. You'd have to correct this in the html, in the drop down menus, they starts with a "1" & not a "0", so if the name field is the very first one, change ALL of the Drop Down menus to begin at "0", and end a "4", get it?

Sleuth


sleuth
Enthusiast / Moderator

Dec 1, 2000, 12:26 PM

Post #12 of 12 (1147 views)
Re: Display and Print delimited file [In reply to] Can't Post

 
Good, glad you got it,

Sleuth

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives