我有一个变量,其中有两个值,用分号分隔。我希望将这些值中的一个放入一个变量中,将另一个放入另一个变量中。我试图这样做:
my $firstField = `echo $line | awk -F; '{print $1}'`;
但它的解释非常奇怪。我最好怎么做?
答案 0 :(得分:4)
这里绝对不需要涉及任何外部工具。使用perl!
Cursor
由于Perl的规则"If it looks like a function call, it's a function call",外括号是必需的。如果没有额外的括号,my $firstField = (split(/;/, $line))[0];
将被解释为函数调用的下标(无效),而不是作为返回列表的下标。
或者如果您愿意:
[0]
这会将my ($firstField) = split /;/, $line;
返回的列表的第一个值分配给变量。
答案 1 :(得分:0)
您可以通过一次拆分调用获得任意数量的内容: 如果你想在标量变量中使用它们,请使用
import { Component, View } from 'angular2/core'
import { CORE_DIRECTIVES } from "angular2/common"
import {SubjectService} from "../../services/subject/SubjectService.ts"
import 'rxjs/add/operator/map';
@Component({
selector: 'dashboard',
providers: [SubjectService]
})
@View({
template: `
<div class="row">
<div class="medium-12 columns">
<h1>Dashboard!</h1>
{{subjects}}
</div>
</div>
`
})
export class DashboardComponent{
constructor(subjectService: SubjectService){
subjectService.getSubject()
}
}
或者将变量编号放入数组中使用:
my ($a, $b) = split(";", $inputstring); # add $c, $d, etc as you like to the list