in reply to Re: Concatenating arrays fetched from different text files
in thread Concatenating arrays fetched from different text files
Sample of text-1.txt files:
Sample of text-2.txt files:Line_1:Line_1_1 Line_2:Line_2_2 Line_3:Line_3_3 Line_4:Line_4_4
Sample of text-3.txt files:Line_5:Line_5_5 Line_6:Line_6_6
the array would have the content 'Line_9_9' and 'Line_10_10' in the second column (where one would expect content from the second file only). This is because push simply appends at the end of the array.Line_7:Line_7_7 Line_8:Line_8_8 Line_9:Line_9_9 Line_10:Line_10_10
To avoid that it would be better to use fixed indexing instead of push:
So
could be replaced by thismy $i = 0; while (<>) { # read from @ARGV chomp; push @{ $value[$i++] }, /:([^:]+)/; # get timestamp $i = 0 if eof; }
my $i = 0; my $filecount = 0; while (<>) { # read from @ARGV chomp; $value[$i++]->[$filecount] = /:([^:]+)/; # get timestamp if (eof) { $i = 0; ++$filecount; } }
You can test if an array cell has a value assigned with code like the following:
Hope this helps! Update: forgot a closing brace in the new code block. It is now fixed.if (!exists $value[$line]->[$file]) { # no value assigned, there is a hole in the array } else { # value assigned }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Concatenating arrays fetched from different text files
by thanos1983 (Parson) on May 29, 2014 at 19:46 UTC | |
by hexcoder (Curate) on May 29, 2014 at 20:55 UTC | |
by thanos1983 (Parson) on May 30, 2014 at 10:53 UTC |