Answer: how do i find the index of a specific array value? contributed by merlyn my @array = qw( your array here ); my $search_for = "here"; my( $index )= grep { $array eq $search_for } 0..$#array; Answer: how do i find the index of a specific array value? contributed by hdp For very large arrays where bailing out as soon as a match is found is a win: my @a = ( 1 .. 1_000_000 ); # some large array my $want = 5843; my $index = 0; ++$index until $a == $want or $index $#a; Answer: how do i find the index of a specific array value? contributed by I0 You could use an index hash: my @array = qw( your array here ); my $search = "array"; my %index; @index{@array} = (0..$#array); my $index = $index{$search}; print $index, "\n"; This is a win, for larger arrays, if you need to do multiple/many lookups while the array remains static. Answer: How do I find the index of a specific array value? contributed by snoopy I like List::MoreUtils : use List::MoreUtils; my @array = qw( Apples Oranges Brains Toes Kiwi); my $search = "Toes"; my $index = List::MoreUtils::first_index {$_ eq $search} @array; print "index of $search = $index\n"; Answer: How do I find the index of a specific array value? contributed by marcussen Using merlyn's example with regular expressions, so you don't need to know the exact value of the element you are matching; my @array = ( 'Name: Mr. Jones', 'Phone: 555-555', 'Email: jones@example.com' ); my ( $index )= grep { $array =~ /Phone/ } 0..$#array; Replace ( $index ) with an array to match multiple instances. Answer: how do i find the index of a specific array value? contributed by MeowChow You could use my aindex and raindex . Answer: How do I find the index of a specific array value? contributed by simul If the array is large and sorted, you might want search it more efficiently: my @array = qw( your large, sorted array here ); my $search = "thing"; my $index = bsearch(\@array, $search); sub bsearch { my ($array, $word) = @_; my $low = 0; my $high = @$array - 1; while ( $low = $high ) { my $try = int( ($low+$high) / 2 ); $low = $try+1, next if $array- lt $word; $high = $try-1, next if $array- gt $word; return $try; } return; } Answer: How do I find the index of a specific array value? contributed by myuserid7 You should usefirst()from List::Util . It is a core module, unlike List::MoreUtils and other modules mentioned above, so it is portable and can be used with no extra effort. use List::Util qw(first); my @array = qw( Apples Oranges Brains Toes Kiwi); my $search = "Toes"; my $index = first { $array eq $search } 0 .. $#array; print "index of $search = $index\n";
use warnings; use strict; open FH,"test.txt"; open RESULT,"result.txt"; open LINE,"line.txt"; open LEN, "length.txt"; while (FH) { #chomp; print RESULT "line $. is $_"; print LINE "line $.\n"; my $leng=length$_; print LEN "The length of line $. is $leng \n"; } close FH; test.txt as follow 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3
Perl's a great language for special variables - variables that are set up without the programmer having to intervene and providing information ranging from the number of lines read from the current input file ($.) through the current process ID ($$) and the operating system ($^O). Other special variables effect how certain operations are performed ($| controlling output buffering / flushing, for example), or are fundamental in the operation of certain facilities - no more so than $_ and @_. ------------------------------------------- 1.@_含义 1)是perl中默认的数组变量 比如说你想移除数组中的一个元素赋值给一个变$value 方法1:你可以定义某个数组如@abcd my $value=shift @abcd; 方法2:你没有定义任何数组 my $value=shift @_; 和上例等效 这里perl会隐式的选择@_ 2)是sub子函数中的默认参数列表. 例如: sub funct($$) { ($param1, $param2) = @_; #Statement } 再例如,有下面一段代码: my $max_number = max(1,2); print "1 and 2 ,the max number is $max_number\n"; sub max{ my ($num1,$num2) = @_ ; ## 取出参数列表中的元素。 ........此处省略求max运算 } 在子函数中直接shift; 就可以从@_的前端弹出一个元素. shift; 等于 shift @_; ------------------------- @_ is the list of incoming parameters to a sub. So if you write a sub, you refer to the first parameter in it as $_ , the second parameter as $_ and so on. And you can refer to $#_ as the index number of the last parameter: sub demo { print "Called with ",$#_+1," params\n"; print "First param was $_ \n"; Note that the English module adds in the ability to refer to the special variables by other longer, but easier to remember, names such as @ARG for @_ and $PID for $$. But use English; can have a detrimental performance effect if you're matching regular expressions against long incoming strings 2.$_含义 1)$_为默认列表变量。在一个命令没有任何参数的时候,表示它从默认变量里读取。 例如: print; 等于 print $_; 2)默认模式匹配空间( pattern matching space ) s/.../.../; 等于 $_ =~ s/.../.../; --------------------------- Then any regular expression matches, chop s (and lc s and many more) without a parameter, and even print s assume you want to work on $_. Thus: while ($line = FH) { if ($line =~ /Perl/) { print FHO $line; } print uc $line; } Shortens to: while (FH) { /Perl/ and print FHO ; print uc; } 3.$1,$2,...等含义 以数字为名的变量保存的是上一次匹配操作(/pattern/)中,第n个小括号中的原符号所匹配内容。 $1就是第一对小括号中的原符号所对应的匹配内容。 $2就是第二对小括号中的原符号所对应的匹配内容。 内插功能: $str = "aaa4zzz7bbb"; $str =~ /(\d)z{3}(\d)/; print "$1\t$2\n"; 输出结果是:4 7
今天本来打算查R的循环语句,无意间碰到了一个函数,合并两个 数据框,能够替代perl里面的一段程序,用于按照ID列表提取数据。 x-data.frame(a=c(a,b,c,c)) x a 1 a 2 b 3 c 4 c y-data.frame(a=c(a,b,c), b=c(10,20,30)) y a b 1 a 10 2 b 20 3 c 30 merge(x,y) a b 1 a 10 2 b 20 3 c 30 4 c 30 ?merge Merge two data frames by common columns or row names, or do other versions of database join operations. 相应的perl程序: open(DATABASE,$ARGV ); open(PICK,$ARGV ); open(PICKDATA,$ARGV ); while (defined($in_line = DATABASE)) { chomp $in_line; @tokens = split /\t/,$in_line; $tokens_ID = $tokens ; $hash{$tokens_ID}=$in_line; } while (defined($in_line = PICK)) { chomp $in_line; @Seq_tokens = split /\t/,$in_line; $Seq_tokens_ID = $Seq_tokens ; print PICKDATA $hash{$Seq_tokens_ID}\n; } close DATABASE; close PICK; close PICKDATA;