PERL: reading 2 column data into Hash file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL: reading 2 column data into Hash file
# 1  
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...
# 2  
Old 11-02-2009
Code:
while(my $line = <FILE>) {
        chomp $line;
        if ($line){
                if ($line =~ m/HOSTNAME\ (.*)/){
                        $hostname = $1;
                }
                else {
                        my ($k,$v) = split(" ",$line);
                        $hash{$hostname}{$k} = $v;
                }
        }
}
foreach my $host(sort keys %hash){
        print "$host \n";
        print "$_\t$hash{$host}{$_}\n" foreach (keys %{$hash{$host}});
        print "-"x80,"\n";
}

You are not storing the data properly. Also try to use the file open using the 3 parameter standard Eg:-
Code:
open my $fh, '<', "$filename" || die "couldnot open the filename $!";

Also when ever you are using to check what is the data you are storing in the hashes always use Data:: Dumper. Its very handy

HTH,
PL
This User Gave Thanks to daptal For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question