deadlift has asked for the wisdom of the Perl Monks concerning the following question:
Hello, I am in need of assistance with a small problem I found in my code that has turned into an issue. I am still relatively new to Perl. My data is two text files that look similar to these two examples. My script currently opens the files, puts them into separate comma delimited arrays(@names, @jobs...), and exports certain differences to another text file. What I need is to print out the change of job title from file one to file two, and I also need the name of the persons whose job title changed.
It should look like this: Jim => Prev: MD Now: Doctor for all changed titles. I know my code is stopping at the new name value and I tried different things like another for loop, or adding 1 to $i, and I even looked at changing it to a hash. I can't figure out a way to skip over the new name in the new array and still stay the same in the old one.
File1 Name, Job, City, State Jim, MD, Pinole, CA Tara, Nurse, San Pablo, CA Julie, MD, San Pablo, CA Sherry, Nurse, Pinole, CA George, MD, Pinole, CA Tim, Nurse, Pinole, CA Bob, Nurse, Pinole, CA Uma, MD, San Pablo, CA Kate, Nurse, Oakland, CA Pete, MD, San Pablo, CA
File2 Name, Job, City, State Jim, Doctor, Pinole, CA Tara, Nurse, San Pablo, CA Julie, Doctor, San Pablo, CA Sherry, Nurse, Pinole, CA Jan, Doctor, San Pablo, CA George, Doctor, Pinole, CA Tim, Nurse, Richmond, CA Bob, Nurse, Pinole, CA Uma, Doctor, San Pablo, CA Kate, Nurse, Oakland, CA Paul, Doctor, Oakland, CA Ruth, Nurse, Richmond, CA Joe, Nurse, Oakland, CA Nick, Nurse, Pinole, CA Pete, Doctor, San Pablo, CA
$file = "1.txt"; (@namesOld = (), @namesNew = (), @jobOld= (), @jobNew= ()); open (FILE, '<', $file) || die; while (<FILE>) { @hospWorkersOld = split(/,/, $_); push (@namesOld, @hospWorkersOld[0]); push (@jobOld, @hospWorkersOld[1]); } close FILE; $file2 = "2.txt"; open (FILE, '<', $file2) || die; while (<FILE>) { @hospWorkersNew = split(/,/, $_); push (@namesNew, @hospWorkersNew[0]); push (@jobNew, @hospWorkersNew[1]); } close FILE; @oldJobs=(); @newJobs=(); @newNames=(); for ($i = 0; $i < scalar(@jobNew); $i++ ) { if ($namesOld[$i] eq $namesNew[$i] && $jobOld[$i] ne $jobNew[$i] +) { #print "$namesNew[$i]--$jobNew[$i]\n"; push (@newJobs, $jobNew[$i]); push (@oldJobs, $jobOld[$i]); push (@newNames, $namesNew[$i]); } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Arrary issues
by Cristoforo (Curate) on Mar 25, 2019 at 21:24 UTC | |
|
Re: Arrary issues
by choroba (Cardinal) on Mar 25, 2019 at 21:26 UTC | |
|
Re: Arrary issues
by tybalt89 (Monsignor) on Mar 25, 2019 at 23:19 UTC | |
|
Re: Arrary issues
by Marshall (Canon) on Mar 26, 2019 at 05:29 UTC |