I have a program that prints out the location of commas in a paragraph of text in the form
For example if the paragraph is
one,two,three
three and a half
four,five
six
seven,eight
The program will print
0:4
0:8
2:5
4:6
I would like to use this output to create an array where the numbers after the colon are listed across columns in the row specified by the index before the colon. The array formed by the coordinates above would be
4 8
<blank or character '.'>
5
<blank or character '.'>
6
so array[0,0] = 4, array[0,1] = 8
array[1,0] = empty
array[2,0] = 5
etc…
I bet this is simple but I need help to write it.
$data_file="file.out";
open(DAT, $data_file) || die("Could not open file!");
@raw_data=<DAT>;
close(DAT);
my %array;
my $line = 0;
foreach $newline(@raw_data) {
chomp;
while ( $newline=~m/(,|;|:|and)/) {
push @{ $array{$line} }, pos($newline); # autovivification
}
$line++; }
Program
Output
Refer:
chomp,join,keys,sort,split.Refer the following documents to get an understanding of Perl’s data structures and especially autovivification which has been used in this example.
perldoc perlrefperldoc perlreftut