Easy way to print Perl array? (with a little formatting)

后端 未结 11 709
猫巷女王i
猫巷女王i 2021-01-30 10:13

Is there an easy way to print out a Perl array with commas in between each element?

Writing a for loop to do it is pretty easy but not quite elegant....

11条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-30 10:22

    Using Data::Dumper :

    use strict;
    use Data::Dumper;
    
    my $GRANTstr = 'SELECT, INSERT, UPDATE, DELETE, LOCK TABLES, EXECUTE, TRIGGER';
    $GRANTstr    =~ s/, /,/g;
    my @GRANTs   = split /,/ , $GRANTstr;
    
    print Dumper(@GRANTs) . "===\n\n";
    
    print Dumper(\@GRANTs) . "===\n\n";
    
    print Data::Dumper->Dump([\@GRANTs], [qw(GRANTs)]);
    

    Generates three different output styles:

    $VAR1 = 'SELECT';
    $VAR2 = 'INSERT';
    $VAR3 = 'UPDATE';
    $VAR4 = 'DELETE';
    $VAR5 = 'LOCK TABLES';
    $VAR6 = 'EXECUTE';
    $VAR7 = 'TRIGGER';
    ===
    
    $VAR1 = [
              'SELECT',
              'INSERT',
              'UPDATE',
              'DELETE',
              'LOCK TABLES',
              'EXECUTE',
              'TRIGGER'
            ];
    ===
    
    $GRANTs = [
                'SELECT',
                'INSERT',
                'UPDATE',
                'DELETE',
                'LOCK TABLES',
                'EXECUTE',
                'TRIGGER'
              ];
    

提交回复
热议问题