Hi all;

I've to update a record for my hw.I'm trying to use seek function but when I use it, new information(phone number) is being written at the end of file...

Here is the function that I've tried...

Assume that the file's content like below and the input is :
1)Peter Parker
2)00000

peter parker 56372
eddie brock 23456
.....
.....

sub edit()
{
    print "Enter the person's name and surname which you'll update the record\n";
    chomp($dest = <STDIN>);

    while($line = <FILE>)
    {
            @array1 = split(/ /,$dest);
        
            @array = split(/ /,$line);

            if($array1[0] eq $array[0] && $array1[1] eq $array[1]){
            
            print "Enter new phone number\n";
            chomp($newNum = <STDIN>);

            seek(FILE,-1 * length($array[2]),1) || die("Couldn't seek back\n");
            print(FILE "$newNum\n");
            $a = 1;
            }    
    
        
    }

        if($a != 1){
            print"Record doesn't exist!!!\n";
        }

}

desired output:

peter parker 00000
eddie brock 23456
....
....

above code's output:

peter parker 56372
eddie brock 23456
....
....
00000

Recommended Answers

All 8 Replies

It would be simpler to use perls inplace editor for this type of task;

sub edit {
    my $file = 'path/to/file.txt';
    print "Enter the person's name and surname which you'll update the record: ";
    chomp($dest = <STDIN>);
    my ($name, $surname) = split(/\s+/,$dest);
    print "Enter new phone number: ";
    chomp($newNum = <STDIN>);
    local @ARGV = ($file);
    local $^I = '.bak';
    my $found = 0;
    while(<>)
    {
            my @array = split(/\s+/);
            if($name eq $array[0] && $surname eq $array[1]){
               print "$array[0] $array[1] $newNum\n";
               $found = 1;
            }    
            else {
               print;
            }
    }
    unless($found){
       print"Record doesn't exist!!!\n";
    }
}

I confused a little...

I don't know these :

local @ARGV = ($file);
local $^I = '.bak';
split(/\s+/) (what's the meaning of \s+???)

We haven't seen that.Can you explain me with seek usage??

I confused a little...

I don't know these :

local @ARGV = ($file);
local $^I = '.bak';
split(/\s+/) (what's the meaning of \s+???)

We haven't seen that.Can you explain me with seek usage??

Below quoted from: http://perldoc.perl.org/perlvar.html

# ARGV

The special filehandle that iterates over command-line filenames in @ARGV . Usually written as the null filehandle in the angle operator <> . Note that currently ARGV only has its magical effect within the <> operator; elsewhere it is just a plain filehandle corresponding to the last file opened by <> . In particular, passing \*ARGV as a parameter to a function that expects a filehandle may not cause your function to automatically read the contents of all the files in @ARGV .
# $ARGV

contains the name of the current file when reading from <>.
# @ARGV

The array @ARGV contains the command-line arguments intended for the script. $#ARGV is generally the number of arguments minus one, because $ARGV[0] is the first argument, not the program's command name itself. See $0 for the command name.
# ARGVOUT

The special filehandle that points to the currently open output file when doing edit-in-place processing with -i. Useful when you have to do a lot of inserting and don't want to keep modifying $_. See perlrun for the -i switch.

# $INPLACE_EDIT
# $^I

The current value of the inplace-edit extension. Use undef to disable inplace editing. (Mnemonic: value of -i switch.)

<end of quote>

$^I tells perl to edit a file inplace. This is a very fast way to edit files with perl. This is much better than what you are trying to do. "local" just makes the values of @ARGV and $^I local to the block they are used in so they do not affect the rest of a program. This is just a good habit to get into unless you really know what you are doing with perls global variables like @ARGV and $^I.

I never use "seek" for anything so I can't advise you on using it. Read the seek documentation and it might help you.

http://perldoc.perl.org/functions/seek.html

We haven't seen that.

Is this school work?

\s+ just means: one or more spaces.

Thanks for the reply and yes this is a school work and i've to do it until Friday...

But I'm still confusing.We only learned the ARGV for run the script with a command by using command-prompt like "perl add.pl 34 67"

Don't be confused, @ARGV is just an array, you can also use it like I showed.

Nice presentation from you Kevin.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.