darrengan has asked for the wisdom of the Perl Monks concerning the following question:
file2
333,felix
777,chris
888,darren
The script should print "darren" as it appear on file1 and file2. i have tried the following but doesn't work:
It does not work, maybe there is some problem with the logicopen(file1,"file1.txt"); while (<file1>) { chop(); $REC = $_; @LINEREC = split(/\,/,$REC); print "$LINEREC[0]\n"; } open(file2,"file2.txt"); while (<file1>) { chop(); $REC = $_; @LINEREC = split(/\,/,$REC); print "$LINEREC[1]\n"; } if ( $LINEREC[0] eq $LINEREC[1]) { print $LINEREC[0]; {
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Compare 2 files and get data
by jpeg (Chaplain) on Sep 05, 2005 at 08:49 UTC | |
I don't mean to cramp your style, but there are a few points I feel I should mention.
-- jpg | [reply] [d/l] [select] |
|
Re: Compare 2 files and get data
by TedPride (Priest) on Sep 05, 2005 at 09:19 UTC | |
| [reply] [d/l] |
|
Re: Compare 2 files and get data
by reneeb (Chaplain) on Sep 05, 2005 at 08:41 UTC | |
It's untested... | [reply] [d/l] |
|
Re: Compare 2 files and get data
by blazar (Canon) on Sep 05, 2005 at 10:08 UTC | |
chomp. (Do a big favour to yourself and) at the top of your program. Then the above would have to become It's not that much more typing and it won't hurt your fingers. OTOH it will help you immensely to avoid common mistakes... Huh?!? Did you paste your script or did you retype it? Hint: the former is a much more reliable option... Ditto as above wrt retyping. It is common sense to post code that at least compiles. i have tried the following but doesn't work:Indeed. How 'bout (something along the lines of:) Note: this is meant as being a minimal example. If your actual code is more complex of course you'd better open your filehandles explicitly. I also made some assumtpions: for example that you don't want to output duplicate entries or that the format of 'file1' is different from that of 'file2' and that both are fixed and corresponding to the one that one could infer from your examples. | [reply] [d/l] [select] |
|
Re: Compare 2 files and get data
by blazar (Canon) on Sep 05, 2005 at 14:04 UTC | |
(this assumes all of the files it is given are in the same format as your "file1". Modify at will!) | [reply] [d/l] |
|
Re: Compare 2 files and get data
by josera (Beadle) on Sep 05, 2005 at 13:02 UTC | |
Perhaps you wold have to use two while nested. In pseudocode, what i've think that you could use is:
It compare each field in file1 with each field in file2. If what you want is that the two fields will be in the same line, then you could use, with only one while: I hope that this pseudocode will help you Yours sincerelly, José Ramón Martínez | [reply] [d/l] [select] |
|
Re: Compare 2 files and get data
by darrengan (Sexton) on Sep 07, 2005 at 01:35 UTC | |
Thanks for all the tips and guide. I finally manage to work around the code and solved my problem. I am using a "IF" and "While" within a "While". Below are my code and it works well. I am not too sure if by running in this flow will it take up resourses or not as it will compare 60,000 of records in the file. Hope that this discussion and codes helps other who seek wisdon. Cheers, Darren | [reply] [d/l] |
by blazar (Canon) on Sep 07, 2005 at 13:02 UTC | |
Thanks for all the tips and guide. I finally manage to work around the code and solved my problem. I am using a "IF" and "While" within a "While".I'm glad you solved your problem. Incidentally, however, I'd like to point out that there's not such a thing as "IF" in Perl, nor "While". If you want to visually mark in a distinctive manner such keywords, you may put them between <c> or <code> tags. For example this: <c>split</c> is rendered like this: split. But for functions, you can also use [doc://split] which is rendered as a hyperlink like this: split Below are my code and it works well. I am not too sure if by running in this flow will it take up resourses or not as it will compare 60,000 of records in the file.Well, let's say that it doesn't seem a very smart way to do what you want. I think you should take another look at other suggestions that were given to you, e.g. in terms of using a hash, which seems most reasonable for such a task, instead. (If you didn't understand some of the replies you can ask for further clarification, of course.) Basically you're re-opening and re-reading your second file across all the lines of the first one, and this makes your program IO intensive. However I will add a few further comments about your code as is. Hope that this discussion and codes helps other who seek wisdon.Of course it will, just as much as quite about every discussion here does... First of all, and most importantly (although you may not see why it is, ATM -- but then please trust us!) more than one monk already recommended to put the following two lines at the top of your script: You'll notice that with one or two exceptions even those who didn't tell you to do so, did include them in their own code examples. There's nothing strictly wrong with this. But it's better to use
Notice that I also put a \n at the end of the die error message, because I prefer it like that, for this kind of errors (I don't think the final user is interested in the additional details that get printed if you omit it), though YMMV. Nowadays no one ever uses chop to do this. They use chomp instead. Please check the documentation for both. No need to copy $_ to $REC just to pass it to split. The former is even the implicit second arg to it, if none is given!! No need to use a temporary array (why all those uppercase letters, BTW?) just to slice it, either. You can slice a list as well. Thus the above may have been simply Incidentally also note that it's not necessary to quote the comma in the regex, as it has not a special meaning. Hmmm, here your opening the same file over the outer cycle over and over again. But you explicitly close it only out of the outer cycle, at the end of your script along with file1 (which is not strictly necessary after all, since open filehandles get closed on program ext anyway). Here you could either use a lexical handle as recommended above, which gets automatically closed on exiting the lexical scope it s defined in, or else you may just open it once at the top (but also then, use a lexical in any case!), at the same time as file1 and use seek to "roll it back". Update: a possible rewrite of your code (same logic!) along the lines of the hints given above: HTH | [reply] [d/l] [select] |