SlideShare a Scribd company logo
1 of 5
Download to read offline
Converting a text file into a CSV file
  with an additional column/field
 The first 11 lines of source text file
                     look like this -->   1990
                                          41502   Year


                                          1991
 The objective is to
 transform the file into a flat
                                          41820
 database file containing                           Village
 the following columns:                   1992      Count
 Year, Count, and                         41876
 Variance. Variance is the
 difference between the                   1993
 count in a row and that in
 the previous row.                        41931
Perl Source Code
#!/usr/bin/perl

use strict;

my $year;
my $brgy_count_1 = 0;
my $brgy_count_2 = 0;
my $counter = 1;
my $first_line = 1;

open SOURCE_FILE, quot;/path/to/source_filequot;;
open TARGET_FILE, quot;>/path/to/target_filequot;;
Perl Source Code (2)
while (<SOURCE_FILE>) {
  next if ($_ =~ m/^$/);
  if ($counter == 1 or $counter == 3) {
      $_ =~ m/(d{4})/;
      print TARGET_FILE $1 . quot;tquot;;
      $counter += 1;
  } else {
      $_ =~ m/(d+)$/;
      if ($first_line == 0) {
          $brgy_count_2 = $1;
      }
      print TARGET_FILE $1 . quot;tquot;.
      ($brgy_count_2 - $brgy_count_1) . quot;nquot;;
Perl Source Code (3)
        if ($first_line == 1) { $brgy_count_1 = $1;
        } else { $brgy_count_1 = $brgy_count_2; }
        if ($counter == 4) {
            $counter = 1;
        } else {
            $counter += 1;
        }
        $first_line = 0;
    }
}

close SOURCE_FILE;
close TARGET_FILE;
The new flat database file
The first 10 lines of output file
                look like this -->   1990   41502     0
                                     1991   41820     318
                                     1992   41876     56
The output file uses a
                                     1993   41931     55
tab as field/column                  1994   41919     -12
separator. To use a                  1995   41929     10
comma, just go to the                1996   41935     6
related code's line                  1997   41939     4
and change the                       1998   41940     1
separator from “t” to
“,”.                                 1999   41940     0

                                            Village
                              Year                      Variance
                                            Count

More Related Content

Viewers also liked

Historical perspective of the philippine educational system lee ann
Historical perspective of the philippine educational system lee annHistorical perspective of the philippine educational system lee ann
Historical perspective of the philippine educational system lee ann
Jerson Panopio
 
Philippine education presentation
Philippine education presentationPhilippine education presentation
Philippine education presentation
Carlo Magno
 
DepEd, CHED and TESDA
DepEd, CHED and TESDADepEd, CHED and TESDA
DepEd, CHED and TESDA
rajnulada
 
K 12 basic education program
K 12 basic education programK 12 basic education program
K 12 basic education program
19710802
 

Viewers also liked (15)

Educational System in the Philippines, Quality Education and Access to Education
Educational System in the Philippines, Quality Education and Access to EducationEducational System in the Philippines, Quality Education and Access to Education
Educational System in the Philippines, Quality Education and Access to Education
 
Historical perspective of the philippine educational system lee ann
Historical perspective of the philippine educational system lee annHistorical perspective of the philippine educational system lee ann
Historical perspective of the philippine educational system lee ann
 
K to 12 electrical teacher's guide
K to 12 electrical teacher's guideK to 12 electrical teacher's guide
K to 12 electrical teacher's guide
 
The Organizational Structure of the Philippine Educational System
The Organizational Structure of the Philippine Educational SystemThe Organizational Structure of the Philippine Educational System
The Organizational Structure of the Philippine Educational System
 
Philippine education presentation
Philippine education presentationPhilippine education presentation
Philippine education presentation
 
K to 12 bread and pastry teacher's guide
K to 12 bread and pastry teacher's guideK to 12 bread and pastry teacher's guide
K to 12 bread and pastry teacher's guide
 
Problems and Issues in the Philippine Educational System
Problems and Issues in the Philippine Educational SystemProblems and Issues in the Philippine Educational System
Problems and Issues in the Philippine Educational System
 
Education System of the Philippines
Education System of the PhilippinesEducation System of the Philippines
Education System of the Philippines
 
Deped K12
Deped K12Deped K12
Deped K12
 
DepEd, CHED and TESDA
DepEd, CHED and TESDADepEd, CHED and TESDA
DepEd, CHED and TESDA
 
K to 12 General Presentation
K to 12 General PresentationK to 12 General Presentation
K to 12 General Presentation
 
K to 12 Science Curriculum Guide
K to 12  Science Curriculum GuideK to 12  Science Curriculum Guide
K to 12 Science Curriculum Guide
 
K to 12 classroom assessment ppt
K to 12 classroom assessment pptK to 12 classroom assessment ppt
K to 12 classroom assessment ppt
 
Historical foundation of philippine education
Historical foundation of philippine education Historical foundation of philippine education
Historical foundation of philippine education
 
K 12 basic education program
K 12 basic education programK 12 basic education program
K 12 basic education program
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

Converting a Text File to Flat Database File

  • 1. Converting a text file into a CSV file with an additional column/field The first 11 lines of source text file look like this --> 1990 41502 Year 1991 The objective is to transform the file into a flat 41820 database file containing Village the following columns: 1992 Count Year, Count, and 41876 Variance. Variance is the difference between the 1993 count in a row and that in the previous row. 41931
  • 2. Perl Source Code #!/usr/bin/perl use strict; my $year; my $brgy_count_1 = 0; my $brgy_count_2 = 0; my $counter = 1; my $first_line = 1; open SOURCE_FILE, quot;/path/to/source_filequot;; open TARGET_FILE, quot;>/path/to/target_filequot;;
  • 3. Perl Source Code (2) while (<SOURCE_FILE>) { next if ($_ =~ m/^$/); if ($counter == 1 or $counter == 3) { $_ =~ m/(d{4})/; print TARGET_FILE $1 . quot;tquot;; $counter += 1; } else { $_ =~ m/(d+)$/; if ($first_line == 0) { $brgy_count_2 = $1; } print TARGET_FILE $1 . quot;tquot;. ($brgy_count_2 - $brgy_count_1) . quot;nquot;;
  • 4. Perl Source Code (3) if ($first_line == 1) { $brgy_count_1 = $1; } else { $brgy_count_1 = $brgy_count_2; } if ($counter == 4) { $counter = 1; } else { $counter += 1; } $first_line = 0; } } close SOURCE_FILE; close TARGET_FILE;
  • 5. The new flat database file The first 10 lines of output file look like this --> 1990 41502 0 1991 41820 318 1992 41876 56 The output file uses a 1993 41931 55 tab as field/column 1994 41919 -12 separator. To use a 1995 41929 10 comma, just go to the 1996 41935 6 related code's line 1997 41939 4 and change the 1998 41940 1 separator from “t” to “,”. 1999 41940 0 Village Year Variance Count