SlideShare ist ein Scribd-Unternehmen logo
1 von 5
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Andere mochten auch

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
 

Andere mochten auch (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
 

Kürzlich hochgeladen

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Kürzlich hochgeladen (20)

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
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
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
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
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
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
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
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
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.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
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 

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