473,321 Members | 1,667 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

How can I CORRECTLY pass strings as arguments from command line?

1
Without going into details; I have the need to pass a string containing double backslashes \\ from the DOS command line and convert these to single backslashes in the code.

The problem is if I do this from within perl code directly it is no problem but when I attempt to pass a string from the command line, it seems to lose something in the translation and when I try to do the replace it does not work. So the question is HOW to pass the string from the command line so it works. Here is the sample code that works without passing from the command line:

perl source code = test_string_replace21.pl:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. use strict;
  3. # replacing double back slash...
  4. my $string1 = "-----\\---------";
  5. $string1 =~ s/"\\"/"\"/g;
  6. print "The resulting value is : $string1 \n";

This of course works:
Expand|Select|Wrap|Line Numbers
  1. C:\>
  2. C:\>test_string_replace21.pl
  3. The resulting value is : -----\---------
As you can see, the double back slash is changed to a single one as expected.



But now use the following code which should do the same thing as above but takes the string as argument from the command line:

perl source code = test_string_replace2.pl:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. use strict;
  3. my $string1 = $ARGV[0];
  4. $string1 =~ s/"\\"/"\"/g;
  5. print "The resulting value is : $string1 \n";
  6.  
I run it from DOS command line:

Expand|Select|Wrap|Line Numbers
  1. C:\>test_string_replace2.pl "-----\\-----"
  2. The resulting value is : -----\\-----
As you can see, the double back slash is still there!!!!! Why????? And how do I get around it?

Thanks to anyone who can help.
Jul 16 '08 #1
1 1912
numberwhun
3,509 Expert Mod 2GB
Instead of enclosing the back slashes in double quotes, as you did in your first example, do it like so:

Expand|Select|Wrap|Line Numbers
  1. $string1 =~ s/\\\\/\\/g;
  2.  
the extra back slashes escape the meaning of the back-slash in the regular expression, which..... is to escape a character's meaning inside of a regex, thus, making the following back-slash, just a back-slash, without any special meaning.

Regards,

Jeff
Jul 16 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: qazmlp | last post by:
void func() { // Is it by anyway possible to read the value of the first command line parameter i.e. argv here ? } int main() { func() ; // No command line arguments are passed to func(). }
2
by: SunRise | last post by:
Hi I am creating a C Program , to extract only-Printable-characters from a file ( any type of file) and display them. OS: Windows-XP Ple help me to fix the Errors & Warnings and explain...
1
by: amirmira | last post by:
I would like to set command line arguments to a service at install time. I need to do this because I need to get information from different registry locations depending on my command line argument....
12
by: BartlebyScrivener | last post by:
I'm still new at this. I can't get this to work as a script. If I just manually insert the values for sys.argv and sys.argv it works fine, but I can't pass the variables from the command line. What...
3
by: balakrishnan.dinesh | last post by:
hi frndz, As we know that, we can pass command line agrument for C using "scanf" commands, So as same as that, Is there any way to pass those commandline arguments through php code to C and...
7
by: Jack | last post by:
Hi, I am having some problems with char and string datatypes. Here is an example int main(){ string str; cout << "enter string: "; cin >str; }
8
by: svata | last post by:
Hello, as I'm new to python I've stumbled accros os.system and its not very well documented usage. I use Win XP Pro and Python 2.5. Here is the code snippet: ...
56
by: subramanian100in | last post by:
The standard allows that we can copy strings onto arg, arg, etc. Why is it allowed ? What can be the maximum length of such a string that is copied ?
1
by: megh55555 | last post by:
Hi , how can we pass arguments to a function located in xyz.py file using the DOS command Prompt. I know its very easy to pass using the python command line but m finding it impossible to do...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.