LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-11-2011, 10:09 PM   #1
fredfletcher
LQ Newbie
 
Registered: Sep 2011
Posts: 3

Rep: Reputation: Disabled
Help needed on language change-adding folder in expression (PERL)


Hello,

I'd really like some help on this, a PERL-CGI script.

How can I add the server's root folder of "my_folder" to this:

~ s/_e\.shtml/_f\.shtml/

This searches all occurences of _e.shtml and replaces them with _f.shtml (it being in the same folder).

Yet, in between that, I would like to add the server's root of "french" folder in there.

For example; and yes I know it's not right:

~ s/_e\.shtml/(MY_FOLDER)_f\.shtml/

I would really appreciate anyone's help on this one, as I can't seem to get it right, with what I know so far.

The complete script is below, which works but only if both language files (file_e.shtml and file_f.shtml) reside in the same folder. I need to be able to have the script work, so that it will get the 'french' file (file_f.shtml) in the /f/ folder (/ being the root).

#!/usr/bin/perl
#
#
# get the URL for the Web page that called this script
$calling_page = $ENV{'HTTP_REFERER'};


if($calling_page =~ /(.*)\#.*/) {

# # only take the first part up to the #
$calling_page = $1;}

# ignore any file that is not _e.shtml or _f.shtml and do nothing!

# is this an _e.shtml file?

if($calling_page =~ /_e\.shtml/) {
# replace the suffix

$calling_page =~ s/_e\.shtml/_f\.shtml/;

print "Location: $calling_page\n\n";

# then is this an _f.shtml file?

}

Last edited by fredfletcher; 09-12-2011 at 01:02 AM. Reason: added PERL at end of subject
 
Old 09-12-2011, 07:14 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
As no-one has answered, I'll give it a crack.
TBH, I'm not entirely clear on what you're doing without a fuller example of the data (ie src & tgt filepaths).
However, have a look at this snippet (tested), which I believe may be the answer, or at least close.

Code:
$var1='/dir/e/x_e.shtml';
print "$var1\n";

$var1 =~ s/_e\.shtml/_f\.shtml/ ;
print "$var1\n";

$var1 =~ s:/e/:/f/: ;
print "$var1\n";

# output is
/dir/e/x_e.shtml
/dir/e/x_f.shtml
/dir/f/x_f.shtml
Note that the s/// notation in Perl is borrowed from sed, and like sed, you can use a different pattern separator if the string(s) use the '/' char eg use ':' instead, to avoid LTS https://secure.wikimedia.org/wikiped...hpick_syndrome

HTH
 
Old 09-12-2011, 07:22 PM   #3
fredfletcher
LQ Newbie
 
Registered: Sep 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
Hello and thanks for replying. The entire script is above; the script is to show the user, a link to the same page but in a different language when they click on the .pl link; for example "This page in English" from a French page (welcome_f.shtml) which would lead to the English equivalent of (Welcome_e.shtml) but in a different folder, being either /e/ or /f/ (e-English and f-French). TBH, I don't know why the "print" statement is doing in my original script lol. That's probably why it might be confusing.
 
Old 09-12-2011, 07:31 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Well my example changes the filename and then changes the dir path for you.
Does that not work, if not why not?
I always advise testing as a non-CGI script first to get make it easier to get the processing code correct, then use the same method inside the CGI version.
 
Old 09-13-2011, 09:29 AM   #5
fredfletcher
LQ Newbie
 
Registered: Sep 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
I'm a "newbie" in a way, but can modify scripts to a certain point, just can't write them from scratch. Am trying to figure out how to implement your suggestion into my existing script. Any chance on a total re-write with my example above?
 
Old 09-14-2011, 02:15 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Not really ... I don't do CGI

Anyway, just remember that what you are fundamentally asking about (I believe) is really just string shuffling; it doesn't matter that they contain dir/file paths. So, just write a basic script like mine and experiment until you 'get it'
Here are some good Perl links
http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials


and ask again if you get stuck
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Can the language of fedora boot message change to other language, zh_CN,e.g.? jimtony Fedora 2 01-11-2011 04:03 AM
Expression Simplification (Adding like terms) [C++] Galib Programming 9 10-08-2010 08:06 AM
Perl Regular expression to change character dissident85 Programming 2 05-27-2009 12:29 AM
sed regular expression help needed Dew Linux - Newbie 1 03-30-2005 03:59 PM
Packages needed to change language serquicia Fedora 0 01-26-2004 12:06 PM


All times are GMT -5. The time now is 08:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration