473,404 Members | 2,178 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,404 software developers and data experts.

windows $argv problems

58
Hey all. complete newbie to perl here.. been tasked with learning and altering some basic arguments in a perl script someone wrote for our group. having issues!
here is a tidbit of the code I need to alter..

Expand|Select|Wrap|Line Numbers
  1. $fileInput=$ARGV[0];
  2. $fileOut="output".$ARGV[0];
  3. $fileCompare="compare".$ARGV[0];
  4. $fileTtbl="Ttbl.csv"
  5. $file0=$ARGV[1]
  6.  
  7. open (READER,$fileO);
  8. open (TT,$fileTbl);
  9. open (READ, $fileInput);
  10. open (WRITE, ">$fileOut");
  11. open (SPIKE, ">$fileCompare");
I am running on winXP from command line...
so this code works fine if say I am in the directory where the .pl and the .csv files are located.. eg
d:\temp>perl altercsv.pl test.csv test.csv
I want to be able to change the directory of the .csv files.. eg
c:\>perl d:\temp\altercsv.pl d:\data\test.csv d:\data\temp\test.csv

I've seen on other posts where perl uses the backslash as an escape? not complete sure what this means, and I also have seen where perl treats \ and / as the same, so tried
c:\>perl d:/temp/altercsv.pl d:/data/test.csv d:/data/temp/test.csv
have also tried \\ and "" and ''
not sure where to go from here! any help will be greatly appreciated!
Thanks,
eric
Jun 14 '07 #1
7 2825
miller
1,089 Expert 1GB
Welcome Eric,

Your script assumes that everything is in the current working directory. You aren't going to be able to treat things as if their in other directories without actually modifying the script.

The module that will be most helpful to you is File::Basename. It allows you to easily break a filename into it's path and basename parts. It's also part of perl's core libraries, so you don't have to worry about installing it.

perldoc File::Basename


Anyway, here are some changes that will get you on your way to fixing the script. Please note that along with the minimal directory support, I've also added error messages for your open statements. These are very important; get in the habit of including them. I've also added "use strict;". This change may be more problematic for you as it requires that all the variables in the script be declared with "my". You may want to take that line out for now, but I strongly encourage you to add it back in as it will help you debug the script and more easily maintain it.

Obviously, it sucks taking over someone else's bad code. Even more so when you're just learning. So I wish you luck.

Expand|Select|Wrap|Line Numbers
  1. use File::Basename;
  2.  
  3. use strict;
  4.  
  5. my $fileInput = $ARGV[0];
  6. my $file0 = $ARGV[1];
  7. my $fileTtbl = "Ttbl.csv";
  8.  
  9. my ($finBase, $finDir) = fileparse($fileInput);
  10. my $fileOut = $finDir . "output" . $finBase;
  11. my $fileCompare = $finDir . "compare" . $finBase;
  12.  
  13. open(READER, $fileO) or die "Can't open $file0: $!";
  14. open(READ, $fileInput) or die "Can't open $fileInput: $!";
  15. open(TT, $fileTbl) or die "Can't open $fileTbl: $!";
  16. open(WRITE, ">$fileOut") or die "Can't open $fileOut: $!";
  17. open(SPIKE, ">$fileCompare") or die "Can't open $fileCompare: $!";
  18.  
- Miller
Jun 14 '07 #2
KevinADC
4,059 Expert 2GB
I believe this should work:

c:\>perl d:/temp/altercsv.pl d:/data/test.csv d:/data/temp/test.csv
Jun 14 '07 #3
erbrose
58
Thank you so much Miller. Definitely on the right track now and slowly but surely picking this code apart and pulling it all back together. Tried 'use strict' but the code I am dealing with if full of holes, so for now have to leave it out, but thanks for the advice!
Cheers,
eric
Jun 14 '07 #4
miller
1,089 Expert 1GB
It's when it's full of holes that it's often most important to add in the enforcement of declaration. It's the only way to figure out what's required where. And to discover possible bugs.

Anyway, good luck in your endevour.

- Miller
Jun 14 '07 #5
erbrose
58
One more question on this bit of code. After reading more about FileParce on windows, line 7 should be getting the "fileOut" $fileName (ie output_test) right? but it isn't... any more thoughts...

c:\>perl
d:/temp/code.pl
d:/temp/data/test.csv ($argv[0])
d:/temp/output/output_test.csv ($argv[1])
d:/temp/data/Ttbl.csv ($argv[2])
d:/temp/dummy/compare.csv ($argv[3])

(new lines and $argv's were placed for convenience of reading only)

Expand|Select|Wrap|Line Numbers
  1.  use File::Basename;
  2. my $fileInput=$ARGV[0];
  3. my $fileInput2=;$ARGV[1];
  4. my $fileTTable=$ARGV[2];
  5. my $fileO=$ARGV[3];
  6.  
  7. my ($fileName, $finBase, $finDir) = fileparse($fileInput2);
  8. my $fileOut = $finDir.$fileName.$finBase;
  9. my $fileSpike = $finDir."compare".$finBase;
Thanks again all!
Jun 15 '07 #6
erbrose
58
Sorry should have worked on it more before I posted. I did manage to figure it out!
Thanks again everyone!
eric
Jun 15 '07 #7
miller
1,089 Expert 1GB
Glad you were able to figure it out :)

Well done,
- Miller
Jun 15 '07 #8

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

Similar topics

14
by: Giles Brown | last post by:
For my sins I'm a MS Windows user at work and apart from that I have a small problem ... I like to write python scripts to do small tasks and then double click on them from the file explorer to...
10
by: Robin Sanderson | last post by:
Sorry in advance if this is a stupid question - I am new to C++. In the process of converting program to be run from the command line into a function to be run from another program I noticed...
3
by: alef | last post by:
Hi, I have the following code which is driving me crazy. I compile it on MacOSX and it keeps crashing upon entering a command in the program (ran trough gdb) pwd Program received signal...
0
by: Patrice de Boisgrollier | last post by:
Hello I'm having trouble under windows 2000 dealing with programming a ping in a Visual C++ 6 environment My problem is that my code is based on a code sampl from Microsoft that is found in...
3
by: Chris Paul | last post by:
I'm having trouble with PHP & PostgreSQL/OpenLDAP/Apache on Windows. I've set this up countless times on BSD (piece of cake) but I'm trying to do this on Windows now so that my developer can work...
0
by: Stefan Krah | last post by:
Hello, I'm trying to run a Python script as a Windows service with a defined shutdown. The script (enigma-client.py) handles the communications with the server in a distributed computing effort...
2
by: merrittr | last post by:
I have a small program to read data from some data files , in it I use string types to build the data file name but when I try to compile it I get: (see code below error) ...
17
by: Matt | last post by:
Hello. I've got a very strange problem. Basically I have a programme where I wish to view all the strings in the argv array so I can see what arguments are being passed to the programme. ...
11
by: Unknown Hero | last post by:
Tim Golden wrote: The first link which points to the Python documentation for the _winreg module I already checked, even before coming here. I am wondering how I should do the loop I need (go...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.