Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

using perl how do i place all the alphabets in a single line after each plot? is 'while' loop better or 'for' loop?
#PLOT1 a b c d f g i a c g s h b h f d #PLOT2 b g d i s e w d u b a o n i o p w q c u s t r r y #PLOT3 e r t d v g v g b

Replies are listed 'Best First'.
Re: while or for?
by CountZero (Bishop) on Dec 30, 2008 at 06:06 UTC
    My take on a while based solution:
    use strict; print scalar <DATA>; # print first #PLOT1 while (<DATA>) { chomp; # get rid of EOL print /#PLOT/ ? "\n$_\n" : "$_ "; } ## end while (<DATA>) __DATA__ #PLOT1 a b c d f g i a c g s h b h f d #PLOT2 b g d i s e w d u b a o n i o p w q c u s t r r y #PLOT3 e r t d v g v g b
    Output:
    #PLOT1 a b c d f g i a c g s h b h f d #PLOT2 b g d i s e w d u b a o n i o p w q c u s t r r y #PLOT3 e r t d v g v g b
    If you don't know the number of iterations beforehand, use while, otherwise a for loop is more elegant.

    Whether it is better (for whatever definition of better) I do not know. My solution uses very little memory as it never holds more than one line in memory, whereas a foreach based solution will (usually) hold all the data in memory at once. Of course for such a small sample of data it does not matter.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: while or for?
by ww (Archbishop) on Dec 30, 2008 at 03:07 UTC

    This would be a better question if:

    1. You showed us what you want
    2. You showed us what you've tried, and where you're stuck

    For 1, either a sample of your desired output using the data supplied, or a clear(er) explanation of your intent, or, better yet, both.

    While, re 2, the Monks give generously of their time and wisdom to those who show effort, but tend to frown disapprovingly on those who see the Monastery as a 'free coding machine'

Re: while or for?
by Lawliet (Curate) on Dec 30, 2008 at 03:10 UTC

    I suggest using a mixture of a while loop, perhaps a join statement, and a dash of split. However, you have not really provided much information so my suggestion could be entirely wrong.

    And you didn't even know bears could type.

      the program i have written is
      open(FH,"input.txt") or die "cant open"; @array=<FH>; foreach(@array){ if($_!=~m/^>/) { $_=~s/\n//g; print "$_\n"; } }
      the desired result is
      #PLOT1 a b c d f g i a c g s h b h f d #PLOT2 b g d i s e w d u b a o n i o p w q c u s t r r y #PLOT3 e r t d v g v g b
Re: while or for?
by imrags (Monk) on Dec 30, 2008 at 05:55 UTC
    TIMTOWTDI
    One of the ways would be
    $count = 0; while (<FILE>) { if ($_ =~ /^\#/) { $count ++; $temp{$count} = ""; } else{ chomp (); $temp{$count} .= " ".$_; } } for $i (0 .. $count) { print $temp{$i} ."\n"; }
    Raghu
Re: while or for?
by runrig (Abbot) on Dec 30, 2008 at 05:40 UTC
    using perl how do i place all the alphabets in a single line after each plot? is 'while' loop better or 'for' loop?
    A while loop. Definitely a while loop. HTH.
Re: while or for?
by kyle (Abbot) on Dec 30, 2008 at 03:01 UTC

    What code do you have so far? Is that your input or the desired output?

      the program i have written is open(FH,"input.txt") or die "cant open"; @array=<FH>; foreach(@array){ if($_!=~m/^>/) { $_=~s/\n//g; print "$_\n"; } } the desired result is #PLOT1 a b c d f g i a c g s h b h f d #PLOT2 b g d i s e w d u b a o n i o p w q c u s t r r y #PLOT3 e r t d v g v g b
        if($_!=~m/^>/)
        I don't see any ">" characters in your input. What do you expect the line above to do (also, the "not matches" operator is "!~", not "!=~")?