Sponsored Content
Top Forums Shell Programming and Scripting How to store info from a txt file into a hash? Post 302997833 by Aia on Saturday 20th of May 2017 12:59:35 PM
Old 05-20-2017
Quote:
Originally Posted by Eric1
I'm trying to make a perl script using the "open" command to open and read a file, storing the information in said file into a hash structure.

This is what is inside my file-

Code:
Celena Standard  F 01/24/94 Cancer 
Jeniffer Orlowski  F 06/24/86 None
Brent Koehler  M 12/05/97  HIV
Mao Schleich  M 04/17/60  Cancer
Goldie Moultrie  F 04/05/96  None
Silva Rizzo  F 10/26/78  Amyloidosis
Leatha Papenfuss  F 10/15/97  CREST
Vita Sabb  F 05/28/87  Autism
Alyce Ugarte  F 12/21/64  HIV
Ela Prout  F 12/05/57  Autism
Mohamed Buchannon  M 07/24/91  Caner
Lael Stall  M 12/05/97  None

The first column is a name, the second is gender, third is birthdate, fourth is disease. The name is supposed to be the key while the other three columns are the values.

Also how would I allow the user to change information and output information to another file?


Moderator's Comments:
Mod Comment Please use CODE tags for data as well as required by forum rules!
Hello Eric1,

Let me give you a few examples.
If I were to implement at face value what you are asking this would be the result:
Code:
$ cat read_list.pl
#!/usr/bin/perl
#
use strict;
use warnings;
use Data::Dumper;

my %patient;
while(<>) {
    my @pair = /^(\w+\s\w+)\s+(.+)$/;
    $patient{$pair[0]} = $pair[1];
}
print Dumper \%patient;


Output:

Code:
$VAR1 = {
          'Leatha Papenfuss' => 'F 10/15/97  CREST',
          'Celena Standard' => 'F 01/24/94 Cancer ',
          'Vita Sabb' => 'F 05/28/87  Autism',
          'Jeniffer Orlowski' => 'F 06/24/86 None',
          'Alyce Ugarte' => 'F 12/21/64  HIV',
          'Silva Rizzo' => 'F 10/26/78  Amyloidosis',
          'Lael Stall' => 'M 12/05/97  None',
          'Mao Schleich' => 'M 04/17/60  Cancer',
          'Brent Koehler' => 'M 12/05/97  HIV',
          'Mohamed Buchannon' => 'M 07/24/91  Caner',
          'Ela Prout' => 'F 12/05/57  Autism',
          'Goldie Moultrie' => 'F 04/05/96  None'
        };

But I suspect that's not what you want. Probably, you would like something more like:
Code:
$ cat read_names.pl
#!/usr/bin/perl
#
use strict;
use warnings;
use Data::Dumper;

my %patient;
while(<>) {
    my @record = split;
    $patient{"@record[0..1]"} = {
        'gender' => "$record[2]",
        'birthday' => "$record[3]",
        'disease' => "@record[4..$#record]",
    }

}
print Dumper \%patient;

Output:
Code:
$ perl read_names.pl people.list
$VAR1 = {
          'Leatha Papenfuss' => {
                                  'disease' => 'CREST',
                                  'birthday' => '10/15/97',
                                  'gender' => 'F'
                                },
          'Celena Standard' => {
                                 'disease' => 'Cancer',
                                 'birthday' => '01/24/94',
                                 'gender' => 'F'
                               },
          'Vita Sabb' => {
                           'disease' => 'Autism',
                           'birthday' => '05/28/87',
                           'gender' => 'F'
                         },
          'Jeniffer Orlowski' => {
                                   'disease' => 'None',
                                   'birthday' => '06/24/86',
                                   'gender' => 'F'
                                 },
          'Alyce Ugarte' => {
                              'disease' => 'HIV',
                              'birthday' => '12/21/64',
                              'gender' => 'F'
                            },
          'Silva Rizzo' => {
                             'disease' => 'Amyloidosis',
                             'birthday' => '10/26/78',
                             'gender' => 'F'
                           },
          'Lael Stall' => {
                            'disease' => 'None',
                            'birthday' => '12/05/97',
                            'gender' => 'M'
                          },
          'Mao Schleich' => {
                              'disease' => 'Cancer',
                              'birthday' => '04/17/60',
                              'gender' => 'M'
                            },
          'Brent Koehler' => {
                               'disease' => 'HIV',
                               'birthday' => '12/05/97',
                               'gender' => 'M'
                             },
          'Mohamed Buchannon' => {
                                   'disease' => 'Caner',
                                   'birthday' => '07/24/91',
                                   'gender' => 'M'
                                 },
          'Ela Prout' => {
                           'disease' => 'Autism',
                           'birthday' => '12/05/57',
                           'gender' => 'F'
                         },
          'Goldie Moultrie' => {
                                 'disease' => 'None',
                                 'birthday' => '04/05/96',
                                 'gender' => 'F'
                               }
        };

However, depending of the real input, that might have a serious flaw. Name plus last name is not unique enough. There is the strong possibility that two or more entries might contain the same name last-name record even when the data would mean different people. Translation: you loose data, since a hash will keep only the last read.

Adding the birthday to the id might help to prevent that. Here's a modification of the previous code, using a modified input to prove handling of name collision and multi-word decease:

INPUT:


Code:
$ cat name.list
Celena Standard  F 01/24/94 Cancer
Jeniffer Orlowski  F 06/24/86 None
Brent Koehler  M 12/05/97  HIV
Mao Schleich  M 04/17/60  Cancer
Goldie Moultrie  F 04/05/96  None
Silva Rizzo  F 10/26/78  Amyloidosis
Leatha Papenfuss  F 10/15/97  CREST
Vita Sabb  F 05/28/87  Autism
Alyce Ugarte  F 12/21/64  HIV
Ela Prout  F 12/05/57  Autism
Silva Rizzo  F 22/5/81  Dissociative Indentity Disorder
Mohamed Buchannon  M 07/24/91  Caner
Lael Stall  M 12/05/97  None

Code:
$ cat read_names.pl
#!/usr/bin/perl
#
use strict;
use warnings;
use Data::Dumper;

my %patient;
while(<>) {
    my @record = split;
    $patient{"@record[0..1,3]"} = {
        'gender' => "$record[2]",
        'birthday' => "$record[3]",
        'disease' => "@record[4..$#record]",
    }

}
print Dumper \%patient;

Output:

Code:
$ perl read_names.pl name.list
$VAR1 = {
          'Ela Prout 12/05/57' => {
                                    'disease' => 'Autism',
                                    'birthday' => '12/05/57',
                                    'gender' => 'F'
                                  },
          'Silva Rizzo 10/26/78' => {
                                      'disease' => 'Amyloidosis',
                                      'birthday' => '10/26/78',
                                      'gender' => 'F'
                                    },
          'Mohamed Buchannon 07/24/91' => {
                                            'disease' => 'Caner',
                                            'birthday' => '07/24/91',
                                            'gender' => 'M'
                                          },
          'Vita Sabb 05/28/87' => {
                                    'disease' => 'Autism',
                                    'birthday' => '05/28/87',
                                    'gender' => 'F'
                                  },
          'Mao Schleich 04/17/60' => {
                                       'disease' => 'Cancer',
                                       'birthday' => '04/17/60',
                                       'gender' => 'M'
                                     },
          'Brent Koehler 12/05/97' => {
                                        'disease' => 'HIV',
                                        'birthday' => '12/05/97',
                                        'gender' => 'M'
                                      },
          'Jeniffer Orlowski 06/24/86' => {
                                            'disease' => 'None',
                                            'birthday' => '06/24/86',
                                            'gender' => 'F'
                                          },
          'Lael Stall 12/05/97' => {
                                     'disease' => 'None',
                                     'birthday' => '12/05/97',
                                     'gender' => 'M'
                                   },
          'Leatha Papenfuss 10/15/97' => {
                                           'disease' => 'CREST',
                                           'birthday' => '10/15/97',
                                           'gender' => 'F'
                                         },
          'Silva Rizzo 22/5/81' => {
                                     'disease' => 'Dissociative Indentity Disorder',
                                     'birthday' => '22/5/81',
                                     'gender' => 'F'
                                   },
          'Alyce Ugarte 12/21/64' => {
                                       'disease' => 'HIV',
                                       'birthday' => '12/21/64',
                                       'gender' => 'F'
                                     },
          'Goldie Moultrie 04/05/96' => {
                                          'disease' => 'None',
                                          'birthday' => '04/05/96',
                                          'gender' => 'F'
                                        },
          'Celena Standard 01/24/94' => {
                                          'disease' => 'Cancer',
                                          'birthday' => '01/24/94',
                                          'gender' => 'F'
                                        }
        };

Note:
The code assumes that the patient will always be name and last-name and not a variation like name alone or name, middle name, last-name, etc...

Once you decide and practice with extracting the data based on actual data, you could show your effort on it and follow up with your second question.

By the way, I hope the example does not contain real people's names and birthdays that you happen to be trusted with. That would be a 'terrible' thing to post.

Last edited by Aia; 05-20-2017 at 02:22 PM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

When a file is created where does unix store the info?

When a file is created where does unix store the information about the file? (5 Replies)
Discussion started by: goodmis
5 Replies

2. UNIX for Dummies Questions & Answers

Binary txt file received when i use uuencode to send txt file as attachment

Hi, I have already read a lot of posts on sending attachments in unix...but none of them were of help for my problem...so here goes.. i wanna attach a text file and send to a mail id..used the following code : uuencode "$File1" "$File1" ;|mail -s "$Mail_sub" abc@abc.com it works... (2 Replies)
Discussion started by: ash22
2 Replies

3. Shell Programming and Scripting

Open txt and store line in a var

Hi I realy need to find a way to do a script that does the following: search a word in a txt file then store on a var the letters that are on the right like this is an exemple, I want to store from here ...abcdefg... so I need a var that receive ...abcdefg... Is that possible?... (8 Replies)
Discussion started by: ruben.rodrigues
8 Replies

4. UNIX for Advanced & Expert Users

Perl loop txt and check if a hash key

Hi, The task i have to do is to 1- create a database contains the Names .run the query and store results in hash make the Name field is the hash key 2- in the same time i have a txt which i will loop through it word by word and check for each word if a hash key ( compare it with the Names in... (0 Replies)
Discussion started by: eng_shimaa
0 Replies

5. Shell Programming and Scripting

Generate a mail when you add info to a txt file

Hi, I have an application's log file: /var/log/logfile which is feeded from time to time due to an application. This file contains data, what I want is: -Whenever some new data is copied to /var/log/logfileI want to generate an email to root BUT only with the new added data in the body.... (6 Replies)
Discussion started by: iga3725
6 Replies

6. Shell Programming and Scripting

How to store the sql query output into txt file?

Hi I want ot save SQL query result in one txt file. for that i have written one code line sqlplus -s $dbstring @/usr/local/bin/sched/nightly_Cronjob/exec_123.sql >> /usr/local/bin/sched/nightly_Cronjob/result.txt but it is not working . database : Oracle so please advice me how can i... (7 Replies)
Discussion started by: Himanshu_soni
7 Replies

7. Programming

store information in hash and display number of keys.

I am trying to store this information (info and number) in hash. number is the key and info is value in a hash.i shown my code below. #!/usr/bin/perl use warnings; use strict; use XML::LibXML::Reader; my $file;open $file, 'formal.xml'); my $reader =... (0 Replies)
Discussion started by: veerubiji
0 Replies

8. Shell Programming and Scripting

Store output of DB Cursor to a txt file

I am writing a cursor to select values from 3 tables. I want to store these values in a txt file which I will be sending via ftp. I am able to store the results of simple select queries to the txt file. but I am not sure how to store the values when using a cursor. I have given the sql query below.... (1 Reply)
Discussion started by: naveensraj
1 Replies

9. Shell Programming and Scripting

Store a column from an excel sheet (exported to txt) into a variable

I typically pull a bunch of data via SQL that lists a bunch of users and the server on which they want to access, as well as other attributes, in one row of an excel sheet and the number of rows is directly proportionate to the number of users. I'm trying to write a loop to read each line of the... (2 Replies)
Discussion started by: MaindotC
2 Replies

10. Shell Programming and Scripting

Call a Perl script within a bash script and store the ouput in a .txt file

I'm attempting to write a bash script that will create a network between virtual machines. It accepts three arguments: an RSpec that describes the network topology, and two list of machines (servers and clients). I have a (working) Perl script that I want to call. This Perl script takes an RSpec... (6 Replies)
Discussion started by: mecaka
6 Replies
All times are GMT -4. The time now is 05:22 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy