Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: opening many files

by jwkrahn (Abbot)
on Feb 26, 2012 at 03:51 UTC ( [id://956177]=note: print w/replies, xml ) Need Help??


in reply to opening many files

UNTESTED, but this may work:

@ARGV = glob 'datafile*'; open my $OUTPUT, '>', 'output_file' or die "Cannot open 'output_file' +because: $!"; while ( <> ) { if ( $. == 1 ) { print $OUTPUT $ARGV =~ /(\d+)$/, " has"; } print $OUTPUT " ", ( split )[ 2 ]; if ( eof ) { print $OUTPUT " $. columns\n"; close ARGV; } }

Replies are listed 'Best First'.
Re^2: opening many files
by wanttoprogram (Novice) on Feb 27, 2012 at 21:24 UTC
    thank you for reply. it is working file except i want 948 rows and 200 columns from 200 files. the program is giving me 200 rows and 948 columns. thank you.
      @ARGV = glob 'datafile*'; open my $OUTPUT, '>', 'output_file' or die "Cannot open 'output_file' +because: $!"; my %data; while ( <> ) { next unless /^(.+) (\S+)$/; push @{ $data{ $1 } }, $2; } for my $key ( sort keys %data ) { print join ' ', $key, @{ $data{ $key } }, scalar @{ $data{ $key } +}, "columns\n"; }
        thank you. the code is working fine. could you please help me understand the code. i am a beginner in perl and had hard time understanding the hi-fi code. also could you please explain how sort is working in your code. what changes do i have to make to code to get all the data with out any sorting. thank you.
        thank you. the code is working fine. could you please explain how sort is working in your code. what changes do i have to make to code to get all the data with out any sorting. thank you
Re^2: opening many files
by wanttoprogram (Novice) on May 22, 2012 at 22:05 UTC
    @ARGV = glob 'datafile*'; open my $OUTPUT, '>', 'output_file' or die "Cannot open 'output_file' +because: $!"; my %data; while ( <> ) { next unless /^(.+) (\S+)$/; push @{ $data{ $1 } }, $2; } for my $key ( sort keys %data ) { print join ' ', $key, @{ $data{ $key } }, scalar @{ $data{ $key } +}, "columns\n"; }
    ======================== The program you have given works fantastic. But I have to deal with one more thing...Here it is. I have files by name datafile1,datafile2,datafile3,datafile4.........datafile200 in a directory. But these files are stored in a a different pattern ...datafile1,datafile10,datafile11,datafile12 and so on. The above program is reading and writing in the same order. I would like to pick data from in the an order from datafile1, 2, 3 to data200. I guess I need to sort. Any suggestions and additions are appreciated. Thank you.

      That's because glob returns the results of datafile* interpolated in the order a shell would return them, so datafile11 comes before datafile2, as you've seen. A quick and dirty solution, if you know you're only dealing with up to 3 digits:

      @ARGV = glob 'datafile? datafile?? datafile???';

      A more general solution that'll sort on any number of digits:

      @ARGV = sort { my $an = substr $a, 8; my $bn = substr $b, 8; $an <=> $ +bn } glob 'datafile*';

      Aaron B.
      Available for small or large Perl jobs; see my home node.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-19 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found