On 3/5/12 Mon  Mar 5, 2012  10:34 AM, "Lancashire, Pete"
<pete.lancash...@portlandoregon.gov> scribbled:

> my brain this morning is blocked on this one
> 
> I have a file something like
> 
> cpu01  value value value
> cpu02  value value value
> cpu03  value value value
> ...
> cpu01  value value value
> cpu02  value value value
> cpu03  value value value
> ...
> cpu01  value value value
> cpu02  value value value
> cpu03  value value value
> ...
> 
> in this example column 1 can be cpu\d+ and cpu_all other it can be even eth0,
> fcs0 etc
> 
> I want to open files for writing, one for each unique value in the first
> column,
> so that if the value of the data in the first column is "cpu01" then
> write to the file with the FH in the variable $cpu01 or if the value is
> "cpu_all" write to the
> file with the FH assigned to the the vaule $cpu_all
> 
> This has to be simple ...

Where are you getting the data shown above? I shall assume it is from a
file. Please let us know if that is not correct.

The simplest approach would be to store the data into a hash, keyed by the
values in column 1: 'cpu01', 'cpu_all', 'eth0', etc. Then, when you have
read all of the data, you can iterate over the keys and write the data for
each key to a separate file. This will work fine as long as you have enough
memory in your computer to hold all of the input data.

If not, then you can store file handles in a hash, keyed by the values from
column 1. See 'perldoc -q filehandles' "How do I make an array of
filehandles?" This requires that you open and keep open a number of files
simultaneously. That should work as long as the number of files is not too
great for your operating system.



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to