Welcome to WebmasterWorld Guest from 54.167.157.247

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Help needed - Search then print if String exists

Print if a string from a text file exists

   
3:20 pm on Jan 4, 2012 (gmt 0)



Hello,

I've searched far and wide for an answer to a question that I have, but did not find anything that could help me achieve what I would like to do.

I have a delimited text file (CSV) with fields that resemble these below that repeats/resemble 18 times (I only included 3 rows for an example).

It's a regularly updated database where Row-1, 2 or 3 etc, stand to change from "open" to "closed". The (img src) sequences are what is in the database (partially).

<img src='/images/closed.gif'>;Row-1;<img src='/images/open.gif' align='absmiddle'>;<img src='/images/closed.gif' align='absmiddle'>
<img src='/images/closed.gif'>;Row-2;<img src='/images/closed.gif' align='absmiddle'>;<img src='/images/closed.gif' align='absmiddle'>
<img src='/images/closed.gif'>;Row-3;<img src='/images/open.gif' align='absmiddle'>;<img src='/images/closed.gif' align='absmiddle'>

With the (working) code I have below, I would like the script to "print" the output, but only if the string "open.gif" exists on any given line, and NOT print(ing) if it is not.

It would be appreciated, thank you. Here is my present code:

#!/usr/bin/perl


use CGI::Carp qw(fatalsToBrowser);
use strict;

my $csvfile = "/my_file.txt";

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

open (CSV, "< $csvfile");
while( <CSV> )
{
my @cols = split(/;/, $_);


if (2..18) {print "Name: $cols[1]<br>$cols[0]Extra data: $cols[3]"

}

}
close CSV
4:58 pm on Jan 4, 2012 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Add this

if ($_ =~ /open\.gif/) {
print "\n$cols[1] is open\n";
}

if (2..18) {
print "Name: $cols[1]<br>$cols[0]Extra data: $cols[3]\n";
}

Note it's outside your other conditional, you can nest it inside if you want it inside 2..18.
5:57 pm on Jan 4, 2012 (gmt 0)



Thank you so much "Rocknbil", with a bit of tweaking, it works beautifully, and just the way I want it work! Cheers!
6:06 pm on Jan 4, 2012 (gmt 0)



I do have another question, similar to your last reply Rocknbil.

How can I implement your code, with the other code I have below?

What the code below does, is that it takes the info that's in the (CSV) database and prints out data from each row, 17 rows in total.

Yet, I would like to know how I can only show the rows in an html table, only if the "open.gif" file exists in the file.

Here is my other code:

#!/usr/bin/perl

use CGI::Carp qw(fatalsToBrowser);
use strict;

my $csvfile = "/my_file.txt";

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

my $max = 0;
my $row = 0;
my @td;
if (open (CSV, "< $csvfile")) {
while (<CSV>){
my @cols = split(/;/);
if ($#cols > $max) {
$max = $#cols;
}
$td[$row++] = "$_";
}
close CSV;
}



print "<table border=1 bordercolor=#CCCCCC cellspacing=0 cellpadding=5 bgcolor=#FFFFFF width=100%>\n";
my $i;
my $j;
my $colline;
foreach (@td){
chomp;
my @cols = split(/;/);

print "<tr>\n";
for ($colline=0;$colline<=$max;$colline++){
if ($cols[$colline] eq '') {$cols[$colline] = "&nbsp;"}
print "<td width=20% nowrap><p align=center><SPAN STYLE='color: #000000'><SPAN STYLE='font-size: 11pt'>$cols[$colline]</SPAN></SPAN></td>\n";

}
print "</tr>\n\n";
}
print "</table>\n";
5:18 pm on Jan 5, 2012 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



The short answer is to wrap the output in the regex if, but it looks like you're doing more work than necessary there. :-)


#!/usr/bin/perl
use strict;
my ($csvfile,@cols,$printrows,$i);
$csvfile = "myfile.txt";
$printrows=''; # so concatenation doesn't kick warnings
print "Content-Type: text/html\n\n";
print qq|
<html>
<head>
<style type="text/css">
td { color: #000; font-size:11px; }
.cols { width: 20%; }
</head>
<body>
|;
#
if (-f $csvfile) {
open (CSV, "< $csvfile") or die("cannot open file $csvfile");
while( <CSV> ) {
if ($_ =~ /open\.gif/) {
@cols = split(/;/, $_);
$printrows .= '<tr>';
if ($_ eq '') { $printrows .= "<td colspan=\"4\">&nbsp;</td>\n"; }
else {
for $i (0..3) {
$printrows.= "<td class=\"cols\">$cols[$i]</td>\n";
}
}
$printrows .= "</tr>\n";
}
}
close CSV;
if ($printrows eq '') {
print "<p>No records found</p>";
}
else {
print qq|
<table border="1" bordercolor="#CCCCCC" cellspacing="0" cellpadding="5" bgcolor="#FFFFFF" width="100%">
$printrows
</table>
|;
}
}
else { print "<p>$csvfile is not a file.</p>"; }
print '</body></html>';
4:45 pm on Jan 6, 2012 (gmt 0)



Thanks (Rocknbil), I will give this a try. All the best in the New Year, cheers!
 

Featured Threads

Hot Threads This Week

Hot Threads This Month