Skip to content

Instantly share code, notes, and snippets.

@craigiansmith
Created November 23, 2016 07:27
Show Gist options
  • Select an option

  • Save craigiansmith/74d487bf0f50b8abe97854f07850c7a5 to your computer and use it in GitHub Desktop.

Select an option

Save craigiansmith/74d487bf0f50b8abe97854f07850c7a5 to your computer and use it in GitHub Desktop.

Revisions

  1. @dashaxiong dashaxiong created this gist Nov 23, 2016.
    36 changes: 36 additions & 0 deletions toMysql.pl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #!/usr/bin/perl

    # Open file
    $file = $ARGV[0];
    open(FH, '<', $file);
    @raw_data = <FH>;
    close(FH);
    # Create array to store all jasper vars found
    @vars = ();
    # Search for Jasper vars like $P{word} or $P!{word}
    foreach $line (@raw_data) {
    if($line =~ /\$P\!?\{([^}]*)\}/) {
    push (@vars, lc $1);
    }
    }
    # Open file for writing to
    open(FH, '>', "$file.new");

    # Make sure array of variables only has distinct entries

    my %hash = map { $_, 1} @vars;
    my @unique = keys %hash;


    # Prepend set statements to beginning of file
    # such as "SET @word = '';" for each word

    foreach $var (@unique) {
    print FH "SET \@$var = '';\n";
    }

    # Substitute mysql vars in place of the Jasper vars i.e. @word
    foreach $line (@raw_data) {
    $line =~ s/\$P\!?\{([^}]*)\}/\@\L$1/g;
    print FH $line;
    }