?
Solved

String manipulation

Posted on 2005-02-24
9
Medium Priority
?
430 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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

764 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