Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
search for a word and store next 2 decimal numbers

Posted by toshimathur (toshimathur), 4 September 2007
Hi

I have 2 log files, file1, file2.
It is required to extract certain information from each file. Tabulate the information in a new file.

Say for example,
file1:
-------
                                         static                              dynamic
Item1                             23.456                             34.989

file2:
-------
                                         static                             dynamic
Value for Item1            12.345                             32.456

Output should look like:
----------------------------------
                                                        Static                                  Dynamic
                                                  file1       file2                         file1      file2      
Item1                                23.456   12.345                34.989   32.456

There are 12-13 such Items to grep, store values for static and dynamic field, tabulate them.
The problem is that the pattern of both the files is quite different so Items are located at different places in different manner.
Can someone please help me and suggest a way to go with this problem?

Regards
Toshi    

Posted by toshimathur (toshimathur), 4 September 2007
Hey
Message posted above has incorrect indentation.
I am sorry for that as spacing changes after posting.

Toshi

Posted by KevinAD (KevinAD), 4 September 2007
"modify" your post and add the appropriate formatting tags. They are above the message box. You probaly want to use the one with a "#" which is for code.

Posted by KevinAD (KevinAD), 4 September 2007
A suggestion is to use a complex data structure, possibly a hash of arrays, where the item# is the hash keys and the numbers are the arrays values. Here is a link to an off-site tutorial  about complex data, otherwise known as references :

http://perldoc.perl.org/perldsc.html


There may be some examples on the Well House website.

Posted by toshimathur (toshimathur), 5 September 2007
Thank you!
This is my script. It searches for Item1 then, stores Item1 and next 2 decimal numbers in 3 different arrays. Then prints these 3 arrays in a temp file. It does not work.
---------------------------------------------------------------------------------
#!/usr/bin/perl -w

open(IN, "file1") or die "can't find file file1: $!";
open(OUT, ">temp.log");

@Item = ();
@Static = ();
@Dynamic = ();

while(IN) {
   chomp;
   if ($_ = /^Item1/){
     @Item[0] = Item1;
     @Static[0] = /[0-9]\.[0-9]/;
     @Dynamic[0] = /[0-9]\.[0-9]/;
   }
   print OUT "@Item \n @Static \n @Dynamic\n";
}
close(IN);
close(OUT);
------------------------------------------------------------------------------------------


Posted by admin (Graham Ellis), 5 September 2007
Kevin, thanks for your inputs  - I'm away for a few days and haven't had much of a chance to look at a growing pile of questions here and elsewhere.  I agree with you that the two files of data really need to be read in and the data stored in hashes.   So - Toshimarthur - that's a different line of coding to you've started I'm afraid.

The shortest (and most elegant) solution would indeed be a hash of lists but that would also be very hard for a relative newcomer to understand.  I woudl tend to read in the first file, splitting of the first field of each line as the key of a hash, then traversing the second file and printing out data as matches are found.



Posted by admin (Graham Ellis), 5 September 2007
Here's a demonstration of that in principle - the syntax is OK but I have not run it (and I'm typing from an expensive hotel connection tonight)

Code:
# Read first file into a hash

open (FH,"file1");
while ($line = <FH>) {
       chop $line;
       ($key,$value) = split("\s+",$line,2);
       $first{$key} = $value;
       }

# Traverse second file for matching lines

open (FH,"file2");
while ($line = <FH>) {
       chop $line;
       ($key,$value) = split("\s+",$line,2);
       if ($first{$key}) {
               print "$key $value $first{$value}";
       }
}



Posted by KevinAD (KevinAD), 6 September 2007
on 09/05/07 at 17:54:48, Graham Ellis wrote:
Kevin, thanks for your inputs


You're welcome Graham.


Posted by toshimathur (toshimathur), 6 September 2007
Thank you Graham, Kevin!

I have tried using Hash of arrays in my script.
I was able to extract required information and store them in hash of arrays.

But I am stuck again as unexpectedly, an array of 1 of the hashes also picks values of corresponding array element of other hash.

Script uses 2 hashes of arrays. Key elements have same name for both. I need 2 hashes, as items to search in 2 input files need slighty different pattern match.

Posting input files, script and output obtained, on forum will need too much of space.
Graham, I have mailed you sample input files, my script and output. I wanted to copy Kevin but did not know his email ID. Can you please forward it to Kevin?

Looking forward to your valuable suggestions.

Regards
Toshi


Posted by KevinAD (KevinAD), 7 September 2007
Please do not email me  programs or files. Post your perl code and maybe it will be evident where the problem is, but maybe not.

Posted by admin (Graham Ellis), 7 September 2007
I have already emailed you - twice - in private pointing you to the forum in preference to private email.  On a forum, a whole variety of people can look at providing an answer whereas in private individuals who give their precious time for free are working JUST for you.  And they may not be around and they may not have the knowledge themselves either (though Kevin, I know, DOES have the knowledge.

I have already told you, in general terms, where the problems lie in you code, and suggested a route forward.   I understand that you don't like or follow my answer? But it's then very cheeky, in my presence, to ask someone else to look at it by email.  And it's naive to expect me to pass it on.   If it was a technical question I didn't know, I might well have used other sources ....

I will not be emailing your script on to Kevin - not only because of his request, but because I felt it would do you no extra good, because it my standard policy to use forum member's email addresses only for emergencies or where something must be done in private and effects them personally.  I suggest you put yourself into the shoes of others (think how they will feel and why they are helping) before you press for more assistance, and I suggest that you take advise given or at least are circumspect in how you ignore it.

Having said all that ... we were all early learners once and I have made what I feel are good suggestions.  I remain hopeful that you will follow them, and I remain open to following through with my part of them.

Posted by toshimathur (toshimathur), 7 September 2007
Hi Graham, Kevin

Please accept my apologies for not making my intentions clear.

There seems to be some misunderstanding. I am not passing my question from one to another.  Neither am I trying to get my problem resolved personally.

I am a newcomer to forum world.
I had a problem, but did not know how to cut short my code to exact problem area, so I emailed it in order to avoid posting  bulky code, input and output files on the forum.

I very well understand that you guys are doing a tough job, dedicating your precious time only to help clueless individuals like me.
I am moved by the way you guys have helped me and I find a lot of value in pursuing healthy discussions on this forum.

With due respect to your replies, I did not ignore your suggestions at all. In fact, your suggestion helped to move forward and write the script I emailed.
Having moved a step ahead I faced another problem and emailed it and once again seek your help.

But I think I must learn to make problem clear and post it on forum.
Once again thanking you for your help
Regards
Toshi




Posted by KevinAD (KevinAD), 7 September 2007
post your perl code, if it is too much code then yes, maybe nobody will take a close look at it as too much code tends to be a lot of work to read and understand. But let "us"
make that  determination. If it is a lot of code I would probably look over it later in the day when I have less to do and more time to devote to answering a question. Or post just the relevant part of your code.

Posted by admin (Graham Ellis), 7 September 2007
I remain very happy to help, effeciently.  

But from the code I have seen, I remain of the opinion that the forum approach may not be the best for you.  To give you an analogy, a forum helps get someone's car who has broken down on a journey a hand getting back on the road, but I fear that you may be so new to the Perl world that it might end up deeply frustrating and inefficient for all of us as we keep putting you back on the road just to have to rescue you again a mile or two later on what could be a very long journey.  

Please do have a look back at my private email where I made some specific suggestions as to how you might get along much better, and consider following all the suggestions and not just the ones you like.  I've been here before ... I know what works and what causes frustration and I want help you follow the former course not the latter!

Posted by KevinAD (KevinAD), 8 September 2007
Toshi,

I am willing to help as long as I see progress and learning going on. I will stop helping though when I think a person is taking advantage of the generosity of those willing to help or simply appears to be unable to learn. One thing I don't like to do is repeat myself over an over when trying to help a person. That indicates to me that a person is not paying attention or just unable to learn for some reason, and my time would be better spent elsewhere.


I don't think you are trying to take advantage of myself or Graham to get your work done so if you have more questions I am willing to continue if Graham is not, although I think he is. He seems to have some insight into your situation though so I would listen to what he has to say.

-Kevin

Posted by toshimathur (toshimathur), 10 September 2007
Hi Kevin,

Sorry for my late reply. I could not connect to internet this weekend due to some problem in my home network.
It was really kind of you to have decided to help even though my lengthy  code required an extended time attention.

I have modified my code a lot, after Graham's suggestion to remove repetitive lines of code, by using for loops where ever applicable.  
This has cut short my code. Also, the problem with array values overlapping, does not occur anymore.

Thank you!
Regards
Toshi

Posted by admin (Graham Ellis), 10 September 2007
Happy to have been helpful.   And thanks for letting us know.

Graham

Posted by KevinAD (KevinAD), 10 September 2007
ditto  



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho