Sponsored Content
Top Forums Shell Programming and Scripting PERL: reading 2 column data into Hash file Post 302366524 by subhap on Thursday 29th of October 2009 07:23:09 PM
Old 10-29-2009
PERL: reading 2 column data into Hash file

I am trying to read in a 2 column data file into Perl Hash array index. Here is my code.
Code:
#!/usr/bin/perl -w
use strict;
use warnings;

my $file = "file_a";
my @line = ();
my $index = 0;
my %ind_file = ();

open(FILE, $file) or die($!);
while(<FILE>) {
chomp($_);
 if ($_ eq '')
 {
  print "undefined line \n";
 }
 else
 {
  my ($name, $value) = split(' ', $_);

# Put the key => value pair in the hash
  $ind_file{$name} = $value;
 # print "$name => $value \n";
 my ($k, $v) = each %ind_file;
 print "$k => $v \n";
 }
}
close(FILE);

while ( ($k, $v) = each %ind_file) {
 print "Post $k => $v \n";
}
========================================================

I am getting a lot of these..
Use of uninitialized value in concatenation (.) or string at ./print-hash.pl line 49, <FILE> line 2.
at ./print-hash.pl line 49

any ideas.. and finally when I see the post index file, most of the data read into the hash is missing or gone..

----------------------------------------------------------------------
Input data file: file_a
Code:
HOSTNAME aria
AVAIL_MEM 21
USED_MEM 11
HOSTNAME borneo
AVAIL_MEM 31
USED_MEM 1
HOSTNAME croc
AVAIL_MEM 11
USED_MEM 21
USER bob
JL/U 2
RUN 3
PEND 4
WAIT 1
USER jill
JL/U 3
RUN 2
PEND 10
WAIT 3
QUEUE short
JOBS 10
PEND 3
RUN 1
QUEUE long
JOBS 2
PEND 10
RUN 2
=====================================



---------- Post updated at 06:23 PM ---------- Previous update was at 05:47 PM ----------

I got around the uninitialized values part, and missing data is, 'cause the primary key for example: HOSTNAME repeats says 20 times, the Index would remember only the last value stored.

I would like a way to store all possible HOSTNAME (20 times) as an KEY INDEX with values. Any ideas of making this possible?

Last edited by pludi; 10-30-2009 at 03:44 AM.. Reason: code tags, please...
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Column data reading

Experts I am new to UNIX shell programming ( or scripting). My problem is that I have an ASCII file in which column wise data is present, the columns are seperated by spaces. I want to read each columns data and store it in arrays, next I will be using the arrays to perform some numerical... (1 Reply)
Discussion started by: FarhanNaseer
1 Replies

2. Shell Programming and Scripting

perl-extract data from hash values

Hello, I have parsed an xml file using perl to get the hash values and the output looks like this $VAR1 = { 'RT' => { 'List' => { 'String' => ... (1 Reply)
Discussion started by: userscript
1 Replies

3. Shell Programming and Scripting

Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below. #!/usr/bin/perl use warnings; use Tie::IxHash; use strict; tie (my %programs, "Tie::IxHash"); while (my $line = <DATA>) { chomp $line; my(... (1 Reply)
Discussion started by: jgfcoimbra
1 Replies

4. Shell Programming and Scripting

Perl: Reading data from other file

Hi, I am writting some perl scripts for daily backup process. In which I want to pass some data/referance from another txt file. Text file contains only one column and multiple rows. I want to pass this data to variables of another perl script. e.g. Refdoc.txt file contains data as: perl1... (3 Replies)
Discussion started by: n.dba
3 Replies

5. Shell Programming and Scripting

Remove default data hash sorting in perl script?

Hi, I have a datahash with 'n' number of values in perl script. I am writing a xml file from the datahash. I am getting output with sorting(Field sorting). My question is that i don't want any default sorting.whatever i am inserting into datahash it should give same xml file. Any help? ... (0 Replies)
Discussion started by: solo123
0 Replies

6. Shell Programming and Scripting

Reading a column from xls file using perl

Hi Everyone! I have a problem in reading a specific column from .xls file using perl language and then manipulating on given criteria. Detailed Description of the problem:: I have one .xls file, in which i have to populate two columns based on Period_date column which is in same file. My... (1 Reply)
Discussion started by: kvth
1 Replies

7. Shell Programming and Scripting

Perl Script for reading table format data from file.

Hi, i need a perl script which reads the file, content is given below. and output in new file. TARGET DRIVE IO1 IO2 IO3 IO4 IO5 ------------ --------- --------- --------- --------- --------- 0a.1.8 266 236 ... (3 Replies)
Discussion started by: asak
3 Replies

8. Shell Programming and Scripting

AWK, Perl or Shell? Unique strings and their maximum values from 3 column data file

I have a file containing data like so: 2012-01-02 GREEN 4 2012-01-02 GREEN 6 2012-01-02 GREEN 7 2012-01-02 BLUE 4 2012-01-02 BLUE 3 2012-01-02 GREEN 4 2012-01-02 RED 4 2012-01-02 RED 8 2012-01-02 GREEN 4 2012-01-02 YELLOW 5 2012-01-02 YELLOW 2 I can't always predict what the... (4 Replies)
Discussion started by: rich@ardz
4 Replies

9. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

10. Shell Programming and Scripting

Perl :: reading values from Data Dumper reference in Perl

Hi all, I have written a perl code and stored the data into Data structure using Data::Dumper module. But not sure how to retreive the data from the Data::Dumper. Eg. Based on the key value( Here CRYPTO-6-IKMP_MODE_FAILURE I should be able to access the internal hash elements(keys) ... (1 Reply)
Discussion started by: scriptscript
1 Replies
Hash::Case::Upper(3pm)					User Contributed Perl Documentation				    Hash::Case::Upper(3pm)

NAME
Hash::Case::Upper - native hash with enforced lower cased keys INHERITANCE
Hash::Case::Upper is a Hash::Case is a Tie::StdHash SYNOPSIS
use Hash::Case::Upper; tie my(%uchash), 'Hash::Case::Upper'; $uchash{StraNGeKeY} = 3; print keys %uchash; # STRANGEKEY DESCRIPTION
Hash::Case::Upper extends Hash::Case, which lets you play various trics with hash keys. In this implementation, the fake hash is case insensitive and the keys stored in upper-case. METHODS
Constructors $obj->addHashData(HASH) See "Constructors" in Hash::Case $obj->addPairs(PAIRS) See "Constructors" in Hash::Case $obj->setHash(HASH) See "Constructors" in Hash::Case tie(HASH, 'Hash::Case::Upper', [VALUES,] OPTIONS) Define HASH to have only upper cased keys. The hash is initialized with the VALUES, specified as ref-array or ref-hash. Currently, there are no OPTIONS defined. SEE ALSO
This module is part of Hash-Case distribution version 1.02, built on March 09, 2012. Website: http://perl.overmeer.net/hash-case/ LICENSE
Copyrights 2002-2003,2007-2012 by Mark Overmeer. For other contributors see ChangeLog. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See http://www.perl.com/perl/misc/Artistic.html perl v5.14.2 2012-03-09 Hash::Case::Upper(3pm)
All times are GMT -4. The time now is 02:58 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy