s*******t 发帖数: 2896 | 1 请问这个在perl里有没有简单写法?
$d="";
foreach (@years) {$d=$d." YEAR$_";} |
S**I 发帖数: 15689 | 2 $d = ' YEAR'.join(' YEAR',@years);
【在 s*******t 的大作中提到】 : 请问这个在perl里有没有简单写法? : $d=""; : foreach (@years) {$d=$d." YEAR$_";}
|
d****p 发帖数: 685 | 3
$d = map " YEAR$_", @years;
【在 s*******t 的大作中提到】 : 请问这个在perl里有没有简单写法? : $d=""; : foreach (@years) {$d=$d." YEAR$_";}
|
t****t 发帖数: 6806 | 4 i believe this is not what he wanted, you get the element count of @year
only.
【在 d****p 的大作中提到】 : : $d = map " YEAR$_", @years;
|
d****p 发帖数: 685 | 5 right. map returns a list. should be joined.
【在 t****t 的大作中提到】 : i believe this is not what he wanted, you get the element count of @year : only.
|
s*******t 发帖数: 2896 | 6 @years=qw(01 02 03);
print join " " , map "YEAR".$_ , @years;
That's cool. Thank you! |
s*******t 发帖数: 2896 | 7 又有个问题,就不开新贴了,免得斑竹说我刷屏。
是这样:我在regular expression匹配时用了/m,所以^和$代表行开始和行结束。我现
在还想匹配整个字符串结束,怎么办呢? |
t****t 发帖数: 6806 | 8 you may use \A and \Z and \z.
【在 s*******t 的大作中提到】 : 又有个问题,就不开新贴了,免得斑竹说我刷屏。 : 是这样:我在regular expression匹配时用了/m,所以^和$代表行开始和行结束。我现 : 在还想匹配整个字符串结束,怎么办呢?
|
g*****e 发帖数: 172 | 9 m//g
【在 s*******t 的大作中提到】 : 又有个问题,就不开新贴了,免得斑竹说我刷屏。 : 是这样:我在regular expression匹配时用了/m,所以^和$代表行开始和行结束。我现 : 在还想匹配整个字符串结束,怎么办呢?
|
s*******t 发帖数: 2896 | 10 perl能不能用list做index存取array?比如$arr[(1..3,4,8)]=5; |
|
|
t****t 发帖数: 6806 | 11 yes, you can. but use @ to indicate slices, and use repetition operation to
repeat RHS (this is not like matlab, where scalars are automatically
expanded to match vector/matrix size)
@arr[(1..3, 4, 8)] = (5) x 5;
【在 s*******t 的大作中提到】 : perl能不能用list做index存取array?比如$arr[(1..3,4,8)]=5;
|
s*******t 发帖数: 2896 | 12 那perl能不能对list做component-wise运算,比如
@a=(1..5);
@b=(2..6);
print @a*@b; # get 25
我想算(1*2,2*3,...,5*6),有省事的办法吗?或者用字符串运算也行。 |
t****t 发帖数: 6806 | 13 @a=map { $_*($_+1) } (1..5);
but this is not generic component-wise calculation.
you may, though, write
@c = map { $a[$_] * $b[$_] } (0..4);
but this is dumb.
BTW, if you write in perl, think in perl. don't think in matlab and write
perl. you can translate literally, but you will result in dumb code.
【在 s*******t 的大作中提到】 : 那perl能不能对list做component-wise运算,比如 : @a=(1..5); : @b=(2..6); : print @a*@b; # get 25 : 我想算(1*2,2*3,...,5*6),有省事的办法吗?或者用字符串运算也行。
|
s*******t 发帖数: 2896 | 14 多谢!准备先用这个
map {$a[$_]*$b[$_]} (0..@a-1)
要是0..@a-1能简写就好了。满脑袋都是R,seq(along=a),thinking不好改啊,跟问题
相关。 |
a***y 发帖数: 2803 | 15 print map { $a[$_] * $b[$_] } 0..$#a;
【在 s*******t 的大作中提到】 : 那perl能不能对list做component-wise运算,比如 : @a=(1..5); : @b=(2..6); : print @a*@b; # get 25 : 我想算(1*2,2*3,...,5*6),有省事的办法吗?或者用字符串运算也行。
|