Retrievtring strings from a file.

Discussion in 'Perl Misc' started by j2ee@att.net, Jul 15, 2004.

  1. Guest

    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..
    , Jul 15, 2004
    #1
    1. Advertising

  2. 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::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'
    }
    };
    --
    A. Sinan Unur
    d
    (remove '.invalid' and reverse each component for email address)
    A. Sinan Unur, Jul 15, 2004
    #2
    1. Advertising

  3. 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
    Gunnar Hjalmarsson, Jul 15, 2004
    #3
  4. 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
    Gunnar Hjalmarsson, Jul 15, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kurt Krueckeberg
    Replies:
    2
    Views:
    694
    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
    Nov 17, 2004
  2. Rick

    Comparing strings from within strings

    Rick, Oct 21, 2003, in forum: C Programming
    Replies:
    3
    Views:
    367
    Irrwahn Grausewitz
    Oct 21, 2003
  3. Klaus Neuner
    Replies:
    7
    Views:
    471
    Klaus Neuner
    Jul 26, 2004
  4. Girish Sahani
    Replies:
    17
    Views:
    557
    Boris Borcic
    Jun 9, 2006
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    733
    Malcolm
    Jun 24, 2006
Loading...

Share This Page