Help with perl script while reading a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with perl script while reading a file
# 1  
Old 05-21-2010
Question Help with perl script while reading a file

Hi Everyone, I am very new to perl, but came across a situation wherein I have to read a c++ header file and write the datatype, its identifier and also the length to an excel file. There can be other header files, in the directory but I should browse through the file which has only "_mef:" string in it. Once I have such a file, then I should again browse through the file to find out the native datatypes, such as int, char, short etc....and write the identifier and its length to csv file.

For example..

header.h

Code:
class a_mef:public data
{
    public:
            char a;
            short value;
            float  val;
            char  b[10];
}

Then the csv file should have the output as

header.h (Name of the header file)

Code:
Field      Type    Length
a          char    1
value      short   2
val        float   4
b          char    10

I would greatly appreciate if anyone can help me to achieve this.

Regards,
RB

Last edited by Scott; 05-21-2010 at 02:30 AM.. Reason: Please use code tags
# 2  
Old 05-21-2010
Ok, first show us what have you tried so far? That would help to move the solution further.
# 3  
Old 05-22-2010
More information on the issue

Hi, thanks for replying. Please find the below solution that I was trying to do.

First I read the directory in which my header files are there, and then read each file to search for the string "_mef"

Code:
#!/usr/bin/perl

use warnings;
my @files = glob("*.h");
$find = "_mef";
foreach my $file (@files)
	{
		FindString($file);
	}

sub FindString
	{
		my $filename = $_[0];
		open (FILE, "<$filename") or die $!;
		my @file = <FILE>;
	
		for(@file)
		{
			if($_=~/$find/)
			{
				foreach $line (@file)
				{
# This is where I am unable to proceed further depending on the below scenario

This will always be a string in the header file say "xyz" until which I have to browse through the file, and then write all the data types, their identifiers and values that come before that string. There could be few which come after the string "xyz" but I have to ignore them.

What I am want to achieve is to read line by line in the file until I hit the "xyz"
and then write the info into an excel file.

Kind Regards,
RB
# 4  
Old 05-22-2010
To write into an Excel file, you will need the module for that. You can find it on CPAN. John McNamara / Spreadsheet-WriteExcel - search.cpan.org

If you want it to quit after the first instance of xyz, you could put an if statment in your foreach statement. Something like:

Code:
if ($_ -eq "xyz") { WriteExcel($_); return 0; }

The return 0 will force it to stop and exit the sub routine and back to the main routine. Of course, WriteExcel is another sub routine for writing your excel document.

Hope this helps!
# 5  
Old 05-22-2010
Can you attach a couple of that header files and tell what is the real string instead of xyz?
Please also specify the expected output. I hope a comma-separated CSV file will be sufficient,
so we can avoid use of extra modules.
Do you need a complete/full Perl solution or can you also accept one awk/sed command before
the actual reformatting through Perl starts?
I cannot guarantee that I can code a solution for it, but I'd definitely give it a try.
# 6  
Old 05-24-2010
Sample header file

Hi, pseudocoder...many thanks for your reply. Please find the sample header attached

Please see the desired output after the above header file is processed.

Code:
Name of the file: sample.h

Id         Type   Size

name    char    23
address char    125
age       short   2
entries   entries 2

StructId  Type  Size
age          short  2
float         salary 4

The "recend" should not be read and also, every line after the recend should not be read at all.

It would be very helpful to me if you propose any solution in perl, since I am already using it.

Thanks in advance and awaiting your reply

Regards,
Ramakanth
# 7  
Old 05-24-2010
Hi Pesudocoder forgot to mention, comma-separated CSV file output will also be sufficient,
but with the values that I mentioned in the above output format

Regards,
RB
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Error while reading variable from a file in perl script

I have a file abc.ini and declared many variables in that file, one of the variable(DBname) value I am trying to read in my perl script but getting error. File abc.ini content # database name DBname =UATBOX my $ex_stat; my $cmd_output; $ex_stat = "\Qawk '/^DBname/{print... (2 Replies)
Discussion started by: Devesh5683
2 Replies

2. Shell Programming and Scripting

Need help with perl script with a while loop reading file

Good morning, I appreciate any assistance that I can get from the monks out there. I am able to get this to work for me so that I can do a hostname lookup if I only specify one hostname in the script. What I want to do is have a file with hostnames and do lookups for each name in the file. Here is... (1 Reply)
Discussion started by: brianjb
1 Replies

3. Shell Programming and Scripting

Perl Script for reading table format data from file.

Hi, i need a perl script which reads the file, content is given below. and output in new file. TARGET DRIVE IO1 IO2 IO3 IO4 IO5 ------------ --------- --------- --------- --------- --------- 0a.1.8 266 236 ... (3 Replies)
Discussion started by: asak
3 Replies

4. UNIX for Dummies Questions & Answers

reading a file in Perl

If a form's action is the following Perl script how do I make it print the entire contents of the file on the screen? if(param()) { my $uploadedFile = param('file');#in the html page 'file' is the value of the name attribute of the input my $fh = upload($uploadedFile); ... (1 Reply)
Discussion started by: zerohour
1 Replies

5. Shell Programming and Scripting

Perl Script Not Reading Input Files Correctly

This is one of the strangest things that's happening to me. I'm writing a new Perl script that is trying to read a file. The file is originally in .mof format, but I also saved the contents into a .txt file. As a simple test, I wrote this: #!/user/bin/perl -w use strict; ... (3 Replies)
Discussion started by: kooshi
3 Replies

6. Shell Programming and Scripting

Help need in perl script reading from file

Need perl script, data file will be csv format. I have text file contains 2 colums. Filename Foldernumber aaaa 13455 bbbb 23465 cccc 26689 I have two location 1. files present and 2. folders present. I need to search for file and folder if folder... (3 Replies)
Discussion started by: hnkumar
3 Replies

7. Shell Programming and Scripting

SOLVED: reading config file in a perl script

Hi! I have a need to do this in Perl. script.pl -config file The script would be doing a wget/LWP on a URL which is defined in the config file. So when I run the script it should return either one of these conditions - 1) OK with exit status 0. Should also print "wget URL" 2)... (6 Replies)
Discussion started by: jacki
6 Replies

8. Shell Programming and Scripting

perl - reading from a file conditionally

Hi, I am new to perl. I want to read from a file on the basis of some conditions.. I want to define parameters in a configuration file in such a manner like... etc.. in my perl script, theer is a variable like this.. then i want to read values from first if block from the file... (1 Reply)
Discussion started by: shellwell
1 Replies

9. Shell Programming and Scripting

Reading each line of a file in perl script

HI I need to read each line (test.txt) and store it in a array (@test) How to do it in perl. Suppose i have a file test.txt. I have to read each line of the test.txt file and store it in a array @test. How to do it in perl. Regards Harikrishna (3 Replies)
Discussion started by: Harikrishna
3 Replies

10. Shell Programming and Scripting

Perl Reading from File

is there a perl equivalent to sscanf? or something where I get the strings separated by spaces? (1 Reply)
Discussion started by: karyn1617
1 Replies
Login or Register to Ask a Question