CY's Take on PWC#110

If you want to challenge yourself on programming, especially on Perl and/or Raku, go to https://perlweeklychallenge.org, code the latest challenges, submit codes on-time (by GitHub or email).

Do tell me, if I am wrong or you strongly oppose my statements!

Well, I wake up early and get some time to blog about The Weekly Challenge again (in addition, correct some parts of my submitted code ‐ a brain with good rest well spots bugs!).

Both Perl and (guest language) Java codes for the two tasks have been done this week.

Task 1: Valid Phone Number

I started my Sunday afternoon with Java. "Eat the frog first."
There are several ways to read text files in Java, I chose the default class which I am most familiar with: java.util.Scanner.
File file = new File(args[0]);
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
    // blah blah blah
}
Before I started, I peeked on the Guest Contribution page and briefly looked what packages the regular Java contributor Mr Zia use, and intentionally avoided repetition. Oh, Mr Zia used Java Regex in Task 1 and some usual packages for Task 2. Though not every programming language works with the TIMTOWTDI motto, for Task 1 we can have different approaches.
Actually I am not familiar Java Regex yet. I divided the verification process into three parts:
  1. Check the first 4 characters;
  2. check the character in between regional calling codes and the latter codes is an empty space character;
  3. finally check the last 10 characters are digits.
My Perl codes do with the same divisions, just have an additional taking care for the total length of the input line because I use substr($s, negative number) for the third checking, so the code would be unsure of the total length unless an additional check was inserted; and make some use of regular expression ^o^.

Perl codes:

my $s;
while ($s = <STDIN>) {
    chomp($s);
    if ( length $s == 15 &&
        substr($s, 4, 1eq " " && 
        substr($s-11) =~ / \d{10}/ &&
        (substr($s,0,4) =~ /\d{4}/ ||
         substr($s,0,4) =~ /\+\d{2}/ ||
         substr($s,0,4) =~ /\(\d{2}\)/)
    ) {
        print $s"\n";
    }
}

Selection of Java codes:
if ( checkHead(num) && checkMid(num) &&
     num.substring(5).trim().length() == 10 && checkTail(num))
    System.out.println(num);

// blah blah blah

private static boolean isNumeric(char ch)
{
    if (ch >= '0' && ch <= '9') return true;
    else return false;
}

It is also worth mentioned that Java requires attention for exception handling whenever the file I/O package is being used. From my limited exploration, the exception handling feature is closely linked with object-oriented programming. Exception handling in Perl requires a Perl hacker with expertise to discuss.

Task 2: Transpose File

After reading the task statement on Monday, immediately I thought of the Perl module for CSV file, hence I thought it would be a sincere task in Perl. From the search engine, it is recommended that the use of already-made external packages for CSV file in Java; but I thought it is okay to handle the special cases (for example, the delimitter comma is used in a field) by myself.


Some CSV files were made from LibreOffice for testing:
task2_str2.csv
"""",",",char
ok,pdl,int
cu,uml,str
task2_str4.csv
"""",okay,char
ok,pdl,int
cu,uml,str
task2_ex2.csv (when the number of fields in each row is not the same)
name,age,sex
Mohammad,45,m,n
Joe,20,m
Julie,35,f
Cristina,10,f
Expected Output name,Mohammad,Joe,Julie,Cristina age,45,20,35,10 sex,m,m,f,f ,n,,,
For Java, I made use of ArrayList<ArrayList<String>>. Omitting the details here.
For Perl, Text::CSV_XS did a good job. But I want quotation marks protecting the comma and handling cases like task2_ex2.csv, therefore a subroutine for printing is added:

sub print_item {
    my $item = $_[0];
    if (defined($item)) {
        print "\"" if $item  =~ /,/;
        print $item;
        print "\"" if $item  =~ /,/;
    }
}

Perl Script Output:
# the example from task statement
name,Mohammad,Joe,Julie,Cristina
age,45,20,35,10
sex,m,m,f,f

# task2_str2.csv
",ok,cu
",",pdl,uml
char,int,str

# task2_str4.csv
",ok,cu
okay,pdl,uml
char,int,str

# task2_ex2.csv
name,Mohammad,Joe,Julie,Cristina
age,45,20,35,10
sex,m,m,f,f
,n,,,
Differences from my Java compiled program for task2_str2.csv , task2_str4.csv:

# task2_str2.csv
"""",ok,cu
",",pdl,uml
char,int,str
# task2_str4.csv
"""",ok,cu
okay,pdl,uml
char,int,str

Oooops... it seems that the """" for the output is better. See RFC 4180 Section 2, point 7. ...


Stay alert and healthy! □

Leave a comment

About C.-Y. Fung

user-pic a beginner in programming; passionate in mathematics, distance running and gender issues; from Hong Kong. https://twitter.com/e7_87