Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: reading N files

by GrandFather (Saint)
on Jul 19, 2006 at 17:36 UTC ( #562365=note: print w/replies, xml ) Need Help??


in reply to reading N files

This smacks of an XY Problem. Unless you are interleaving the files in some fashion, opening the file handles in advance does not seem like a good solution. If this is not a task requiring interleaving you might like to explain what you want to achieve so we can help with the larger problem.

BTW, you should generally use the three parameter open to avoid surprises. Your two open lines change to:

open (OUT, '>', $outfile') || ... and open (IN, '<' $_) || ...

Generally the usage line is better printed only when "required":

if (! @ARGV) { print "Usage: $0 output input1 input2 ...\n"; exit -1; }

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: reading N files
by gri6507 (Deacon) on Jul 19, 2006 at 21:16 UTC
    Actually, this was to interleave the input files. I had a problem where I had N files, each with 2 columns: a time column and a value column. I needed to interleave the N files, so that the output would have N+1 columns: one sorted time columns and N columns of either empty cells or the corresponding value. I hope this is a bit clearer.
      So, if you happened to be on a *n*x box (or have a windows port of standard unix utilities), you could just do a shell command (that includes a perl one-liner):
      # assuming N files are named in some systematic way, # and columns are separated by whitespace: paste file.* | perl -pe '($t)=(/^(\S+)/); s/\t$t//g;' > multi-column.f +ile
      The unix "paste" command takes a list of file names and concatenates them "horizontally", line by line; for a list of input files (1..N), its default behavior replaces the newline with a tab for each line of files 1..N-1.

      Assuming that all files in the set have the same series of values in the first column, the perl script removes all but the first occurrence of that value on each line. (If all these assumptions don't apply, then your approach of reading from a set of file handles in a loop is fine, of course.)

        I didn't know about the paste command. I'll have to keep it in mind in the future. However, it would not have worked in my scenario. One of the problems is that that values in the first column may or may not be the same series, making the merging step a bit more difficult.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://562365]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2021-10-27 00:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (91 votes). Check out past polls.

    Notices?