Basic Perl - removing white from split field.
Im trying to take a pipe delimited file and remove the white spaces except for particular fields specified. I would like this ARG to accept multiple files but I havnet got that far yet. For some reason its not working.. if someone could please help, it would be most appreciated.
Code:
$SourceFile = $ARGV[0]; |
Hi, neighbor-to-the-south.
It might help if you posted samples: the input data, how you want it to look as transformed, and how your code is mangling it ... cheers, makyo |
Quote:
|
sorry
the input file would be something like
First Name|Date of Birth| City | State | phone Joe Smith |01 01 1990 |New York|NY|212 555 1212 Im curious how I can remove all white spaces except for the fields specified from the command line ( ARGV[1] ). so if thats my input file, and I entered $script.pl 1,3 the output would be Joe Smith |01011990|New York|NY|2125551212 this is just an example, the actual input file has many many more fields which is why I want to specify which fileds NOT to remove white spaces rather than which fields to remove. Thank you for your help! |
Hi, ryedunn.
I took your script and changed it a bit. I added some debugging prints so that you can enable them to see intermediate data transformations. To do that, just interchange the debug statements in the code below. I usually write to STDOUT so that I can redirect the output as I chose. Keep in mind that this is a skeleton of one way to handle the data, and you might need to add to it to suit your situation. Run the code, figure out what it is doing and if you have questions that are not answered by looking in a perl book, then feel free to ask ... cheers, makyo (73) #!/usr/bin/perl Code:
#!/usr/bin/perl Code:
% ./p1 1,4 data1 |
Wow
Oh wow that great, and its teaching me a lot more about perl in general. this is pretty far over my head but I dot have Learning Perl and The Perl cookbook by O'Reilly to help me sort out some of the commands Im not familiar with.
Just one question, since there are more fields to remove white than not, how hard is it to reverse? (ie 1, 4 will NOT remove white from those fields?) and how do I use the debug option? Very very cool stuff. |
Hi, ryedunn.
I'm glad you liked it. Once you have written a number of programs in a language you will probably be able to think in that language, and, for example, your fingers will seem to know to type in a ";" at the end of a perl statement, etc. I believe this is similar to knowing a natural language well enough so that you dream in it. What I call "makyo's limit" for this number is around 100. To modify the program from changing a field to not changing, I recommend that you think about a way that would consider every field, and if the field is not to be squeezed, then skip over that. That means you need to see how many fields there are and have a loop that looks at every field. Chapters 3 and 10 in Learning Perl, 3rd Edition discuss control structures. Get the job done, but don't be afraid to experiment by writing little scripts to make sure you understand some points of the language. Remember that this is one way to accomplish the specific task you are addressing, and that there are others ... cheers, makyo |
An alternative, in Python:
Code:
import sys Code:
c:\> python test.py 1,2 |
All times are GMT -5. The time now is 01:20 PM. |