Recently wrote a Perl script that implements the function of piecing together data from two columns in a table and then using the "|" Connected together.
The table reads as follows:
Employee number |
Employee Name |
Position |
Date of entry |
1001 |
Tom |
Sales |
1980/12/17 0:00:00 |
1002 |
John doe |
Financial |
1981/02/20 0:00:00 |
1003 |
Harry |
Manager |
1981/02/22 0:00:00 |
1004 |
As |
Accounting |
1981/04/02 0:00:00 |
Requirements are as follows:
Put the employee name and entry date together in the form of key-value pairs, and then use the pieced-together data with the "|" Connected together.
The results are as follows:
Zhang San: 1980/12/17 0:00:00| John Doe: 1981/02/20 0:00:00| Harry: 1981/02/22 0:00:00| Chen II: 1981/04/02 0:00:00
The script is as follows:
#!/usr/bin/perluse strict;my $line; while (<>) { s/\r\n//; if (/(\s+) \s+ (\s+) \s+ (\s+) \s+ (. *)/) { $line. =$2. ': '. $4. ' | '; }} print "$line \ n";
The results of the script execution are as follows:
Employee Name: entry date | Zhang San: 1980/12/17 0:00:00| John Doe: 1981/02/20 0:00:00| Harry: 1981/02/22 0:00:00| Chen II: 1981/04/02 0:00:00|
Basically meet the requirements.
Summarize:
1. \s matches white space characters in Perl. \s matches non-whitespace characters. Originally started with \w, but \w default matches are English characters, numbers and _. Does not match Chinese characters, so I use \s to represent it here.
2. This requirement is quite common in practice, and the common database is easy to make up for a few columns, but it is a challenge to put together the results once again, and Perl solves the problem well.
How to use Perl to cobble together data from different columns in a table and then use the "|" To connect together