Retrievtring strings from a file.

J

j2ee

I have a file which has these 3 columns (for example)

Name Size1 Size2
+ abc_p.h 12345 432
*unknown
+ dfe_e_io.h 210989 123
+ dfx_e_io.c 210912 1290 and so on upto 500 entries.

I have to retreive Name(file names) and size1 and store it in an array

Then I have to retrieve name and size2 and store it in another array
My solution:
I checked if the each line in the file matched the file name using
regular expression. If there is match

then store those filenames and size1 in array1 using substr operation.
But the problem is I hardcoded the values of starting
position and length of the string in the

substr operation. So my code will work only for a given length of
string. for eg. say 20. If a name is of lenght> 20, my code won't
work.
Can you tell if there is a generic way of writing regular
expression that matches the name in my

file , and then size1 and stores them in a array? Special cases: IN
the name column you may have some

unwanted string like *unknown which should be ignored.

Let me know if you need clarifications. Thanks..
 
A

A. Sinan Unur

(e-mail address removed) wrote in @posting.google.com:
I have a file which has these 3 columns (for example)

Name Size1 Size2
+ abc_p.h 12345 432
*unknown
+ dfe_e_io.h 210989 123
+ dfx_e_io.c 210912 1290 and so on upto 500 entries.

I have to retreive Name(file names) and size1 and store it in an array

Then I have to retrieve name and size2 and store it in another array
My solution:
I checked if the each line in the file matched the file name using
regular expression. If there is match

It would be best if you can show actual code rather than verbally
describe it. To help others test and help with your code, I would suggest
the following template

use strict;
use warnings;

# your code goes here

# provide a reasonable number of lines of sample
# data below

__DATA__
+ abc_p.h 12345 432
*unknown
+ dfe_e_io.h 210989 123
+ dfx_e_io.c 210912 1290

It seems to me that you want to skip lines that start with a * and
process lines that start with a +.

I am also not sure multiple arrays would be a good data structure. How
about the following?

use strict;
use warnings;

my $data = { };

while(<DATA>) {
if( /^\s*\+\s*(.+?)\s+(\d+)\s+(\d+)\s*$/ ) {
$data->{$1}->{size1} = $2;
$data->{$1}->{size2} = $3;
}
}

use Data::Dumper;
print Dumper $data;

__DATA__
+ abc_p.h 12345 432
*unknown
+ dfe_e_io.h 210989 123
+ dfx_e_io.c 210912 1290

C:\Home> t.pl
$VAR1 = {
'dfe_e_io.h' => {
'size1' => '210989',
'size2' => '123'
},
'dfx_e_io.c' => {
'size1' => '210912',
'size2' => '1290'
},
'abc_p.h' => {
'size1' => '12345',
'size2' => '432'
}
};
 
G

Gunnar Hjalmarsson

I have a file which has these 3 columns (for example)

Name Size1 Size2
+ abc_p.h 12345 432
*unknown
+ dfe_e_io.h 210989 123
+ dfx_e_io.c 210912 1290 and so on upto 500 entries.

I have to retreive Name(file names) and size1 and store it in an
array

Why an array? Why not a hash?
Then I have to retrieve name and size2 and store it in another
array
My solution:
I checked if the each line in the file matched the file name using
regular expression. If there is match then store those filenames
and size1 in array1 using substr operation.
But the problem is I hardcoded the values of starting position and
length of the string in the substr operation. So my code will work
only for a given length of string. for eg. say 20. If a name is of
lenght> 20, my code won't work.

Then substr() is not a suitable function for the task, right?
Can you tell if there is a generic way of writing regular
expression that matches the name in my file , and then size1 and
stores them in a array?

You should probably use the split() function or the m// operator
instead of substr().
Special cases: IN the name column you may have some unwanted string
like *unknown which should be ignored.

Let me know if you need clarifications.

What does your code look like?
 
G

Gunnar Hjalmarsson

Gunnar said:
What does your code look like?

##This was earlier code:##
foreach my $line(@file1)
{
chomp($line);
if($line=~/\s\s\d+/)
{

@rom_arr1[$i] = substr($line,0,55);
$i++;

}
}
foreach $i (@rom_arr1)
{
print OUT1 "$i\n";
}

##Then I changed it and used split funtion,but it's not working##
while(<IN1>)
{
if($line =~/\+\s\w\.h/)
{

@rom_arr1[$i]= split(/\s+/,$line);

#@rom_arr1[$i] = substr($line,0,20);
$i++;
}

}
foreach $i (@rom_arr1)
{
print OUT1 "$i\n";
}

When asking about your code, I meant that you should post it to the
group, not to me privately. Anyway, it now appears that you want to
save the result in two new files, and that you don't need any arrays.

This is an interpretation of what you might want:

#!/usr/bin/perl
use strict;
use warnings;

my $infile = 'myfile.txt';
my $outfile1 = 'out1.txt';
my $outfile2 = 'out2.txt';

open IN, $infile or die $!;
open OUT1, "> $outfile1" or die $!;
open OUT2, "> $outfile2" or die $!;
while (<IN>) {
if ( substr($_, 0, 1) eq '+' ) {
my ($name, $s1, $s2) = (split)[1..3];
print OUT1 "$name\t$s1\n";
print OUT2 "$name\t$s2\n";
}
}
close IN;
close OUT1;
close OUT2;

__END__


I can't help feeling, though, that you should take a step back and do
some reading and simple exercises. Especially you should learn to use
strictures and warnings and to do simple debugging. Just saying "it's
not working", as you did above, does not impress anyone.

Please check out this link:

http://learn.perl.org/

Good luck!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top