Learning Perl 7th Edition assistance. Make a program that reads a list of string
ID: 3849052 • Letter: L
Question
Learning Perl 7th Edition assistance.
Make a program that reads a list of strings from a file, one string per line, and then lets the user interactively enter patterns that may match some of the strings. For each pattern, the program should tell how many strings from the file matched, then which ones those were. Don’t reread the file for each new pattern; keep the strings in memory. The filename may be hardcoded in the file. If a pattern is invalid (for example, if it has unmatched parentheses), the program should simply report that error and let the user continue trying patterns. When the user enters a blank line instead of a pattern, the program should quit. (If you need a file full of interesting strings to try matching, try the file sample_text in the files you’ve surely downloaded by now from the O’Reilly website; see Chapter 1.
Write a program to make a report of the access and modification times (in the epoch time) of the files in the current directory. Use stat to get the times, using a list slice to extract the elements. Report your results in three columns, like this:
fred.txt 1294145029 1290880566
barney.txt 1294197219 1290810036
betty.txt 1287707076 1274433310
Modify your answer to Exercise 2 to report the times using the YYYY-MM-DD format. Use a map with localtime and a slice to turn the epoch times into the date strings that you need. Note the localtime documentation about the year and month values it returns. Your report should look like this:
fred.txt 2011-10-15 2011-09-28
barney.txt 2011-10-13 2011-08-11
betty.txt 2011-10-15 2010-07-24
Explanation / Answer
package sorter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import javax.swing.JOptionPane; public class SortingAlgorithm { private static final String CREATE_DB = "CREATE DATABASE sorting"; private static final String DROP_DB = "DROP DATABASE sorting"; private static final String CREATE_TABLE = "CREATE TABLE sorting.sorting ( num double not null )"; public static void main(String[] args) throws Exception { Class.forName("com.mysql.jdbc.Driver"); List doubles = new ArrayList(50); String typed; do { typed = JOptionPane.showInputDialog(null, "Type a double:"); if (typed != null) doubles.add(Double.parseDouble(typed)); } while (typed != null); List sorted = new ArrayList(50); try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306", "root", "root")) { try (PreparedStatement ps = con.prepareStatement(CREATE_DB)) { ps.executeUpdate(); } try (PreparedStatement ps = con.prepareStatement(CREATE_TABLE)) { ps.executeUpdate(); } for (Double d : doubles) { try (PreparedStatement ps = con.prepareStatement("INSERT INTO sorting.sorting (num) VALUES (" + d + ")")) { ps.executeUpdate(); } } try ( PreparedStatement ps = con.prepareStatement("SELECT * FROM sorting.sorting ORDER BY num"); ResultSet rs = ps.executeQuery()) { while (rs.next()) { sorted.add(rs.getDouble("num")); } } try (PreparedStatement ps = con.prepareStatement(DROP_DB)) { ps.executeUpdate(); } } JOptionPane.showMessageDialog(null, "The array sorted is: " + sorted); } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.