http://www.perlmonks.org?node_id=194049

ap3k has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I hope this is possible, or i have just wrote a script for nothing!

I have a directory on my server '/var/log/user' which contains files with random names (based on date/time and random numbers). Each one of these files has the following layout:
variable1 variable2 variable3 variable4 variable5
I need the code to pick up one of these files, open it, put the contents of the file into $variable1 $variable2 ..... and then once it has done that, delete that file, so the script can then move on to the next file until it has read them all in, and no more files exist.

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

Replies are listed 'Best First'.
Re: Pick Up Files and put contents into variables
by Daruma (Curate) on Aug 30, 2002 at 10:41 UTC
    Greetings!!

    A good place to start your research would be the File Input and Output Tutorial.

    Good Luck!

    -Daruma

    Update: modified my node link... Thanks, podmaster!
Re: Pick Up Files and put contents into variables
by fruiture (Curate) on Aug 30, 2002 at 10:43 UTC
    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
      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
Re: Pick Up Files and put contents into variables
by kodo (Hermit) on Aug 30, 2002 at 10:47 UTC
    Hi ap3k!

    I don't want to give you a simple code-solution, but an idea how to work this out:
    use opendir()/readdir() to get the files in that dir (or File::Find), then open a file, split() the line so that you get the content in $var1...$varN (in an array if you not sure how much it will be).
    Then just use unlink() once you've read in the file.

    Depending on what you want to do with the data once it's all read in, I would suggest you to use a hash with filenames or something and then have the content in arrays in each element .

    good luck!

    giant
Re: Pick Up Files and put contents into variables
by Vennis (Pilgrim) on Aug 30, 2002 at 10:48 UTC
    *removed*

    Ok, in my enthousiasm, i made a mistake providing a total uncommented code solution.

    I support Giant's comment and the Homework node.

      vennis,

      Is there any chance of unremoving that code? I have been trying to do this for bloody ages now and have got nowhere. I'm about to pull all of my hair out :(
        Well, basically, what you want is reading a file, splitting it into a array, using split, split on the seperating character, which, in your example would be an space.

        Then put the list into the desired variables.

        First read the directory using readdir, and finally remove them using unlink.