#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Another newbie question :/


    Hi! I am new to perl/programming and I have a question which I suspect is stupid. But I have been stuck here for quiet a few hours now and this is getting tiresome, so some help would be greatly appreciated!

    So I am having a problem with an else function. The code is not finished yet but it is supposed to separate different Gene sequences from each other, and because I cannot get the else funtion to work I cannot complete it. It is only a syntax error but I dont understand what it is..

    When I remove the else function the program runs without error and if add the else code in another program it also works fine. So presume it is something with the code in this context.

    I get syntax error at line 26 and 33, marked them with arrows in the code.

    The code looks as following.

    use strict;
    use warnings;

    if(!(open "SEQ", "sequnces4.txt")){
    die("could not open file");
    }

    my @lines = <SEQ>;
    my @storage;
    my @storage2;

    my @matrix;
    foreach my $line(@lines){
    my @columns = split(/(<gi)/, $line);
    push(@matrix,\@columns);
    }

    for(my $row = 0; $row < scalar(@matrix); $row++){
    if($matrix[$row][0] =~ /(>gi)/){
    push(@storage,$matrix[$row][0]);
    for(my $row = 0; $row < scalar(@matrix); $row++){
    if($matrix[$row][0] =~ /^[ATGC\s]+$/){
    push(@storage,$matrix[$row][0]);
    else{(@storage2=join('',@storage)); <-- syntax close to "else"
    }
    }
    }
    }
    } <-- syntax error
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    832
    Rep Power
    496
    It would be easier if you had put code tags around your program, so as to preserve indent and other formatting.

    The problem is probably that you are missing a closing curly brace at the end of the if statement, before the else statement.

    Try to change the end of your program as follows:

    Code:
    # ...
    f($matrix[$row][0] =~ /^[ATGC\s]+$/){
    push(@storage,$matrix[$row][0]);
    }
    else{(@storage2=join('',@storage)); 
    }
    }
    }
    }
    (i.e. add a '}' before the else and remove one '}' below below the same else.

    I would add that the content of this else statement does not make very much sense to me.

    Code:
    @storage2=join('',@storage)
    Joining the elements of the @storage array into another array does not seem to make sense, join is returning a string.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
    Originally Posted by ParadoxFox
    <-- syntax error
    I quickly threw your code in an editor and it looks like you have bracket out of place, try this:
    Code:
    my @lines = <SEQ>;
    my @storage;
    my @storage2;
    
    my @matrix;
    foreach my $line( @lines ) {
     my @columns = split( /(<gi)/, $line );
    push( @matrix, \@columns );
    }
    
    for( my $row = 0; $row < scalar(@matrix); $row++) {
      if ( $matrix[$row][0] =~ /( >gi )/ ) {
        push( @storage, $matrix[$row][0] );
        for( my $row = 0; $row < scalar( @matrix ); $row++ ) {
          if ( $matrix[$row][0] =~ /^[ATGC\s]+$/ ) {
            push( @storage, $matrix[$row][0] );
          } else { ( @storage2 = join( '', @storage ) ); <-- syntax close to "else"
          }
        }
      }
    }
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0
    Wow, it worked! Thank you very much!

    I suspected it was something like that. I tried to move it around but never got it work. Seems like I need to look at the placement of curly brackets a little more careful.

    About the join function, I do think it is doing what I want it to do. The code is as I said not complete yet. The idea is to make each DNA sequence into one string and then store that string as one element in an array that will contain all sequences. The idea is that I can easily search each sequence individually that way and I also imagine that it would remove the problems of sequences being split between lines. First I searched each line but then I realized a pattern like AT\n CG
    wouldŽnt be found by the search pattern "ATCG".

    I hope that it works :/

    So far I am very intuitive when I program, just try to think about how it could be done from a logic point of view without actually knowing the exact limitations of the functions and so on. I learn everything on my own also, never talked with anyone about perl programming, which makes things complicated sometimes..

    Anyway, thanks for the help, I might pop in again

IMN logo majestic logo threadwatch logo seochat tools logo