perl - how to read a line and save multiple parameters into variables separated by ;? -


so lets have file.txt, documents syntax this:

"1;22;333;'4444';55555", 

i want code following:

  1. open file = done
  2. read line , save each parameter separated ; variable ( $one = 1, $two = 22, $three = 333, $four = '4444', $five = 55555; )
  3. this step writing variables db thats done already
  4. loop until lines of file done

so need step 2, think able loop , db code. guys have ideas or tips how this? beginnerfriendly nice can learn out of it.

       foreach $file (@file){                           $currentfile = "$currentdir\\$file";            open(my $reader, "<", $currentfile) or die "failed open file: $!\n";            ?????            close $reader;        } 

if you're doing 'numbered fields' should thinking 'array':

use data::dumper; while ( <$reader> ) {       chomp;     @row = split /;/;      print dumper \@row; } 

this give array can access - e.g. $row[0] first element.

$var1 = [           '1',           '22',           '333',           '\'4444\'',           '55555'         ]; 

if know headers 'named' , prefer work on names can similar hash:

#!/usr/bin/perl use strict; use warnings; use data::dumper;  @cols = qw ( id value fish name sprout );   while ( <data> ) {     %row;     chomp;     @row{@cols} = split /;/;     print dumper \%row; }  __data__ 1;22;333;'4444';55555 

this gives instead:

$var1 = {           'fish' => '333',           'name' => '\'4444\'',           'id' => '1',           'value' => '22',           'sprout' => '55555'         }; 

note - hashes unordered, whole point don't need care 'order' - print $row{name},"\n";


Comments