mysql - Perl: Breaking out of foreach loop when last array element is encountered -


Pearl Nob here. I have a small script (see below) that I am using to create a MySQL INSERT statement.

  Use strict; My @records = qw / record1 record2 / 3 records; My $ insert = "include in table prices"; Enter $ $ before my $ record (@ Records). {$ Insert. = "('". $ Record. "'), \ N"; } Print "$ insert \ n"; Enter  

current output

  table values ​​('Record 1'), ('Record 2'), ('Record 3'), < / Code> <
 

I should know how to break the last element of the @records array and instead of attach ,

include the desired output

  in the table values ​​('Record 1'), ('Record 2'), ('Record 3');    

You can do this map and < Join / Code>

  my @records = qw / Record1 Record2 Record3 /; My $ insert = "include in table prices"; $ Insert = Join ',', map {'('. $ Dbh-> quotation ($ _). ')'} @records; $ Insert = ';'; # This line is not required  

The stuff of $ dbh is better than just quoting because it handles bad things for you Map is not very different from a foreach loop, and joining will take care of putting a comma in between the elements, not the previous one.


Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -