getting data list into a hash array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting getting data list into a hash array
# 1  
Old 03-09-2004
getting data list into a hash array

Begginer alert!!

I have just started learning perl and have got stuck. I need to initialize a hash array with values from a list. I've been told to do this using a split command, but its just not happening.

Here is the script and output:

SCRIPT:
Code:
#!/bin/perl
system("clear");

@ARGV=("data");
%arr_data=();

while(<>){
chomp;
($name,$ext) = split;
if ($name){
$arr_data{$name} = "$ext";
}
$arr_data{$number}=$name;
}


until ($x eq x){
print "Please enter name, enter l for list, q for quit or f to find\n";
chomp($name = <STDIN>);

if ($name eq "l"){
while (($key,$value)=each(%arr_data)){
print "Name: $value\tExt: $key\n";
}
} elsif ($name eq "f"){
print "Please enter name\n";
chomp($find_name = <STDIN>);
$ext=$arr_data{$find_name};
if($ext eq ""){
print "Name not found\n";
} else {
print "Ext: $arr_data{$find_name}\n";
}
} elsif ($name eq "q"){
$x=x;
}else {
print "Please enter extention number\n";
chomp($ext = <STDIN>);
$arr_data{$ext}=$name;
}
}

*added code tags for readability -Optimus_P

Last edited by Optimus_P; 03-09-2004 at 10:41 AM..
# 2  
Old 03-09-2004
blonde

..having bad day, submitted that before I'd finished!! Smilie

and here is the output:

Please enter name, enter l for list, q for quit or f to find
l
Name: Ext:
Name: Ext: Tracy
Name: Ext: Chet
Name: Ext: Kemi
Name: Ext: Simon
Name: Ext: Dawn
Name: Ext: 3161
Name: Ext: 68843
Name: Ext: 68942
Name: Ext: 68944
Name: Ext: Marcie
Name: Ext: 3375
Name: ' Ext: s
Name: Ext: 5734
Name: Ext: 68895
Name: Ext: 68968
Name: Ext: Ella
Name: Ext: Ken
Please enter name, enter l for list, q for quit or f to find
q
(dev,discordia) >

The list i'm inputting looks like this:

(dev,discordia) > cat data
Dawn
68968
Ella
5734
Marcie
68942
Tracy
68895
Ken
3161
Chet
3375
Simon
68843
Kemi
68944

Cheers,

Cath
# 3  
Old 03-09-2004
Okay. <> reads line by line, and therefore the thingy to be split() is actually just a single line, and there is no space to split (default split character is a space), so you get that funny result.

Just use a boolean counter to track whether you are reading an odd/even numbered (and that in turn tells whether that line is a name or extension) then, if that is a name, then output "Name:" and then the name read. otherwise "Ext:" and the extension printed. An if-else loop inside the while(<>) loop should be sufficient already.
# 4  
Old 03-09-2004
1) your data file is inconsistant w/ how your program works.
2) the way you are populateing your hash is inconsistant
3) your last else statement does nothing.
4) you have various code flaws.

always use the -w switch when codeing.
get into the habbit of useing strict.

take a look at my reworked example of your script and you will notice a differance.

one thing i would highly suggest is when you are populating the hash lowercase all the names and lowercase all the userinput for the "find" option.

Code:
[qgatu003]/export/home/mxdooley$cat test.pl
#!/usr/bin/perl -w

use strict;


system("clear");

open (DATA, "./data") || die "Can NOT open file. ($!)";

my %arr_data;

while(<DATA>){
        chomp;
        my ($name, $ext) = split;
        die "You have DUPLICATE NAMES fix this problem\n" if $arr_data{$name} ;
        $arr_data{$name} = "$ext";
}

my $option;
my $key;
my $value;
my $name;
my $ext;
my $x;
until ($x=0){
        print "Please enter option, enter l for list, q for quit or f to find\n";
        chomp($option = <STDIN> );

        if ($option eq "l"){
                while (($key,$value)=each(%arr_data)){ print "Name: $key\tExt: $value\n"; };
        } elsif ($option eq "f"){
                print "Please enter name\n";
                chomp($name=<STDIN>);
                if (exists $arr_data{$name}) {
                        $ext=$arr_data{$name};
                        if(undef $ext) {
                                print "Ext not found\n";
                        } else {
                                print "Ext: $arr_data{$name}\n";
                        }
                } else {
                        print "Name not Found\n";
                }
        } elsif ($option eq "q"){
                die "You have quit the Program\n";
        }
}
[qgatu003]/export/home/mxdooley$cat data
Dawn 68968
Ella 5734
Marcie 68942
Tracy 68895
Ken 3161
Chet 3375
Simon 68843
Kemi 68944
[qgatu003]/export/home/mxdooley$

# 5  
Old 03-09-2004
if your data is how you listed it:

name
number
name
number
....
....


then what you could do to populate your hash is:

1) use a regex to test weither the line you just read in was either a word or a number (or series of).

OR

2) you could try to use the $. variable.

From the Programming Perl:
Code:
$. 
[ALL] The current record number (usually line number) for the last filehandle you
 read from (or called seek or tell on). The value may be different from the actual physical
 line number in the file, depending on what notion of "line" is in effect
--see $/ ($INPUT_RECORD_SEPARATOR) on how to affect that. An explicit close on a filehandle
 resets the line number. Because <> never does an explicit close, line numbers increase across 
ARGV files (but see examples under eof). Localizing $. also localizes Perl's notion of "the last 
read filehandle". (Mnemonic: many programs use "." to mean the current line number.)

# 6  
Old 03-09-2004
wow, that's really good.

Thanks, I understand it more now. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Perl Array within an hash

Hi All I have been using a curl code to output an hash that looks like this $VAR1 = { 'data'... (5 Replies)
Discussion started by: ab52
5 Replies

2. Shell Programming and Scripting

array of hash - perl

How do I get the unique hashes from an array of hashes? @ar1 = ( {a=>1,b=>2}, {c=>3,d=>4},{a=>1,b=>2});I need : @ar2 = ( {a=>1,b=>2}, {c=>3,d=>4});Thanks. (2 Replies)
Discussion started by: shellwell
2 Replies

3. Shell Programming and Scripting

perl Can't coerce array into hash at

Hi guys I have this part of a perl script that returns and odd error if ($args{software}) { print " @DISTFILE_GROUPS $output->{distfile_groups}->{ get_rdist_groups}\n"; and the error is Can't coerce array into hash at i've never seed this error before, any ideas thanks... (0 Replies)
Discussion started by: ab52
0 Replies

4. Shell Programming and Scripting

perl - need help with 2 arrays to hash or 2d array?

I have 2 arrays: @array1 outputs the following: 1 1 1 2 @array2 outputs the following A B C D (2 Replies)
Discussion started by: streetfighter2
2 Replies

5. Web Development

Intersection and union of array by hash

Hi, A piece of script from Perl-cookbook I do not understand, and post here for explanation. The purpose is to find the element in either array (union), and in both array (intersection). Thank you in advance. @a=qw(1 3 5 6 7 8); @b=qw(2 3 5 7 9); foreach $e (@a, @b) {$union{$e}++ &&... (3 Replies)
Discussion started by: yifangt
3 Replies

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

7. Shell Programming and Scripting

Print Entire hash list (hash of hashes)

I have a script with dynamic hash of hashes , and I want to print the entire hash (with all other hashes). Itried to do it recursively by checking if the current key is a hash and if yes call the current function again with refference to the sub hash. Most of the printing seems to be OK but in... (1 Reply)
Discussion started by: Alalush
1 Replies

8. Shell Programming and Scripting

Read csv into Hash array?

Hi all experts, May I know how to read a csv file and read the content in a hash in PERL? Currently, I hard-coded and defined it in my code. I wanna know how to make up the %mymap hash thru reading the cfg.txt ==== csv file(cfg.txt): 888,444 999,333 === #!/usr/bin/perl my... (1 Reply)
Discussion started by: kinmak
1 Replies

9. Shell Programming and Scripting

hash,array and perl

Hi,i have a code fragment below. %tag = (); #line 1 $tag{'info'} = $datastring; #line 2 $resp = $ua->request( #$ua is a user agent POST 'http://10.2.3.0' , Content_Type => application/x-www-form-urlencoded Content => #line 3 I am not sure of what the code... (3 Replies)
Discussion started by: new2ss
3 Replies

10. Shell Programming and Scripting

Hash within array, within hash, within array...

I have a little problem. To keep a configuration simple, I've exceeded my perl knowledge. :-) I've worked with multi-dimentional arrays before, but this one has me beat: @info = ( { 'defval' => 'abc' 'stats' = ( { 'name' => 'a', }, { 'name' =>... (1 Reply)
Discussion started by: jsmoriss
1 Replies
Login or Register to Ask a Question