Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Hello al I am working on a BioInformatics assignment and I am using Perl to parse a few input files. I am new to Perl. Here is my question: I ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2003
    Posts
    18

    Empty lines in a files using Perl


    Hello al

    I am working on a BioInformatics assignment and I am using Perl to parse a few input files. I am new to Perl. Here is my question:

    I am using this to parse the lines:

    ($ID, $Rest) = split ( " ", $_, 2);

    The first variable is an ID and the rest of the line is a description.

    My problem is when a line is blank or empty. My program terminates giving this error:

    use of unitialized string in concatenation . or string a line 33.

    I get one of these for every line that is blank. Can anyone tell me how to just ignore blank lines?

    Thanks

  2. #2
    Linux Newbie
    Join Date
    Aug 2001
    Location
    USA, Texas
    Posts
    217
    Try something like this:

    $someVar .= $Rest if($Rest);


    This is as much as I can tell you without a little more of the code you are using.
    [ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ];
    /* Don\'t make me use it! */

  3. #3
    Just Joined!
    Join Date
    Jan 2003
    Posts
    18
    I'm sorry. The input file is a little different than I described earlier.

    The lines in the file look like this:

    1.1.1.1 00010 00071 00120 00350 00561

    I want to create an output file like so:

    1.1.1.1 00010
    1.1.1.1 00071
    1.1.1.1 00120
    1.1.1.1 00350
    1.1.1.1 00561

    Here is the code that I am using:

    #!/usr/bin/perl -w
    use FileHandle;

    print "Type the name of the input file: ";
    $in_file = <STDIN>;
    chomp ($in_file);

    $_ = $in_file;


    $dot_index = rindex($in_file, ".");

    $out_file = substr($in_file, 0, $dot_index) . ".dat";

    open(INFILE, $in_file) || die "Can't open $in_file: $!";
    open(OUT, ">$out_file") || die "Can't create $out_file: $!";

    while(<INFILE>) {
    chomp;

    ($ID, $Rest) = split ( ' ', $_, 2) if ($ID);

    @maps = split ( ' ', $Rest) if ($ID);

    $i = 0;

    while ( @maps )
    {
    print $ID;
    print OUT "$ID\t$maps[$i]\n";
    $i++;
    }

    }

    close(INFILE) || die "Can't close $in_file: $!";
    close(OUT) || die "Can't close $out_file: $!";

    I made the changes you suggested. I don't get errors any more, but it doesn't write to the output file. Thanks for the help.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Engineer
    Join Date
    Jan 2003
    Location
    Lebanon, pa
    Posts
    994
    I made a few changes to your while loop you might want to look at.
    Code:
    while&#40;<INFILE>&#41; &#123;
    chomp;
                                                                                    
    &#40;@line&#41;= split&#40;' ', $_&#41;;
    $len=@line;
    $i=1;
    while &#40;$i < $len&#41; &#123;
      print "$line&#91;0&#93;\n";
      print OUT "$line&#91;0&#93;\t$line&#91;$i&#93;\n";
      $i++;
    &#125;
    That works correctly on my box with the test line you gave me. If you want, I can probably shorten a few more lines.

  6. #5
    Just Joined!
    Join Date
    Jan 2003
    Posts
    18
    That worked. Thank you very much.

    One more problem:

    The input file has ID numbers that come in the form 'x.x.x.x'

    Some of them are full like

    1.1.1.1
    1.1.1.21

    but some come like this:

    1.1.1.-
    1.-.-.-

    I need to disregard the IDs that are not full ( the ones with dashes instead of numbers ).

    How do I check for this? I've read about the /d flag, but how do I use it correctly?

    Thanks again

  7. #6
    Linux Engineer
    Join Date
    Jan 2003
    Location
    Lebanon, pa
    Posts
    994
    Revision #2
    Code:
    while&#40;<INFILE>&#41; &#123;
    chomp;
    if &#40;!/-/&#41; &#123;                                                                     
    &#40;@line&#41;= split&#40;' ', $_&#41;;
    $i=0;
    while&#40;$i < @line&#41; &#123;
      print "$line&#91;0&#93;\n";
      print OUT "$line&#91;0&#93;\t$line&#91;$i&#93;\n";
      $i++;
    &#125;
    &#125;                                                                     
    &#125;

  8. #7
    Just Joined!
    Join Date
    Jan 2003
    Posts
    18
    You are a life saver. Thank you.

    One more question and I'll leave you alone

    In the input file, some lines look like this:

    1.1.1.10 00040

    When I run the script, the second element in the array is ' 00040'. I need it to be '00040'.

    How do I fix this?

    Thanks again

  9. #8
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Try (@line) = split(' +', $_);

  10. #9
    Linux Engineer
    Join Date
    Jan 2003
    Location
    Lebanon, pa
    Posts
    994
    Revision #3. The foreach line should run through the @line array and remove any white space in an element. Though I am not sure how you were getting spaces since we were using ' ' to split up the line.

    Code:
    while&#40;<INFILE>&#41; &#123;
    chomp;
    if&#40;! /-/&#41; &#123;
    &#40;@line&#41;= split&#40;' ', $_&#41;;
    $i=1;
    foreach &#40;@line&#41; &#123;s/ //;&#125;
    while&#40;$i < @line&#41; &#123;
      print "$line&#91;0&#93;\n";
      print OUT "$line&#91;0&#93;\t$line&#91;$i&#93;\n";
      $i++;
    &#125;
    &#125;
    &#125;

  11. #10
    Just Joined!
    Join Date
    Jan 2003
    Posts
    18
    Hey guys

    Thanks a lot for all of your help. I've almost got it. The revision you sent ,genlee, that contained:

    foreach (@line) {s/ //};

    did not work so I still have that problem. I suppose that it means to replace all white space in a value with nothing. I don't know why it doesn't work, but I still get variables with white space like
    ' 00010'.

    I'm still working on it. If you have any other suggestions, please let me know.

    Thanks again

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •