Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

concatenating text files before reading

by Hossein (Acolyte)
on Sep 05, 2013 at 06:49 UTC ( [id://1052497]=perlquestion: print w/replies, xml ) Need Help??

Hossein has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I need to read several (small) text files (all files in same dir). The content of all files are supose to go into one scalar $content.

I think of concatenating them before I read them.

How should I do it in a smart and smooth way?

Thanks :)

/Hossein
  • Comment on concatenating text files before reading

Replies are listed 'Best First'.
Re: concatenating text files before reading
by Eily (Monsignor) on Sep 05, 2013 at 09:14 UTC

    The diamond operator has a bit of magic that could help. When used with an empty filehandle, it will read lines from all files in @ARGV. So you just have to put your input file list in @ARGV, and read it all with a while loop. And glob is your friend too.

    local @ARGV = glob "folder/*.txt; # list of all .txt files in folder print while (<>); # print each line of all those files

      Thank you :)

      this was what I was looking for.

      regards

      Hossein
Re: concatenating text files before reading
by hdb (Monsignor) on Sep 05, 2013 at 09:33 UTC

    You could also do it on the command line:

    cat *.txt | perl myscript.pl
Re: concatenating text files before reading
by 2teez (Vicar) on Sep 05, 2013 at 09:05 UTC

    Hi Hossein,
    I need to read several (small) text files (all files in same dir)
    look into using opendir to open your directory, then you can use readdir to get your files to read, using open.
    However, one need ask; Is the ORDERing of your files contained in your directory important? If so, you might have to sort your file in a way to get the final content in the a correct order.
    * The content of all files are supose to go into one scalar $content
    You can initialize a string say my $content = ""; then when you open and read through each of the files, chomping the newlines from each lines you can concatenate like so

    ... while(my $line = <$fh>){ chomp $line; $content .= $line; }
    * Update.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
      Thank you :) Sorting is not needed:)

      Regards

      Hossein
Re: concatenating text files before reading
by Anonymous Monk on Sep 05, 2013 at 07:08 UTC

    How should I do it in a smart and smooth way?

    Can you do it in such a way that you consider dumb/rough?

      I can write many lines of code to do it, but I'm sure there is a better and smarter way of doing it. That's why I asked for help:)

      First thing I learned about Perl: "There are more than 1000 ways of doing it"

        I can write many lines of code to do it, but I'm sure there is a better and smarter way of doing it. That's why I asked for help:) First thing I learned about Perl: "There are more than 1000 ways of doing it"

        If you remove the newlines then it all fits on one line -- I sure would like to see how you would do it

        my $textref = cat_files_ref( ... ); sub cat_files_ref { use File::Slurp; my $buffer = ""; for my $file ( @_ ){ read_file( $bin_file, buf_ref => \$buffer, { binmode => ':raw' } ) ; } return \$buffer; } sub cat_files_ref { use Path::Tiny; my $buffer = ""; for my $file ( @_ ){ $buffer .= path( $file )->realpath->slurp_raw; } return \$buffer; } sub cat_files_ref { use IO::All; my $content; for my $file ( @_ ){ $content << io($file); } return \$content; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-23 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found