Search code examples
perl

What is the best way to ignore elements in a list assignment?


I am using a list assignment to assign tab-separated values to different variables, like so:

perl -E '(my $first, my $second, my $third) = split(/\t/, qq[a\tb\tc]); say $first; say $second; say $third;'
a
b
c

To ignore a value, I can assign it to a dummy variable:

perl -E '(my $first, my $dummy, my $third) = split(/\t/, qq[a\tb\tc]); say $first; say $third;'
a
c

I don't like having unused variables. Is there another way to do it?


Solution

  • You can use undef:

    use warnings;
    use strict;
    use feature 'say';
    
    (my $first, undef, my $third) = split(/\t/, qq[a\tb\tc]);
    say $first; 
    say $third;
    

    Output:

    a
    c