#!/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:) | [reply] [d/l] |
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
| [reply] [d/l] |
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?
| [reply] |
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.
| [reply] [d/l] [select] |
[...]
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!,
print "profeth still\n" if /bird|devil/;
| [reply] [d/l] |
$_->[1] >= 15 && last or ++$beg for @data;
$_->[1] >= 15 && last or ++$end for reverse @data;
Perl reduces RSI - it saves typing
| [reply] [d/l] |
10 or 15...anything is fine. i can always change the parameters. it was just a sample program. :)
| [reply] |