in reply to a hash/array problem and can contents of an array be printed as one line

Well, there's no reason to read both files into mem.. you can just process/replace the items in file 2 as you read it..
for example
open(FILE, "<$file1") || die "oh the misery, $!"; while(<FILE>) { chomp; # making the assumption that each field # is delimited by comma space, safe? ($id, $desc) = split(/, /); $update_hash{$id} = $desc; } close(FILE); #create backup rename $file2, "$file2.bak" || die "bah, skum $!"; open(INFILE, "<$file2.bak") || die "dagnabit! $!"; open(OUTFILE, ">$file2") || die "ookiemouth! $!"; while(<INFILE>) { chomp; # once again, assuming data is delimited # by comma space @vals = split(/, /); # update if a new description exists $vals[4] = $update_hash{$vals[2]} if $update_hash{$vals[2]}; # write file delimited by comma space (even last item) foreach $data (@vals) { print OUTFILE "$data, "; } print OUTFILE "\n"; } close(INFILE); close(OUTFILE);
Not a whole lot changed, just some extra stuff, including more validation..
-Syn0
  • Comment on Re: a hash/array problem and can contents of an array be printed as one line
  • Download Code

Replies are listed 'Best First'.
Re: Re: a hash/array problem and can contents of an array be printed as one line
by abstracts (Hermit) on Jul 16, 2001 at 03:22 UTC
    If everytime you open the file you split by /, / and then print using:
    # write file delimited by comma space (even last item) foreach $data (@vals) { print OUTFILE "$data, "; } print OUTFILE "\n";
    You will end up with a file with lots of trailing commas.
    Instead of the loop, you can use:
    print OUTFILE join(', ', @vals) . "\n";

    Hope this helps
    Aziz,,,

      The original post had the commas (explicitly put in there by the code he wrote) at the end of the lines... Instead of changing his file formats, I just used the format that was stated.
      -Syn0