Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: a hash/array problem and can contents of an array be printed as one line

by synapse0 (Pilgrim)
on Jul 15, 2001 at 13:59 UTC ( [id://96844]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://96844]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-16 17:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found