Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: process a file and reading a line and passing the values to another sub function

by 2teez (Vicar)
on Dec 12, 2012 at 16:43 UTC ( [id://1008528]=note: print w/replies, xml ) Need Help??


in reply to process a file and reading a line and passing the values to another sub function

Hi Vijay8l,
Am trying to read directory/subdirectories and for files from a path recursively

Why not use File::Find instead of doing all the jobs yourself.

..reading file content(usually just 1 line in a file) and substituting space with comma from the line.
Are you trying to create a Comma-separated values files? If yes then see Text::CSV.

Just to give a head up, using File::Find and substitution function, I did something very close to what you wanted.

#!/usr/bin/perl use warnings; use strict; use File::Find; my $base_dir = '...'; # put in your base directory find( \&wanted, $base_dir ); sub wanted { return if $_ eq '.' or $_ eq '..'; if (-d) { print " >>> dive into: $_\n" if -d; } else { readout_file($_); ## call subroutine readout_file } } sub readout_file { my ($filename) = @_; open my $fh, '<', $filename or die "can't open file:$!"; while (<$fh>) { chomp; s/ /,/; ## OR s/ /,/g; if you want print $_, $/; ## OR any other subroutine you want } }

Update:
Seriously, Please look into Text::CSV if csv files are intended.
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
  • Comment on Re: process a file and reading a line and passing the values to another sub function
  • Download Code

Log In?
Username:
Password:

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

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

    No recent polls found