Perl assignment with a dummy placeholder

后端 未结 4 2253
北恋
北恋 2020-12-18 17:51

In other languages I\'ve used like Erlang and Python, if I am splitting a string and don\'t care about one of the fields, I can use an underscore placeholder. I tried this

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-18 18:36

    You can assign to (undef).

    (undef, my $id) = split(/=/, $fields[1]);
    

    You can even use my (undef).

    my (undef, $id) = split(/=/, $fields[1]);
    

    You could also use a list slice.

    my $id = ( split(/=/, $fields[1]) )[1];
    

提交回复
热议问题