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

Good morning! I have a variable <$user> that is being passed through, massaged, then it is written to a log file and timestamped.
What I would like to do is take that variable and create a file with the variable appended on the end so that the text file only contains timestamps. i.e. "user_status_file.$user"
I was given some code some time back and am trying to make it work, but am not having any luck.

open (TEST,">>c:/temp/argtest6.txt") or die "Failed to open argtest.tx +t file"; # Configure the number of times and the time window using the # following two variables. $times = 5; $minutes_threshold = 30; # Parse the command line parameter - should be the user name $count = 0; foreach $element (@ARGV) { #print TEST "$count,$element \n"; $count += 1; } $raw_user = lc ("$ARGV[7]"); my $user; if ($raw_user =~ /^(\w+)/) { $user = $1; } print TEST "$user\n"; $now = time(); #this line works $oldest_time = $now - ($minutes_threshold*60); #this line works $user_status_file = "C:/temp/user_status.$user"; open (USF,$user_status_file) or die "Failed to open user_status_file f +ile"; @lines = <USF>; close USF; # Now, read through the lines of the file from last (most recent) to f +irst (oldest) # and see if n times occurred in the last m minutes. for ( $i=$#lines; $i>=0; $i-- ) { $timestamp = $lines[$i]; chomp $timestamp; last if ( $timestamp < $oldest_time ); $count++; } print USF "$now\n"; rint USF "$now\n"; close USF; close TEST;
The 'for' statement works, but am unable to get the user_status_file.$user created.
Is there a better way of doing this?
The purpose is so I can watch how many timestamps that particular $user has and can threshold on excessive timestamps.

Thanks,
Jonathan

Replies are listed 'Best First'.
Re: How do I create a file based on a variable?
by Corion (Patriarch) on Aug 03, 2006 at 14:16 UTC

    Uhhh - do you want to read from the file or do you want to create that file? The code you have is for reading from that file, and you seem to say that reading works for you, so I assume that you also want to create the file and write to it. But that doesn't make sense, as you have no data to write to the file. So, let's create a different file with the number of timestamps. See the documentation for the open function.

    use strict; my $user_status_output = "C:/temp/user_status.$user.out"; my @output = ( "Hello world", "This is a test", "And some more text to go into the output file" ); open my $out, ">", $user_status_output die "Failed to create output file '$user_status_output': $!"; for my $line (@output) { print $out "$line\n"; }; close $out; print "Wrote output to '$user_status_output'\n"; <c>
Re: How do I create a file based on a variable?
by Fletch (Bishop) on Aug 03, 2006 at 14:18 UTC

    Erm, you're opening the file for reading, then reading in the contents; that, not surprisingly, won't create a new file. You need to read perlopentut about how to specify opening a file for writing if that's what you're really trying to do.

Re: How do I create a file based on a variable?
by ptum (Priest) on Aug 03, 2006 at 14:22 UTC

    So, there are a few problems. You may want to read up on open to see how to open a file handle to a file for reading, writing, or both. You may want to open the file in "<+" mode, if you want to read and write to the same file.

    I don't see you writing to the file anywhere ... I expect to see something like this (assuming you have opened the file for writing):

    print USF "$timestamp\n";

    Personally, I would probably store the last timestamp for each user in a relational database table, and then query the table each time I saw that user, to see if they are within the threshold.


    No good deed goes unpunished. -- (attributed to) Oscar Wilde
      Sorry, I did not include the whole script.
      Right now I can get all $user's and their respective timestamps to the TEST log. But wanting it to create the user_status.$user file to collect on them individually.
      I will look into the open command in more detail.