perl - Looping through a dataset and handling missing values -


I am looping through a large data file and I am finding the type of variable in each column, such as Whether it is an engineer or a float etc. It works perfectly, though, at the moment it is still very basic and I want to add another idea. So far the announcement of the variable is based on the second line of the data set. (The first one is used as the header.) Here's the beginning of the code:

  #! Use / usr / bin / perl warnings; Use diagnostics; Getopt :: Use Std; Obtain ("I: S: T:") or "Bad Options: $!" Die; If ($ opt_i) {INFILE open, "& lt; $ opt_i"; Chomp ($ headline = & lt; INFILE & gt;); $ Sec = & lt; INFILE & gt; } And (die) "input file is given \ n"; } If ($ opt_t) {$ tablename = $ opt_t; } And {$ tablename = $ opt_i; $ Tablename = ~ s / \. W \ //; } If ($ opt_s) {$ sep = $ opt_s; } And {$ sep = ","; } $ Headline = ~ s / \ "//g; $ headline = ~ s /. / /___g; @header = split / $ sep /, $ headline; $ sec = ~ s / \" // g; @second = split / $ sep /, $ second; @terms = split / $ sep /, $ sec; @ Type = partition / $ sep /, $ second; And now I have implemented a small loop. The problem is that I do not know how to handle the missing values, which have been declared with the tap. At the moment, loop is called "" i.e. < Specifies "nothing" for code> $ vartype [$ j]  

  $ j = 0; While ($ j & lt; = $ # seconds) {if ($ type [$ j] = ~ / NULL /) {$ Vertext [$ j] = ""; } Elsif ($ type [$ j] = ~ / [azza-jade] /) {$ vertex [$ j] = "varchar"; } Elsf ($ type [$ j] = ~ / ./) {$ Vertext [$ j] = "double"; } And {$ Vertext [$ j] = "int"; } $ J ++; }  

How can I apply another loop structure in the current loop so that whenever I have a value in a column, the loop reads the next value in the same column and Unless he gets a number or a word.

A sample example of my data will be:

  country.name time.NameA.L.ND.AGRI.JS.NY.GDP MKTP CD NEMPGNGFSJS Brunei Darussalam 1960NUule 1139121335.16 3.46 Brunei Darussalam 1960NUule 167759756.64 0.9 Brunei Darussalam 1960NUJ 148833 9328.59 4.19 Brunei Darussalam1961 3.98 1869828587.8 3.14 Brunei Darussalam1961 3.98 2346769422.22 3.38 Brunei Darussalam19613 9 8 9 236310 9 706.3 3.17  

As already mentioned for the loop, only the second line is used to decide on the type of variable.

Now I want to execute another loop so that in the third column AG.LND.A GRI.ZS goes through the column until it is the first real 3.98 in this case Detects value. At the moment, the loop recognizes the missing value, which is marked with zero and only provides an empty value.

I'm trying hard to find out what you are trying to do You are trying to guess column types based on column contents, this is one way to do this important thing is that when the area is NULL , then there is nothing to set, if You have already set your type, and once all the field types are determined, then loop Area exit.

  #! Use / usr / bin / perl strict; Use warnings; Use scalar :: Util qw (looks_like_number); My @name = partition '', scaler & lt; Data & gt ;; My @ type; While (& lt; data & gt;) {chomp; My @values ​​= split / {2,} /; For my $ i (0 .. $ # value) {next defined $ type [$ i]; My $ val = $ value [$ i]; Next if $ val eq 'NULL'; If ($ val = ~ / ^ [0-9] + \ z /) {$ type [$ i] = 'int'; } Elsf ($ val = ~ /^[ -0-9.]+\z/ and looks_like_number ($ val)) {$ type [$ i] = 'double'; } And {$ type [$ i] = 'varchar'; }} Unless grep {not defined}}; } Print "$ _ \ n" for the print; __DATA__ country.name time.name AGL.Agri.jsnn.gpmt.pds..immp.gnsfss Brunei Darussalam 1960 Nouvel 1139121335.16 3.46 Brunei Darussalam 1960 Null 16775 9 756.64 0.9 Brunei Darussalam 1960 Null 1488339328.59 4.19 Brunei Darussalam19613.98 1869828587.8 3.14 Brunei Darussalam1961 3.98 2346769422.22 3.38 Brunei Darussalam1961 3.98 236310 9706.3 3.17 < / Code> 

Output:

 varchar int double double double 

Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -