in reply to Pick Up Files and put contents into variables

I have no idea how to do this, so help would be much appreciated.

:) The idea is to opendir, readdir,open,<>,split, close, closedir using some loops and usefull data strucures.

As data structure is propose a hash withe filenames as keys and arrayrefs as value.

#!/usr/bin/perl use strict; use warnings; my %files; my $dir = shift || '.'; opendir DIR,$dir or die "error opeing directory $dir: $!"; while( defined( my $file = readdir DIR ) ){ my $path = "$dir/$file"; next unless -f $path; open my $fh , '<' , $path or do { warn "$file : $! skipping\n"; next }; $files{$file} = []; while(<$fh>){ push @{$files{$file}} , [ split ] } close $fh; } closedir DIR;
--
http://fruiture.de

Replies are listed 'Best First'.
Re: Re: Pick Up Files and put contents into variables
by Anonymous Monk on Aug 30, 2002 at 11:27 UTC
    hi there,

    Thanks for that, but how would i obtain the output from the files? i don't see any part of the script that puts it into variables?
    I am useless at perl, so i probably overlooked it!
    AP3K
      push @{$files{$file}} , [ split ]

      This line actually assignes the fields of the current line to "a variable". More detailed 'split' actually means 'split " ",$_' which is described in `perldoc -f split`. The resulting list is put into an anonymous array-reference, which itself is pushed into an array that is again an anonymous ref and the value of the filename-key in the hash %files.

      --
      http://fruiture.de