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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How To Parse Data

Status
Not open for further replies.

mtorbin

Technical User
Nov 5, 2002
369
US
Hey all,

So I have this entry in my text database that looks something like this:

0001|mtorbin|password|Tue May 18 15:33:27 2004|in

How do I separate that info into differnt pieces in an array?

Thanks,

- MT

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
Below is a very simple method of pushing each line in your text database file (flatfile) into an array ...

assume the name of your file is test.txt

open (TEXTDB,"test.txt");
while (<TEXTDB>)
{
# split data on |, send result to @array
@array=split(/\|/, $_);
# extra logic required here to make use of result
# i'm just printing eachline !
print "\n @array";
}
close (TEXTDB);


 
So let me ask you this. Here is the code that I'm currently using. The name of the file is readEntry.cgi:

#!/usr/bin/perl -wT
print "Content-type:text/html\n\n";
open(fileIN,"logSheet.dat") or dienice("Cannot open the log sheet: $!");
@logData = <fileIN>;
close(fileIN);
print <<EndHTML;
<html>\n<head>\n
<title> Log File Data </title>\n
</head>\n<body>\n
<h2>Log File Data</h2>\n
EndHTML

foreach $line (@logData) {
chomp($line);
print "$line<br>\n";
}

#print "</body>\n</html>";

# Error Trapping Sub...should things go pear shaped!
sub dienice
{
my($msg) = @_;
print "<html>\n<head>\n<title>Error Opening File!</title>\n";
print "</head>\n";
print "<body><h2>Error</h2>\n<b>";
print $msg;
print "\n</b></body>\n</html>";
exit;
}

Where would I make that change?

- MT



Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
I need to create a subrouteine for this because it's something I think I'll be using over and over. For example, the first entry in the database is a key and that needs to be incremented every time a new entry is added.

- MT

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
Ok, so I've got it at this point where it printes everything in a nice order using an array. How do I refer to just one piece of that array (for example) the 0001 part? Here is the updated code:

#!/usr/bin/perl -wT
print "Content-type:text/html\n\n";
open(fileIN,"logSheet.dat") or dienice("Cannot open the log sheet: $!");
@logData = <fileIN>;
close(fileIN);
print <<EndHTML;
<html>\n<head>\n
<title> Log File Data </title>\n
</head>\n<body>\n
<h2>Log File Data</h2>\n
EndHTML

foreach $line (@logData) {

@myList = split(/\|/, $line);
print "@myList<br>";

# chomp($line);
# print "$line<br>\n";
}

#print "</body>\n</html>";

# Error Trapping Sub...should things go pear shaped!
sub dienice
{
my($msg) = @_;
print "<html>\n<head>\n<title>Error Opening File!</title>\n";
print "</head>\n";
print "<body><h2>Error</h2>\n<b>";
print $msg;
print "\n</b></body>\n</html>";
exit;
}

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
Ok, if you wish to refer to the very first element of the array then this would be,

$myList[0]

... if you wish to refer to the last element of the array, i.e. (in your example, the last element of the line)

$myList[$#myList]

regarding an auto-incrementing then appending information to your text file, you could utilise something similar to the following subroutine...


sub add_to_db
{
foreach $line (@logData) {

@myList = split(/\|/, $line);
$last_value=$myList[0];
}

++$last_value; # generate new key

open(fileOUT,">>logSheet.dat") or dienice("Cannot open the log sheet: $!");
# subtitute next line with 'real' values ...
print fileOUT,$last_value,"|",$val1,"|",$val2," \n";
close (fileOUT);
}

hopefully this should keep you on the right track...
 
Thanks all for your help. I'm trying to attack this in the only way I know possible, which is piece by piece.

- MT

Matt Torbin
Web and Graphic Arts Engineer
PEI-Genesis

aim: dgtlby
direct email: mtorbin_at_earthlink.net
 
Code:
#!/usr/bin/perl

print "Content-type: text/html\n\n";

open(FILEIN,"logSheet.dat") or dienice("Cannot open the log sheet: $!");
@logData = <FILEIN>;
close(FILEIN);

print <<EndHTML;
<html>
<head>
<title>Log File Data</title>
</head>
<body>
<h2>Log File Data</h2>
EndHTML

foreach $line (@logData) {
  chomp($line);
  [red][b]... put it here dude ...[/b][/red]
  print "$line<br>\n";
}

#print "</body>\n</html>";

#Error Trapping Sub...should things go pear shaped!
sub dienice
{
my($msg) = @_;
print <<errorHTML;
<html>
<head>
<title>Error Opening File!</title>
</head>
<body><h2>Error</h2>
<b>$msg</b>
</body>
</html>
errorHTML
exit;
}

I've tried to clean up the script a bit also...


Kind Regards
Duncan
 
That red text nearly blinded me, no lie! :)

I really think you should pick up a Perl book, OP. We don't mind helping you but unless you know how and why we coded the way we did, you're never going to understand it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top