Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: File Manipulation - Need Advise!

by blue_cowdawg (Monsignor)
on Jan 03, 2008 at 18:58 UTC ( [id://660297]=note: print w/replies, xml ) Need Help??


in reply to File Manipulation - Need Advise!

Dear Monk,
Let me at the risk of being repetitive since you've already gotten advise on this subject try and make things more clear to you.

Consider the following code:

#!/usr/bin/perl -w use strict; my %storage=(); # untested, but should in theory work... my $junk=<DATA>; #get rid of header while(my $line=<DATA>){ chomp($line); # Get rid of newline my ($host,$dist_id,$status)=split(/[\s\n\t]+/,$line); # split on + any whitespace my $storage{$host) = { host=> $host, dist_id => $dist_id, status=> $status }; # Put this into a hash keyed on the two fields we +want to key on } # We never removed the new line character from $junk so... print $junk; # We reclaim this from the trash can foreach my $key(sort keys %storage){ # # Print the remaining record matching the keys printf "%s\t%s\t%s\n",$storage{$key}->{host},$storage{$key}->{dis +t_id},$storage{$key}->{status}; } exit(0); __END__ COMPUTER DISTRIBUTION_ID STATUS 30F-WKS `1781183799.xxxx1' IC--- 30F-WKS `1781183799.xxx11' IC--- ADM34A3F9 `1781183799.41455' IC---

The way this works is you are going to overwrite subsequent records that you read in for the same host in the hash %storage and as a result get the last record in your data set output. Since you said in CB you don't know if your fields are space or tab separated I covered both bases by using the regex /[\s\t\n]+/ in the split callout.

Hope this helps


Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-23 15:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found