Advertisement
ExaGridDba

lists of quoted items in perl

Aug 2nd, 2016
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. $ cat inlist.pl
  2. #!/usr/bin/perl
  3.  
  4. use strict;
  5. use warnings;
  6. use IO::File;
  7. use Data::Dumper;
  8.  
  9. my ( $filename ) = @ARGV;
  10.  
  11. defined $filename
  12. or die "usage: inlist.pl listfile";
  13.  
  14. my $fh = IO::File->new( $filename )
  15. or die "can't open $filename: $!";
  16.  
  17. local $/;
  18. undef $/;
  19.  
  20. my @items = split ' ', <$fh>;
  21.  
  22. warn "Genenerate in-list from perl array.\n";
  23. my @quotitems = map { qq/'$_'/ } @items;
  24. printf "in (\n\t%s\n)\n", join ",\n\t", @quotitems;
  25.  
  26. warn "Generate insert values from perl array with quoted identifiers and serially numbered bind variables.\n";
  27. my @dquotitems = map { qq/"$_"/ } @items;
  28.  
  29. my $bindex = 1;
  30. my @binds = map { sprintf ":b%s", $bindex++ } @items;
  31. printf "( %s ) values ( %s )\n", ( join ", ", @dquotitems ), ( join ", ", @binds );
  32. $ ./inlist.pl itemlist.txt
  33. Genenerate in-list from perl array.
  34. in (
  35. 'Sherlock',
  36. 'Watson',
  37. 'Moriarty',
  38. 'Moran'
  39. )
  40. Generate insert values from perl array with quoted identifiers and serially numbered bind variables.
  41. ( "Sherlock", "Watson", "Moriarty", "Moran" ) values ( :b1, :b2, :b3, :b4 )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement