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

Hi Monks, I have a file that contains some employee information .. sort schedulling file. I need to update the file and sechdul more people however , I wana make sure I am not sechdulling the same person based on thier number the data file looks like this :
data ---- Loc num name age car $$ 817 09 Sam 99 TU 300 444 11 Ram 88 CM 7 111 3 Kin 8 RT 88
so the key will be the num ,if the employee number exists then error. I was doing the following:
my $empNUM; open(DATA, "data") or die ; open(UPDAT, ">updated") or die; while(<DATA>) { my @line = split(/ /, $_); foreach $line (@line) { if ($line[2] eq $empNum){exit 0;} } }end loop print UPDAT $details; close(DATA); close(UPDAT);
since I am new to perl , I don't think this is the wise way of doing it , any advice on doing this? thanks

Replies are listed 'Best First'.
Re: data inside file
by AcidHawk (Vicar) on May 11, 2004 at 20:16 UTC
    I am not sure what you are trying, but a few comments.

  • You declare my $empNUM; but later refer to $empNum - Possible Type ??
  • since you are using my try to be consistant.. so you line foreach $line (@line) would look like foreach my $line (@line)
  • instead of exiting I.e. exit 0; you might have employees to process after you found a match so instead do a next
  • So you might end up with something like

    my $empNUM; open(DATA, "data") or die ; open(UPDAT, ">updated") or die; while(<DATA>) { my @line = split(/\s+/, $_); #Assuming your file is split on multi +ple spaces..? if ($line[2] eq $empNUM){ next; } else { print UPDAT "$_"; } } close(DATA); close(UPDAT);
    Update: Removed the foreach as it is not necessary..
    -----
    Of all the things I've lost in my life, its my mind I miss the most.
Re: data inside file
by mifflin (Curate) on May 11, 2004 at 20:24 UTC
    try
    @line = split /\s+/, $_;
    as your split.
    \s is matches whitespace.
    The plus sign says one or more of the prior character.
    Note: you really dont need th $_ in the split statement.
    By default split uses $_
    so your split line could look like...
    @line = split /\s+/;
Re: data inside file
by saintbrie (Scribe) on May 11, 2004 at 23:44 UTC
    You probably want to look at using a hash lookup, instead of an array.
    my %lookup = (); # initialize the hash while (defined ($line = <DATA>)) { my @line = split /\s+/, $line; if (defined ($lookup{$line[1]})) { # array indexes start at 0 # if the hash key already has been defined (see below)... exit(0); # error out; } else { $lookup{$line[1]} = 1; # define the lookup value; } }
Re: data inside file
by nothingmuch (Priest) on May 11, 2004 at 20:05 UTC
    The criteria for split is not good, because the match will not split columns. Read perlretut and split to see how to write an appropriate regex.

    end loop is not valid perl, you ought to comment it f that's what you mean.

    Think what happens to the data after the line you updated.

    Unless this is home work, there are many modules on the CPAN to help deal with serialized tables of any sort, from CSVs to an SQL server's tables. Perhaps most notable is the AnyData namespace.

    -nuffin
    zz zZ Z Z #!perl