简体   繁体   中英

perl, how do I use an array without assigning it to a variable first?

Here's what I currently do in my program

@array = split /\n/, $longstring;
$data = $array[14];

I really only want to get the element at position 14 out of the array and use that, the other stuff in the string is not useful to me. I know that in a language like java I could do something like this

$data = (split /\n/, $longstring)[14];

which is what I want to do but in perl.

So how do I access array elements without having to assign the array to a variable first?

edit: hmm, ok what about this

the long way

my $data = "abc\nd^e^f\nghi";
my @a = split (/\^/, (split /\n/, $data)[1]);
print $a[2];
__OUTPUT__
f

the short way

my $data = "abc\nd^e^f\nghi";
my $a = split (/\^/, (split /\n/, $data)[1])[2]; # line 60
print $a;
__OUTPUT__
syntax error at script.pl line 60, near ")["
Execution of script.pl aborted due to compilation errors.

this confuses me more than usual since it works on the inner split, but not the outer split

edit 2:

I'm a little confused as to why these two lines are different

my $a = (split  /\^/, (split /\n/, $data)[1])[2]; # works
my $a =  split (/\^/, (split /\n/, $data)[1])[2]; # doesnt

here's my thought process for the 2nd line which is what I wrote originally (in other words, this is what I think my program is doing)

my $data = "abc\nd^e^f\nghi";
my $a =  split (/\^/, (split /\n/, $data)[1])[2];
my $a =  split (/\^/, ("abc", "d^e^f", "ghi")[1])[2];
my $a =  split (/\^/, "d^e^f")[2];
my $a =  ("d", "e", "f")[2];
my $a =  "f";

That's what I expect to happen, can someone point out where my thinking has gone awry?

I'm just going to explain why these lines are different:

my $r = (split  /\^/, (split /\n/, $data)[1])[2]; # works
my $r =  split (/\^/, (split /\n/, $data)[1])[2]; # syntax error
my $r = (split (/\^/, (split /\n/, $data)[1]))[2]; # but this works

In Perl, you can use an array subscript like [2] on a list in parentheses (it's called a list slice ). But there's another rule that says "If it looks like a function call, it's a function call." That is, when you have the name of a function (like split ) followed by optional whitespace and an open parenthesis, it's a function call. You can't subscript a function call; you'd need to add an extra set of parentheses around it. That's why my third line works.

On another note, you should never say my $a or my $b . $a and $b are special package variables that are used with sort , and if you've turned them into lexicals, you'll have weird problems. Even if you're not using sort at the moment, you might add it later. It's easiest to just completely avoid ever making $a or $b lexical.

For readability, I might adjust the whitespace a bit and add a comment:

my $r = (split /\^/, (split /\n/, $data)[1] )[2]; # get 3rd field of 2nd line

您刚刚将第一个(放错了位置,请尝试以下操作:

my $a = (split/\^/, (split /\n/, $data)[1])[2];

What you've written is fine.

Here's a demo at codepad .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM