Sponsored Content
Top Forums Shell Programming and Scripting getting data list into a hash array Post 48496 by Optimus_P on Tuesday 9th of March 2004 10:49:05 AM
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$

 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
qmail-getpw(8)                                                System Manager's Manual                                               qmail-getpw(8)

NAME
qmail-getpw - give addresses to users SYNOPSIS
qmail-getpw local DESCRIPTION
In qmail, each user controls a vast array of local addresses. qmail-getpw finds the user that controls a particular address, local. It prints six pieces of information, each terminated by NUL: user; uid; gid; homedir; dash; and ext. The user's account name is user; the user's uid and gid in decimal are uid and gid; the user's home directory is homedir; and messages to local will be handled by home- dir/.qmaildashext. In case of trouble, qmail-getpw exits nonzero without printing anything. WARNING: The operating system's getpwnam function, which is at the heart of qmail-getpw, is inherently unreliable: it fails to distinguish between temporary errors and nonexistent users. Future versions of getpwnam should return ETXTBSY to indicate temporary errors and ESRCH to indicate nonexistent users. RULES
qmail-getpw considers an account in /etc/passwd to be a user if (1) the account has a nonzero uid, (2) the account's home directory exists (and is visible to qmail-getpw), and (3) the account owns its home directory. qmail-getpw ignores account names containing uppercase let- ters. qmail-getpw also assumes that all account names are shorter than 32 characters. qmail-getpw gives each user control over the basic user address and all addresses of the form user-anything. When local is user, dash and ext are both empty. When local is user-anything, dash is a hyphen and ext is anything. user may appear in any combination of uppercase and lowercase letters at the front of local. A catch-all user, alias, controls all other addresses. In this case ext is local and dash is a hyphen. You can override all of qmail-getpw's decisions with the qmail-users mechanism, which is reliable, highly configurable, and much faster than qmail-getpw. SEE ALSO
qmail-users(5), qmail-lspawn(8) qmail-getpw(8)
All times are GMT -4. The time now is 02:30 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy