SlideShare una empresa de Scribd logo
1 de 5
Descargar para leer sin conexión
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

Más contenido relacionado

Destacado

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 EducationRose Ann Enriquez
 
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 annJerson Panopio
 
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 guideNoel Tan
 
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 SystemGlance Ruiz
 
Philippine education presentation
Philippine education presentationPhilippine education presentation
Philippine education presentationCarlo Magno
 
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 guideNoel Tan
 
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 SystemJames Paglinawan
 
Education System of the Philippines
Education System of the PhilippinesEducation System of the Philippines
Education System of the PhilippinesCarms Celis
 
DepEd, CHED and TESDA
DepEd, CHED and TESDADepEd, CHED and TESDA
DepEd, CHED and TESDArajnulada
 
K to 12 General Presentation
K to 12 General PresentationK to 12 General Presentation
K to 12 General PresentationDepEdPhilippines
 
K to 12 classroom assessment ppt
K to 12 classroom assessment pptK to 12 classroom assessment ppt
K to 12 classroom assessment pptCarlo Magno
 
Historical foundation of philippine education
Historical foundation of philippine education Historical foundation of philippine education
Historical foundation of philippine education Michael John Labog
 
K 12 basic education program
K 12 basic education programK 12 basic education program
K 12 basic education program19710802
 

Destacado (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
 

Último

Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
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 TerraformAndrey Devyatkin
 
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...apidays
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
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 FresherRemote DBA Services
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 

Último (20)

Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
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
 
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...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
+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...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
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
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 

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