Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by 2teez (Priest)
on Dec 12, 2012 at 16:43 UTC ( #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
Node Status?
node history
Node Type: note [id://1008528]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2014-09-23 08:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (211 votes), past polls