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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
How can I do this?

What Anonymous Monk says. But that's probably too dense at first glance. So let's go step by step.
You have a well formed input file containing two fields separated by ' : '.
The first field is data, the second field denominates the destination file.
To write the data portion to the destination, you have to open/create a file for writing, for the respective destination. See open.
While processing each line, you want to separate the data from the destination denominator. See split.
Now, how can you get the filehandle to print at from the destination denominator? You store the filehandle in a hash. See perldata.
A hash can store filehandles as values to be retrieved via a string. Since you have to retrieve the destination for each line of input, you need not open the files beforehand, since you can combine the retrievement of a filehandle from the hash with a logical or: if it doesn't exist, you open the file and store the filehandle in the filehandle hash under the respective destination key.

How could you put that into code? Well, first let's set up a filehandle hash containing the tokens and filehandles for the destination. See my.

my %fh;

Reading a file given as an argument at the command line is easy:

while(<>) { # the line read is stored into $_ chomp; # remove line ending character from $_ ... }

See readline for the diamond operator <>. See chomp.
Split the line based on your separator (see perlre, perlop and perlfunc):

my( $data, $fh_token ) = split /\s*:\s*/;

Now, get the filehandle for $fh_token, and if it doesn't exist, create one and store the filehandle into %fh

if( ! $fh{$fh_token}) { open my $fh, '>', "$fh_token.txt" or die "Can't write to '$fh_token.txt': $!\n"; $fh{$fh_token} = $fh; # store filehandle } my $fh = $fh{$fh_token}; # this is the filehandle to print to

Then print the data stuff to the determined filehandle:

print $fh $data, "\n"; # append a line break. You'd use "\r\n" on +windows.

So, we have:

my %fh; while(<>) { # the line read is stored into $_ chomp; # remove line ending character from $_ my( $data, $fh_token ) = split /\s*:\s*/; if( ! $fh{$fh_token}) { open my $fh, '>', "$fh_token.txt" or die "Can't write to '$fh_token.txt': $!\n"; $fh{$fh_token} = $fh; # store filehandle } my $fh = $fh{$fh_token}; print $fh $data, "\n"; # append a line break. You'd use "\r\n" on +windows. }

The code by Anonymous Monk above makes use of features built into perl. See perlrun and IO::File. Condensation of this code to resemble the succinct variation is left as an excercise to the reader. TIMTOWTDI (there's more than one way to do it).

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

In reply to Re: Break a file into separate files based on string match by shmem
in thread Break a file into seperate files based on string match by hvirani

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 about the Monastery: (3)
As of 2024-04-19 05:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found