arrays - How can i split string per line on file -
i have file contain string this:
- ' *[0-9]-? [^a-c]@[*-^a-c]' '' < temp-test/758.inp.325.1 - ' *[0-9]-? [^a-c]@[*-^a-c]' '' < temp-test/759.inp.325.3 - ' *[0-9]@[[9-b]??[0-9]-[^-[^0-9]-[a-c][^a-c]' 'new' < temp-test/1133.inp.487.1`enter code here` - ' *[0-9]@[[9-b]??[0-9]-[^-[^0-9]-[a-c][^a-c]' 'new' < temp-test/1134.inp.487.2 - '"@@' 'm' < input/ruin.1890
i want split string per line 2 part , , hope result this:
- line 1: array[0]=' *[0-9]-? [^a-c]@[*-^a-c]'; array [1]='' < temp-test/758.inp.325.1 - line 2: array[0]=' *[0-9]-? [^a-c]@[*-^a-c]'; array [1]='' < temp-test/759.inp.325.3 - line 3: array[0]=' *[0-9]@[[9-b]??[0-9]-[^-[^0-9]-[a-c][^a-c]'; array[1]='new' < temp-test/1133.inp.487.1 - line 4: array[0]=' *[0-9]@[[9-b]??[0-9]-[^-[^0-9]-[a-c][^a-c]'; array[1]='new' < temp-test/1134.inp.487.2 - line 5: array[0]='"@@'; array[1]='m' < input/ruin.1890
and code i've try this:
#!/usr/bin/perl # location of universe file $tc = "/root/desktop/siemens/replace/testplans.alt/universe"; # open file universe; open( f, "<$tc" ); @test_case = <f>; while ( $i < 5 ) { $test_case[$i] =~ s/ //; @isi = split( / /, $test_case[$i] ); if ( $#isi == 2 ) { print "input1:" . $isi[0] . "\n"; print "input2:" . $isi[1] . "\n"; print "input3:" . $isi[2] . "\n"; } $i++; }
i confused because can't slit string " " (space), because each of line have different order space , can't 2 part. thank you.
use strict; use warnings; # stuff gives me data testing $data =<<eof; ' [0-9]-? [^a-c]\@[-^a-c]' '' < temp-test/758.inp.325.1 ' [0-9]-? [^a-c]\@[-^a-c]' '' < temp-test/759.inp.325.3 ' *[0-9]\@[[9-b]??[0-9]-[^-[^0-9]-[a-c][^a-c]' 'new' < temp-test/1133.inp.487.1 ' *[0-9]\@[[9-b]??[0-9]-[^-[^0-9]-[a-c][^a-c]' 'new' < temp-test/1134.inp.487.2 '"\@\@' 'm' < input/ruin.1890 eof $line (split(/\n/,$data)) { # re splits strings according # perceive requirements if ($line =~ /^(.*)('.*?' <.*)$/) { print("array[0]=$1; array[1]=$2;\n") } } 1;
output:
array[0]=' [0-9]-? [^a-c]@[-^a-c]' ; array[1]='' < temp-test/758.inp.325.1; array[0]=' [0-9]-? [^a-c]@[-^a-c]' ; array[1]='' < temp-test/759.inp.325.3; array[0]=' *[0-9]@[[9-b]??[0-9]-[^-[^0-9]-[a-c][^a-c]' ; array[1]='new' < temp-test/1133.inp.487.1 array[0]=' *[0-9]@[[9-b]??[0-9]-[^-[^0-9]-[a-c][^a-c]' ; array[1]='new' < temp-test/1134.inp.487.2; array[0]='"@@' ; array[1]='m' < input/ruin.1890;
applied code might this:
while ($i<5) { $test_case[$i] =~ /^(.*)('.*?' <.*)$/; @isi = ($1,$2); print "input1:".$isi[0]."\n"; print "input2:".$isi[1]."\n"; $i++; }
Comments
Post a Comment