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?
5 1710 -
my %hash = ();
-
open(IN, $file) or die "Can't open $file: $!";
-
while (<IN>) {
-
my ($key, $val) = split ' ', $_, 2;
-
$hash{$key} = $val;
-
}
-
- Miller
Generally you also want to chomp() the input to get rid of the record seperator: - my %hash = ();
-
open(IN, $file) or die "Can't open $file: $!";
-
while (<IN>) {
-
chomp;
-
my ($key, $val) = split ' ', $_, 2;
-
$hash{$key} = $val;
-
}
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?: -
open (out, "> newfile_w/_sortedhash") or die $!
-
foreach $key (sort { $hash{$a} <=> $hash{$b} } keys %hash) {
-
print OUT ??
-
}
-
close (OUT);
-
very close: - open (out, "> newfile_w/_sortedhash") or die $!
-
foreach $key (sort { $hash{$a} <=> $hash{$b} }
-
keys %hash)
-
{
-
print OUT "$key = $hash{$key}\n";
-
}
-
close (OUT);
or instead of the key and value ("$key = $hash{$key}\n") to just print the value: - print OUT "$hash{$key}\n";
or just the key:
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 => ,
....
....
];
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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,...
| |