473,397 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

file to hash script

Hey!
I need to take an input file which has two columns of data
The rows of the first column either contains a number or word
The second column contains a single number
I need to take the file and make it into a hash with the first column as the keys and the second column as the values of the hash
so the input file would look like the following:

ADFSA 5.0908
923894 6.0098

So "5.09.. and 6.00 ..." as values for "ADFSA" and "923984" as keys
Im confused as to how to go about defining the hash.
so I was thinking something like this:

open(IN,$file) or die $!;
while (<IN>)
{ split (' ', $_, 2) }

%hash = $_

????
Suggestions would be greately appreciated. Hopefully their is a way of doing this w/o using modules?
Jun 16 '07 #1
5 1710
miller
1,089 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. my %hash = ();
  2. open(IN, $file) or die "Can't open $file: $!";
  3. while (<IN>) {
  4.     my ($key, $val) = split ' ', $_, 2;
  5.     $hash{$key} = $val;
  6. }
  7.  
- Miller
Jun 16 '07 #2
KevinADC
4,059 Expert 2GB
Generally you also want to chomp() the input to get rid of the record seperator:


Expand|Select|Wrap|Line Numbers
  1. my %hash = ();
  2. open(IN, $file) or die "Can't open $file: $!";
  3. while (<IN>) {
  4.           chomp;
  5.           my ($key, $val) = split ' ', $_, 2;
  6.           $hash{$key} = $val;
  7. }
Jun 16 '07 #3
So know if I wanted to sort the newly created hash in descending order (highest to lowest value) based upon the values and create a new file that would contain the sorted hash, would the following be appropriate?:

Expand|Select|Wrap|Line Numbers
  1. open (out, "> newfile_w/_sortedhash") or die $!
  2. foreach $key (sort { $hash{$a} <=> $hash{$b} } keys %hash) {
  3.     print OUT ??
  4. }
  5. close (OUT);
  6.  
Jun 17 '07 #4
KevinADC
4,059 Expert 2GB
very close:

Expand|Select|Wrap|Line Numbers
  1. open (out, "> newfile_w/_sortedhash") or die $!
  2. foreach $key (sort { $hash{$a} <=> $hash{$b} }
  3. keys %hash)
  4. {
  5. print OUT "$key = $hash{$key}\n";
  6. }
  7. close (OUT);
or instead of the key and value ("$key = $hash{$key}\n") to just print the value:

Expand|Select|Wrap|Line Numbers
  1. print OUT "$hash{$key}\n";
or just the key:

Expand|Select|Wrap|Line Numbers
  1. print OUT "$key\n";
Jun 17 '07 #5
would explain exactly why the statement after Print OUT is required (what it means how it works etc.) Im new to perl so any explnanation would enhance my learning experience.
Jun 17 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Lowell Kirsh | last post by:
I have a script which I use to find all duplicates of files within a given directory and all its subdirectories. It seems like it's longer than it needs to be but I can't figure out how to shorten...
5
by: avashisht | last post by:
Hi, I am veeeery new to perl.. i have a script where i am outputting the Environment variable to a HASH. either i want to output hash values into a text file, Or get hash into an array and then...
0
by: Bill Burwell | last post by:
I am converting a VB6 WebClass application to VB.Net. Used the VB upgrade tool to do the conversion - and it left me a lot of little code things to do. Did those code things and got my app to...
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
5
by: EP | last post by:
This inquiry may either turn out to be about the suitability of the SHA-1 (160 bit digest) for file identification, the sha function in Python ... or about some error in my script. Any insight...
6
by: mojo | last post by:
Hello all; I'm having trouble with my perl script and need some guidance. I am able to read data from a .csv file using the script and am placing it into an array @field. The data consists of four...
4
mehj123
by: mehj123 | last post by:
Hi friends, Please clear my doubt. Suppose we have a hash variable in a file, and we are accessing the same hash and adding some more keys to the hash in some other script. Will this change be...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
6
by: dillipkumar | last post by:
hi, i have one excel file, i want to create a hash of hash from the excel file, like: %hash= , sheet2 => , .... .... ];
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.