Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: chopping the beginning and end of lines in 2 files

by heidi (Sexton)
on Nov 13, 2008 at 06:55 UTC ( [id://723354]=note: print w/replies, xml ) Need Help??


in reply to Re: chopping the beginning and end of lines in 2 files
in thread chopping the beginning and end of lines in 2 files

#!/usr/bin/perl $b="a b c d e f g h i j"; $a="1 3 45 65 7 89 67 9 8 2"; @seq=split(/ /,$b); @arr=split(/ /,$a); $beg=0; $end=0; for($i=0;$i<scalar(@arr);$i++){ if ($arr[$i]<15){ $beg++; } else{ last; } } @arr_rev=reverse(@arr); for($i=0;$i<scalar(@arr_rev);$i++){ if ($arr_rev[$i]<15){ $end++; } else{ last; } } for ($i=0+$beg;$i<scalar(@arr)-$end;$i++){ print "$seq[$i] "; } print "\n"; for ($i=0+$beg;$i<scalar(@arr)-$end;$i++){ print "$arr[$i] "; }
this is a sample which i worked it... since there are more than one way to do in perl, i thought i wil ask you. Moreover, i sometimes feel that my codes dont look mature when compared to what you all write. want to learn the way how monks do it:)

Replies are listed 'Best First'.
Re^3: chopping the beginning and end of lines in 2 files
by GrandFather (Saint) on Nov 13, 2008 at 10:04 UTC

    Ah, much better! We don't care what your code looks like (actually, that's a lie) so long as you are prepared to take some advice and improve. This is lovely code - there is a ton of room for improvement. ;)

    First off, always use strictures (use strict; use warnings;). Don't use $a and $b as variables - they are special (used by sort). Avoid the C style for loop - Perl's for loop is safer, clearer and cleaner.

    Generally if you have parallel data try to use a single data structure for it so you don't have to double handle everything with the resultant duplication of code and much greater risk of errors.

    Bearing that in mind consider:

    use strict; use warnings; my $str1 = "a b c d e f g h i j"; my $str2 = "1 3 45 65 7 89 67 9 8 2"; my @seq = split (' ', $str1); my @arr = split (' ', $str2); my @data = map {[$seq[$_], $arr[$_]]} 0 .. $#seq; my $beg = 0; my $end = 0; for (@data) { last unless $_->[1] < 15; $beg++; } for (reverse @data) { last unless $_->[1] < 15; $end++; } print join (' ', map {$data[$_][0]} $beg .. $#arr - $end), "\n"; print join (' ', map {$data[$_][1]} $beg .. $#arr - $end), "\n";

    Perl reduces RSI - it saves typing
      ahhh...this is how i want to improve coding, using map, grep etc etc. since this a small program, its ok, otherwise the program should take less time to run, which will happen only when the code is tight and has less number of loops. am i right?

        Well, it's not so much using map or grep as some sort of magic bullet replacement for a for loop (they still effectively loop), but more a matter of choosing the right tool for the job. Perl lets you do the same thing in a multitude of ways, and generally none of them is "the one true way". The trick is to find the combination of tools that make the intent of the code clear and maintainable - often execution time is secondary because I/O is the limiting factor.

        The mantra is: make it work first, then make it clean, then make it fast (if you need to). "Clean" and "Work" are often related - it's much harder to debug messy code than it is to debug clean code. Consider how easy it is to be off by one in a messy C for loop compared with a clean Perl for loop for example.


        Perl reduces RSI - it saves typing
Re^3: chopping the beginning and end of lines in 2 files
by Crian (Curate) on Nov 13, 2008 at 10:14 UTC

    I hope a few general remarks are welcome.

    I would suggest you to use use strict; and use warnings;.

    And please don't use $a and $b, the are even with use strict; not under the strict conditions, because there are used by sort.


    Edit: I should renew the page before answering. Grandfather was faster and he did better.

Re^3: chopping the beginning and end of lines in 2 files
by ChOas (Curate) on Nov 13, 2008 at 08:46 UTC
    I kind of like this one:

    [...] for ($start=0;$arr[$start]<15;++$start) {}; for ($end=$#arr;$arr[$end]<15;--$end) {}; print "@seq[$start..$end]\n"; print "@arr[$start..$end]\n";

    Just don't understand if you were looking for a number smaller than 10 or 15 :)


    GreetZ!,
      ChOas

    print "profeth still\n" if /bird|devil/;

      then you might like:

      $_->[1] >= 15 && last or ++$beg for @data; $_->[1] >= 15 && last or ++$end for reverse @data;

      Perl reduces RSI - it saves typing
      10 or 15...anything is fine. i can always change the parameters. it was just a sample program. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://723354]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 13:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found