david_lyon has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: join mutiple files on first column
by moritz (Cardinal) on Mar 27, 2011 at 14:54 UTC | |
It shouldn't be too hard to do, perlintro teaches you everything you need to know to accomplish it. We are here to help you to learn perl, not to write the scripts you want. If you show some effort, we will certainly help you. | [reply] |
|
Re: join mutiple files on first column
by ww (Archbishop) on Mar 27, 2011 at 14:56 UTC | |
I ask, because we prefer to teach, rather than do-for, those who bow "and send Praises." We also take the soft soap more seriously if you trouble yourself to read Update: upon rereading this screed, I see I've failed to be specific about the reference to "I know what I mean;..." : Given your OP, we have no idea of the format of the "text" files in particular WRT the "first column" - are you talking about a simple text file, a CSV or Tab Delimited File or something else. | [reply] |
|
Re: join mutiple files on first column
by graff (Chancellor) on Mar 27, 2011 at 14:59 UTC | |
See what you can do. If you have trouble, show us the code you tried, and we'll be able to help out with the details. (I'm hoping I understood your task correctly -- if I didn't, please clarify.) UPDATE: Actually, the method can be stated even more simply than what I showed above. In effect, you can leave out the part that handles the first file separately, and just do the "for" loop over all files in the list -- i.e. just treat all files in the list the same way. | [reply] [d/l] |
|
Re: join mutiple files on first column
by TomDLux (Vicar) on Mar 27, 2011 at 17:30 UTC | |
You have 75% of a shell solution there. All you need to complete a shell solution is to append the grepped values onto a destination file. On the other hand, if you want a Perl solution, go for a Perl solution. Shelling out constantly is going to be relatively expensive, so instead read the files, once. As you read each file, split it into fields and store a single row as an array. Store that array in a hash keyed by the primary field, creating the entry if it doesn't exist, appending the field values onto the existing array if it does exist. As Occam said: Entia non sunt multiplicanda praeter necessitatem. | [reply] |
|
Re: join mutiple files on first column
by planetscape (Chancellor) on Mar 27, 2011 at 19:59 UTC | |
See join - join two files according to a common key. HTH, planetscape | [reply] |
|
Re: join mutiple files on first column
by graff (Chancellor) on Mar 30, 2011 at 22:42 UTC | |
I get the impression that you aren't really interested in learning to program in Perl -- so go ahead and just use R, no problem there. If you actually do want to learn perl, you might try the algorithm I proposed in my earlier reply, instead of copying code that someone else wrote and that you don't understand. It's not hard to write a working perl script based on a decent pseudo-code description -- you just have to settle a few details, like "where does the list of file names come from?" Here's an example that assumes the list of file names comes from command line args (which end up in @ARGV) -- that is, you would invoke the script like this: That assumes that your 30 text files are all in the current working directory, and you are able to create a new file in that directory. The following example adds a few extra steps that weren't covered in my earlier post: Now, if there's anything there you don't understand, you'll need to do some reading, check some tutorials, search through some perl documentation, etc. That way, you're more likely to be able to write a script on your own the next time a task like this comes up. (And you're more likely to be able to fix this one, if/when things don't go the way you expect -- e.g. if some files have different keys than other files, etc). | [reply] [d/l] [select] |
by david_lyon (Sexton) on Apr 08, 2011 at 00:36 UTC | |
Thanks so much for your very detailed help, very much grateful. I shall be making use of it from now onwards Thanks again graff! | [reply] |