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: Beginner:
removing parts of a string

 



sirxris
stranger

Jun 22, 2001, 9:05 AM

Post #1 of 7 (1084 views)
removing parts of a string Can't Post

Okay, if i have a string like this (it's really an array)

$image = "THIS_img.jpg";

how could i get it to remove the "_img.jpg" from the varaible? Would it use chop, split or what? Thanx for helping me, the forum is great for newbies!



mhx
Enthusiast / Moderator

Jun 22, 2001, 10:04 AM

Post #2 of 7 (1032 views)
Re: removing parts of a string [In reply to] Can't Post

Hi,

use regular expression search and replace:

Code
$image = "THIS_img.jpg"; 
$image =~ s/_img\.jpg$//; # replaces _img.jpg with nothing

Hope this helps.

-- Marcus



sirxris
stranger

Jun 22, 2001, 11:32 AM

Post #3 of 7 (1026 views)
Re: removing parts of a string [In reply to] Can't Post

Sorry, it didn't work.. :( I explained very un clear:
Im using this code:

my $dir = '/usr/home/web/d/dlmugen/chars/';

opendir DIR, $dir or die "Cannot open directory `$dir': $!\n";
my @images = grep /_icn\.jpg$/, readdir DIR;
closedir DIR;

my $webimg = 'http://balder.prohosting.com/~dlmugen/chars/';
my $bord = '1px solid rgb(166,166,166)';
my $bordhov = '1px solid rgb(0,170,222)';


print qq(<HTML><HEAD></HEAD><BODY>);
print qq(<IMG SRC="$webimg$_" border="0" style="border: $bord" name="$_"><A HREF="$webimg$_" target="_blank" onMouseOver="$_\.style\.border=\'$bordhov\'" onMouseOut="$_\.style\.border=\'$bord\'"> $_</A>
) foreach sort @images;
print qq(</BODY></HTML>);

and im looking to use another seperate variable for $_ that takes away the _icn.jpg so i can use it to show the link, and in the onMouseOver / onMouseOut. Thanx a million times, sorry for the problems :(



mhx
Enthusiast / Moderator

Jun 22, 2001, 5:39 PM

Post #4 of 7 (926 views)
Re: removing parts of a string [In reply to] Can't Post

Hi,

first, if you have a look at your other post in the intermediate forum, Jasmine has a solution that's a lot better than mine. I've taken her solution and modified it so it should fit your needs:

Code
#!/usr/bin/perl -wT 
use CGI qw /:standard/;
use CGI::Carp qw /fatalsToBrowser/;
use strict;

my $page_title = 'Image List';

my $required_beginning = '';
my $required_ending = '_icn.jpg';
my $html = 'http://balder.prohosting.com/~dlmugen/chars';
my $path = '/usr/home/web/d/dlmugen/chars';

my $bord = '1px solid rgb(166,166,166)';
my $bordhov = '1px solid rgb(0,170,222)';

my @linked_images =
map {
/^\/?(.*)$required_ending$/;

# image
td(
img( { -src => "$html$_",
-border => 0,
-style => "border:$bord",
-name => "img$1",
}
),

# url
a( { -href => 'http://url-to-go-to.com',
-target => '_blank',
-onMouseOver => "document.img$1.style.border='$bordhov'",
-onMouseOut => "document.img$1.style.border='$bord'",
},
$1
)
)
}
map { /^(?:$path)?(.*)$/ }
grep { -B } glob "$path/$required_beginning*$required_ending";

print header, start_html( $page_title ),
table( map { Tr( $_ ), "\n" } @linked_images ),
end_html;

I hope this works for you. If you need further explanation than is given in Jasmine's post, feel free to ask.

-- Marcus



sirxris
stranger

Jun 22, 2001, 5:54 PM

Post #5 of 7 (924 views)
Re: removing parts of a string [In reply to] Can't Post

I am using prohosting's free service until I can get enough support to host on a dedicated server, anyway, i don't think i can use the mod thingys



mhx
Enthusiast / Moderator

Jun 22, 2001, 6:01 PM

Post #6 of 7 (923 views)
Re: removing parts of a string [In reply to] Can't Post

Hi,


In Reply To
anyway, i don't think i can use the mod thingys

You mean the CGI:: modules? I strongly hope they're installed at prohosting. AFAIK they're part of standard Perl. If they really shouldn't be installed, I recommend to change to a free service who supports the CGI:: modules. At netfirms you get a 25MB account with cgi and sendmail support and most important perl modules installed. They do have the CGI:: modules.

-- Marcus



sirxris
stranger

Jun 23, 2001, 10:21 AM

Post #7 of 7 (913 views)
Re: removing parts of a string [In reply to] Can't Post

Thanx for the help, i got it all working smooth. Free.Prohosting does allow alot of modules, as for netfirms :( they don't allow you to execute cgi's, they only give you 25mb, banners are always on the top, SSI restrictions, just not a cool server to use... If you wanted to see the script you guys made check it out

http://balder.prohosting.com/~dlmugen/chars/index.shtml


 
 


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

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