in reply to Loop skipping

I tried to follow your code to figure out what was going on, but my head started hurting, so instead I'll offer a simpler way to do what I think you're trying to do (which I'm not really clear on, so I could be way off base). Assuming that you're trying to massage lines in this format:

YAL038W 1.1 2.4 4.1 YCL040W 1.1 1.6 1.8 9.11 0.0402128119838095

Into a structure like so:

%hash = ('YAL038W' => [1.1, 2.4, 4.1], 'YCL040W' => [1.1, 1.6, 1.8, 9.11, 0.0402128119838095], ...);

then I would do something like so with each line:

my $cur_identifier = ''; chomp $line; foreach my $element (split /\s+/, $line) { if ($element !~ /\d+(?:\.\d+)?/) { # matches int or float numbers $cur_identifier = $element; } else { push @{$hash{$cur_identifier}}, $element; } }

This splits the line on whitespace, then loops through the resulting list. Whenever it encounters an element that doesn't look like a number, it considers it the start of a new 'identifier' (hash key), and subsequent numbers are pushed onto the array referenced by that key, until the next identifier is reached.

-b

Replies are listed 'Best First'.
Re^2: Loop skipping
by ringleader (Acolyte) on Aug 11, 2004 at 15:48 UTC
    Incredibly sorry about the head-hurtingness, yet appreciative of the help :D
    Guess when i stare at my code all day, i don't realise that it makes sense only to me (that, and being primarily a biologist, doesn't help).

    I have many files with 10's of thousands of lines like these:
    YAL038W 1.1 2.4 4.1 YCL040W 1.1 1.6 1.8 9.11 0.0402128119838095 YDR132C 99 YDR223W 99 0.0085523710563531 YDL188C 01.05.04 10.03.01 40.01 42.0 43.01.03.05 YGL134W 01.05.04 02.1 +9 -0.0831302979427955

    I have these read into a 2-D array already.
    Now, with the code I posted, i am trying to see if, for each value of the identifier on the left side of the line, it contains a value that starts with a number from the list i have stored in an array ($pathway_name: these are simple integers like 1, 2, 3).
    That's where the awful while loop thingy came from. My code only has to work, not be pretty ;)
    The %seen hash refers to whether the identifier has already been seen on the iteration for this pathway value. I'm beginning to think that may be causing the problem, although there are no error messages displayed.

    My problem is, that when it comes to going on to the next $m in the outer for loop, it skips every second one. It will enter the for loop, but not the if/elsif/else statements.

    Is this any clearer?
      It would help if you showed us what output you are getting, and what output you are WANTING to get, for the given sample data you have shown us.

      Then we can write your application for you! :) Or at least show you where yours isn't working quite right.

        Ah, indeed. That would have been the smart thing.
        Righto.

        For the data given:
        YAL038W 1.1 2.4 4.1 YCL040W 1.1 1.6 1.8 9.11 0.040212811 YDR132C 99 YDR223W 99 0.0085523710 YDL188C 1.5.4 10.3.1 13.32 YGL134W 01.05.04 02.19 -0.083130297
        and the variables:
        @pathway_name = (1, 2, 4, 99); # looking for values starting with these numbers

        Output:
        <b>To command line</b> Pathway is 1 Gene: YAL038W, Value: 1 Gene: YDL188C, Value: 1 Pathway is 2 Pathway is 4 Gene:YAL038W, Value: 4.1 Pathway is 99 Done. <b>And in the file OUTPUT:</b> YAL038W 1 YDL188C 1 YAL038W 4

        It completely bypasses the 2 and 99 ones, but i can't see why.