loops - How to write a Perl script to check x number of columns to print a value in another line for each returned value -


I have the data in the CSV file (IP address), which will be 9 -2 9 columns. If there are no values ​​in the other columns, by default, it should only print in 9 columns. There is an output file that will print the value of a set of values ​​and the value of the column 9 (and 13 if a value exists) has been added to create an alias value with a constant value. My question is, how will you do it efficiently? I have this code that works:

  my $ alias0 = "component algebra = ['computer system: $ column [9]']; \ n"; My $ alias1 = "component alleys = ['computer system: $ column [9]', 'computer system: $ column [10]']; \ n"; My $ alias2 = "component_alize = ['computer system: $ column [9]', 'computer system: $ column [10]', 'computer system: $ column [11]']; \ n"; Print bark "computer system; \ n"; If (($ column [11]! = '') & Amp; amp; ($ column [10]! = '')) {Print barkowk $ alias 2} alsf (($ column [11] == '') & Amp; Amp; ($ column [10]! = '')) {Print BAROC $ alias1} Elsf (($ column [11] == '') & amp; amp; ($ column [10] == '')) {Print BAROC $ alias0}   

What I have to do to make this work, but there is a chance that columns 9-13 or 9-11 in the CSV file, etc. Will have value. It is easy to think that static writing would be fine, but I would like to understand it efficiently and well and always apply best practices. I'm new to Perl script, but it has been designed to solve problems at work continuously. Suggestion:

This output is, BTW:

  ComponentAliases = ['Computer System: 10.1.0.225', 'Computer System: 10.200.252.77', 'Computer System: 10.100 252.77 '];    

  #! Use / usr / bin / env perl strict; Use warnings; Use text: CSV_XS; My $ csv_in = text :: CSV_XS-> new or dead text :: CSV_xs-> fault error; My $ csv_out = text: CSV_XS- & gt; New ({always_quote = & gt; 1, quote_char => q {'},}) or text: CSV_XS-> error_diag; While (my $ line = $ csv_in- & gt; galline (\ * data)) {my @aliases = map "Computer system: $ _", grep defined & amp; Amp; Length, @ $ line [9. 13]; If ($ csv_out-> Coalition (@lillies)) {printf "Elements = [% s]; \ n", $ csv_out- & gt; String; }} __DATA__ 0,1,2,3,4,5,6,7,8,10.1.0.225,10.200.252.77 ,,,,,,, 0,1,2,3,4,5,6,7 , 8,10.1.0.225,10.200.252.77,10.100.252.77 ,,,,,,,,   

Output:

 C: \ temp> Gn Component Aliases = ['Computer System: 10.1.0.225', 'Computer System: 10.200.252.77']; ComponentAliases = ['Computer System: 10.1.0.225', 'Computer System: 10.200.252.77', 'Computer System: 10.100.252.77'];   

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -