Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Retrievtring strings from a file.

Reply
Thread Tools

Retrievtring strings from a file.

 
 
j2ee@att.net
Guest
Posts: n/a
 
      07-15-2004
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..
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      07-15-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in news:f425dc38.0407141900.91eead0
@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:umper;
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'
}
};
--
A. Sinan Unur
(E-Mail Removed)d
(remove '.invalid' and reverse each component for email address)

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-15-2004
(E-Mail Removed) wrote:
> 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?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-15-2004
(E-Mail Removed) wrote (to me privately):
> Gunnar Hjalmarsson wrote:
>>
>> 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!

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
How to generate k+1 length strings from a list of k length strings? Girish Sahani Python 17 06-09-2006 11:01 AM
Catching std::strings and c-style strings at once Kurt Krueckeberg C++ 2 11-17-2004 03:53 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM
Comparing strings from within strings Rick C Programming 3 10-21-2003 09:10 AM



Advertisments