Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

String manipulation

Posted on 2005-02-24
9
Medium Priority
?
433 Views
Last Modified: 2010-03-05
The script at the end of this note gets customer codes from a mysql database and displays them in a webpage. Each customer code has a consistent naming convention and I could use it to assess their internal webpage link and add it to the webpage. Can someone help me to get my script to make the link and add it to the page?

An example of a customer code is: Cand

I would like to modify my script to add the following link beside: www.Cand_lin.com

Any ideas?




#!/usr/bin/perl
use strict;
use CGI::Carp qw(fatalsToBrowser);
use CGI;
use DBI;
 
my $q = new CGI;
print $q->header;
 
my $dbh = DBI->connect("DBI:mysql:NAVUSERS;localhost",
"foms");
 
my $sth = $dbh->prepare("SELECT hostname FROM HomeHost where
UID = 0 order by hostname");
 
$sth->execute();
 
print "\tUser
Names:\n=====================================\n";
 
 
while ( my @row = $sth->fetchrow_array()) {
        print "@row\n";
}
 
exit;
 
$sth->finish;
$dbh->disconnect;
0
Comment
Question by:heavenlydishes
  • 4
  • 3
  • 2
9 Comments
 
LVL 16

Expert Comment

by:manav_mathur
ID: 13394041
What does your data_structure look like?? Is there only one field in the database??

Manav
0
 
LVL 16

Expert Comment

by:manav_mathur
ID: 13394065
print $q->a(href => "www.${variable)-append_something.com","some text here") ;

will create a link pointing to "www.${variable)-append_something.com" and the text of the link wil be 'some text here'.

Manav
0
 

Author Comment

by:heavenlydishes
ID: 13394863
There is just one field being read by the script. That field is hostname, which is the customer code.

I made a bit of a mistake in describing the situation. I need only the 2nd and 3rd characters of the customer code to construct my web link.

For the customer code example I previously mentioned, Cand, I need to capture the 2nd and 3rd letters in a variable and create the following link:

www.an.com

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:heavenlydishes
ID: 13395213
Hey, you got me most of the way there! Its working but the page is displaying href instead of indicating a link. IAny ideas?
0
 
LVL 1

Expert Comment

by:timdr
ID: 13395245
Here is the code you should use in the while loop do what you want:

while ( my @row = $sth->fetchrow_array()) {
    my ($code) = $row[0] =~ /^.(..)/;
    print "www.$code.com\n";
}
0
 

Author Comment

by:heavenlydishes
ID: 13395392
I tried that. It also does not add links but dispays href before each item on the page.
0
 
LVL 1

Accepted Solution

by:
timdr earned 2000 total points
ID: 13395465
Oh, I didn't realize you wanted to make it an HTML link. Try this:

while ( my @row = $sth->fetchrow_array()) {
    my ($code) = $row[0] =~ /^.(..)/;
    my $domain = "www.$code.com";
    print "<a href='http://$domain/'>$domain</a>\n";
}
0
 

Author Comment

by:heavenlydishes
ID: 13395667
That's perfect! Thank you very much!
0
 
LVL 16

Expert Comment

by:manav_mathur
ID: 13401133
As long as you are using CGI, why not use its funcitons instead??

print $q->a({href=>"http://$domain/"},"$domain") ;

Manav
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question