Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
This will work under the assumption that all input files have the same number of lines. However, if you have for example

Sample of text-1.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-2.txt files:
Line_5:Line_5_5 Line_6:Line_6_6
Sample of text-3.txt files:
Line_7:Line_7_7 Line_8:Line_8_8 Line_9:Line_9_9 Line_10:Line_10_10
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.

To avoid that it would be better to use fixed indexing instead of push:

So

my $i = 0; while (<>) { # read from @ARGV chomp; push @{ $value[$i++] }, /:([^:]+)/; # get timestamp $i = 0 if eof; }
could be replaced by this
my $i = 0; my $filecount = 0; while (<>) { # read from @ARGV chomp; $value[$i++]->[$filecount] = /:([^:]+)/; # get timestamp if (eof) { $i = 0; ++$filecount; } }

This would put the timestamps always in the right places, but you would have to be prepared to have 'holes' in the array, if the input files have different number of lines.

You can test if an array cell has a value assigned with code like the following:

if (!exists $value[$line]->[$file]) { # no value assigned, there is a hole in the array } else { # value assigned }
Hope this helps! Update: forgot a closing brace in the new code block. It is now fixed.

In reply to Re^2: Concatenating arrays fetched from different text files by hexcoder
in thread Concatenating arrays fetched from different text files by thanos1983

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-04-20 02:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found