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

Am completely new to perl, just started learning and it is really easy though a little icky with the syntax. I have a text file which has data stored for parameters in the following format : "param1 "VALUE" param2 "......." i need to replace VALUE with the user's input and then rename the file with the last modification time. i used the localtime(stat()) to get the timestamp but it does not get appended to the file name if i say
rename &file,&file.&timestamp
I have been trying all kinds of wierd regular expressions
$VALUE=~s/^;(.*)&$/$user
does not seem to work. please help out.. Thanking you in advance

Replies are listed 'Best First'.
Re: Regular expression and rename file help
by chargrill (Parson) on Feb 03, 2006 at 06:37 UTC

    If we can assume that file is a variable containing the old name of the file, and timestamp is a variable containing the, well... timestamp... you may find that one would access the value of a variable named file by prepending a dollar sign ($), not an ampersand (&). An ampersand generally denotes a subroutine, while a dollar sign denotes a scalar variable.

    You might also want to post a more complete version of your code for further dissection.



    --chargrill
    $/ = q#(\w)# ; sub sig { print scalar reverse join ' ', @_ } + sig map { s$\$/\$/$\$2\$1$g && $_ } split( ' ', ",erckha rlPe erthnoa stJu +" );
Re: Regular expression and rename file help
by l.frankline (Hermit) on Feb 03, 2006 at 06:41 UTC

    Hi Kitty,

    Sorry to say that, I dont understand what is your expectation. Can you provide your code to know what you've tried so far. I dont know what is stored in the $VALUE scalar variable and what you are trying to replace with the regular expression.

    regards,
    Frank

    Don't put off till tomorrow, what you can do today.

Re: Regular expression and rename file help
by chargrill (Parson) on Feb 03, 2006 at 06:40 UTC

    On second glance, I'm not sure what you're trying to do with the regex, but if my above comments gets you on your way to your solution, perhaps we won't need to delve into the problems with the regex above as written.



    --chargrill
    $/ = q#(\w)# ; sub sig { print scalar reverse join ' ', @_ } + sig map { s$\$/\$/$\$2\$1$g && $_ } split( ' ', ",erckha rlPe erthnoa stJu +" );
Re: Regular expression and rename file help
by idle (Friar) on Feb 03, 2006 at 07:47 UTC
    If the name VALUE in files always the same and you using right OS(*nix like) you can do what you need just using sh, like this very simple example:
    #!/bin/sh cat file | sed ``/VALUE/s//$1/'' > file.`date "+%s"`
    Otherwise, try this code:
    use strict; use warnings; my $user_input = shift || die "I need input"; my $file = "file"; open FH, $file or die "can't open $file for reading $!"; my @arr = <FH>; close FH; foreach(@arr){ s/VALUE/$user_input/g; } open FH, ">$file" or die "can't open $file for writing $!"; print FH @arr; close FH; rename $file, $file.time;
      Thankyou for the explanation, the rename function was not working as i had not closed the filehandler, i did not notice it at first but then figured it out. i was actually trying to build a small parser for the file. and perl was the easiest scripting language to learn. i went on to extract some parameters at first and try to run the program, will try to continue with more parameters. thank you again :)
        and perl was the easiest scripting language to learn.

        Fine to know you solved your problem, whatever it was. But are you sure of what you're saying?!? Don't misunderstand me, I love Perl - and I dislike the line-noise-reputation it unfortunately has in certain circles. But I wouldn't catalogue it as "easy" so... easily.

        As another side note, since it seems that quite a lot of people had difficulties to understand your post, may I suggest you to read How do I post a question effectively??

Re: Regular expression and rename file help
by tweetiepooh (Hermit) on Feb 03, 2006 at 10:46 UTC
    Safer would be to open the input file, get the data, process and write out to a new file. Once all is completed you can delete (unlink) the original file. This last stage could be left out until you have the rest of the program working. That way your starting file is still available.

    Other replies and your own diagnostics have solved the issues you originally asked.