Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Use of Uninitialized in Concatenation or String Error?

by Laurent_R (Parson)
on Aug 09, 2013 at 09:27 UTC ( #1048723=note: print w/ replies, xml ) Need Help??


in reply to Use of Uninitialized in Concatenation or String Error?

Hi,

I am not sure that it will work on every system, but I was able to create on my system an array of 155 file handlers. Then, I only need to read the input data, find out in which range the line should be output and print it to the right fila handler. Thus, no need to sort the data.

use strict; use warnings; use integer; no strict "vars"; my @FH; for my $i (0..154) { my $file = "file$i" . ".txt"; open $FH[$i], ">", $file or die "could not open $file $!\n"; my $fh = $FH[$i]; print $fh "File number $i \n"; } while (<DATA>) { my $locus = (split /\t/, $_)[4]; my $range_nr = $locus / 1e6; warn "out of range: $_" and next if $range_nr > 154; my $fh = $FH[$range_nr]; print $fh $_; } close $FH[$_] for (1..154) __DATA__ 0 50 2 48 654732 0 1 1 1 0 2 3 2 + 1 3 0 50 4 46 1723430 0 2 1 2 1 1 1 1 + 3 1 0 50 2 48 14654732 0 1 1 1 0 2 3 2 + 1 3 0 50 4 46 7723430 0 2 1 2 1 1 1 1 + 3 1 0 50 2 48 2654732 0 1 1 1 0 2 3 2 + 1 3 0 50 2 48 2654733 0 1 1 1 0 2 3 2 + 1 3 0 50 2 48 2654734 0 1 1 1 0 2 3 2 + 1 3 0 50 2 48 2654735 0 1 1 1 0 2 3 2 + 1 3 0 50 2 48 2654736 0 1 1 1 0 2 3 2 + 1 3 0 50 4 46 6723430 0 2 1 2 1 1 1 1 + 3 1

Content of file2.txt:

File number 2 0 50 2 48 2654732 0 1 1 1 + 0 2 3 2 1 3 0 50 2 48 2654733 0 1 1 1 + 0 2 3 2 1 3 0 50 2 48 2654734 0 1 1 1 + 0 2 3 2 1 3 0 50 2 48 2654735 0 1 1 1 + 0 2 3 2 1 3 0 50 2 48 2654736 0 1 1 1 + 0 2 3 2 1 3


Comment on Re: Use of Uninitialized in Concatenation or String Error?
Select or Download Code
Re^2: Use of Uninitialized in Concatenation or String Error?
by ccelt09 (Sexton) on Aug 09, 2013 at 11:10 UTC

    This looks like an interesting solution. I'll have to put in some time to fully understand what you did, but I very much appreciate the help. It's great to see how others approach a problem differently and, certainly here, more eloquently.

      Just to explain a bit more what the program does. In the first for loop, the program is opening 155 files, named from file0.txt to file154.txt (and writes a header line "File number #", this is not really needed, this was just a debugging mean of checking that the program does actually write correctly in all 155 files).

      Then I read sequentially the input data file; for each line, I look for the locus and divide its value by 1000000 (integer division), which returns a range between 0 and 154. This range is then used to choose into which output file to print the current line.

      Then there is a final for loop to close all the files.

        Awesome, that is quite efficient in the case that all files can be open and printed to at once, good thinking. The 1e6 range was the largest I'll be working with, the smallest being 1e3, meaning 155,247 files. For those I will have to get my original program to work, I don't know if opening that many files at once is feasible.

        I am working on getting a while loop to actually loop with a post incremented value, I think I'll try posing about it after a a few more attempts. Good on ya mate! Thanks for the help

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2014-12-26 11:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls