sabas has asked for the wisdom of the Perl Monks concerning the following question:
I have two files:
file1.txt (example)
Name School Subject JO EMS Math Jen DES English Mo IES Science Al TES Algebra
file2.txt (example)
Name School Subject Jon EMS Physics
I am wondering what is the best way to replace the first entry in file1.txt(JO EMS Math) with my row in file2.txt (Jon EMS Physics) without changing the row format like padding and spacing.
Note that I know the row number/index that I would like to replace.
I stored and worked on @ and was able to do the replacement however the padding and spacing changed. I also tried to create a temporary .txt file and write the rows however the spacing also changed.
Appreciate if someone can give me an advise..
2020-01-15 Athanasius added code and paragraph tags.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Text File Manipulation
by AnomalousMonk (Bishop) on Jan 14, 2020 at 02:24 UTC | |
... replace the first entry in file1.txt(JO EMS Math) with my row in file2.txt (Jon EMS Physics) without changing the row format like padding and spacing. I don't understand what "padding" means in the context of your question. (In general, I don't understand the context of your question.) I don't understand how "spacing" can be expected to remain the same when two of the three replacement fields have different widths. I stored and worked on @ ... I don't understand what @ is. ... and was able to do the replacement however the padding and spacing changed. Can you please show a Short, Self-Contained, Correct Example of what you have done so far? It might do much to clear up my misunderstandings. Also: Is this a school assignment? It sure looks like one. If it is, please tell us and we will be better able to offer appropriate help. Give a man a fish: <%-{-{-{-< | [reply] [d/l] |
Re: Text File Manipulation
by kcott (Bishop) on Jan 14, 2020 at 07:11 UTC | |
G'day sabas, As already stated, your question is somewhat vague and doesn't give us a lot to work on. We're happy to help you out with things you're stuck on (e.g. some aspect of Perl you're having difficulty understanding) but this is not a code writing service. Another good resource to help you improve your posts is "How do I post a question effectively?". "... without changing the row format like padding and spacing." Have a look at the printf and sprintf functions. These may help with this aspect of your problem. Here's a quick example:
— Ken | [reply] [d/l] |
by sabas (Acolyte) on Jan 14, 2020 at 08:28 UTC | |
I apologize for my elementary coding in Perl. I am a beginner and I been debugging this for a over 3 days and i cannot figure out....the idea i am making is this: file1.txt aaa bbb ccc file2.txt ddd eee fff ggg hhh ggg file3.txt should looks like these: aaa bbb ccc ggg hhh ggg the length of my rows are over 381 and when i wrote it to file3.txt the length becomes < 200 and the padding (space) for each column became one space only per column.
| [reply] [d/l] |
by hippo (Chancellor) on Jan 14, 2020 at 10:12 UTC | |
and the padding (space) for each column became one space only per column. `echo $row >> $EGIS_table_fileName_tmp`; # THIS IS NOT WORKINGGGGGGGGGGGGGGGGGGGGGGGGG! The problem is that instead of opening a file and printing to it you are instead shelling out and running echo from the shell which by default collapses multiple spaces into just one. Have a good read through perlintro, especially the part about how to write to files from within Perl in the section Files and I/O. Good luck. | [reply] [d/l] |
by soonix (Canon) on Jan 14, 2020 at 11:54 UTC | |
(which you are already using), is just an abbreviation for and instead of where open and close are effectively useless, you should use (I prefer to put the file handle after print without a space to help remembering there's no comma after the file handle) If you feel adventurous, you might to look over Text::Table Tutorial, but that's an advanced topic, just to see what's possible. | [reply] [d/l] [select] |
by roboticus (Chancellor) on Jan 14, 2020 at 14:56 UTC | |
sabas-- Your code is frightening. You need to learn how to read code and understand what's happening inside it. Your code has three different places where you're creating the desired row of data (one which looks just fine) and then you're playing with temporary files in desperate attempts at trying to somehow get the data into the file you want. It looks like it would work if you'd remove all the junk you don't really need. I was able to cut your code down into something that looks reasonable in less than 40 lines. I'd ordinarily make a long post describing some ways to improve your code, but I don't have the time to do it today. Instead I'll show you the line that looks like it would work (I changed @rows to $rows to eliminate a warning):
Since that line puts the data in the correct format into @rows, you can delete the code dealing with temporary files and just write the @rows data to the output file. ...roboticus When your only tool is a hammer, all problems look like your thumb. | [reply] [d/l] |
Re: Text File Manipulation
by Laurent_R (Canon) on Jan 14, 2020 at 18:54 UTC | |
| [reply] [d/l] [select] |