LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-22-2005, 06:25 PM   #1
PB0711
Member
 
Registered: Aug 2004
Location: London, UK
Distribution: Ubuntu 10.10, ubuntu 11.04, suse 9.2, OSX
Posts: 259

Rep: Reputation: 30
Perl and Hash automatic


Hello,
I am still pretty new to perl and i cannot figure out how to make a hash without physically writting it into the program. Here is the basic of my code (at the bottom). What i am trying to do is find in a file names and place them into a hash. The file will look like;
/firstname{John}
/lastname{smith}
/address{Geneva}
----
/lastname{ ...etc

If someone could just tell me how to make a hash automaticly in a loop that would be all i need. If someone wants to look at my script that would be even better. Thank you in advance

Here is the code
-----------------------------
#!/usr/bin/perl -w
use strict;

my $In = "listofnames.dat";
open IN, "$In" or die "Problem opening file, $! \n";
my $file;
while (my $line =<IN>){
$file .= $line; # opens file & parse's it to $file
}
close IN;
#print "This is the \$file \t$file \n"; # a check

my @lookup=split(/----/, "$file"); # to split $file by 4 - so each entry is seprate
#print "@lookup \n"; # a check

my %address; # create global hash
my %firstname;
for (my $i=0; $i < @lookup;$i++){
my $where_first = index($lookup[$i], "firstname"); # Uses the indez command
my $where_last = index($lookup[$i], "lastname"); # to find the first char of ""
my $where_add = index($lookup[$i], "address"); #

my $finish_First =index($lookup[$i], "}", $where_first); # finds "}" starting from above
my $finish_Last=index($lookup[$i],"}",$where_last); # $where_? and places it as
my $finish_Add=index($lookup[$i], "}", $where_add); # the last possible char

my $startF = index($lookup[$i], "{", $where_first);# Finds where the name starts
my $startL = index($lookup[$i], "{", $where_last); # using the index command
my $startA = index($lookup[$i], "{", $where_add);

my $firstN = substr($lookup[$i],$where_first+10, $finish_First - ($startF+1)); # finally make the
my $lastN = substr($lookup[$i], $where_last+9, $finish_Last - ($startL+1)); # name and puts into
my $address = substr($lookup[$i], $where_add+8, $finish_Add - ($startA+1)); # string
#print "First\t$firstN\nLastN\t$lastN\nAddress\t$address\n\n"; # a check

$address{ $lastN } =$address; # should make hash
$firstname{ $lastN } = $firstN; #should make hash ????
}
my @k = keys %firstname; #asks for all keys of the hash
my @v = values %firstname;
print "______First Name Hash______\n"; # just for effect
for (my $j=0;$j<@k;$j++){
print "$k[$j]\t$v[$j]\n"; # some printing of the hash
}
#----------same as above but with other hash
my @ka = keys %address;
my @va = values %address;
print "\n _____Address Hash______\n";
for (my $i=0;$i<@ka;$i++){
print "$ka[$i]\t$va[$i]\n";
}
 
Old 09-22-2005, 09:24 PM   #2
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 30
Regular expressions to the rescue! (Also, please post code with the code tags!)

Code:
#!/usr/bin/perl

my %address;
my %firstname;

# this is the input record separator special variable
$/ = "----\n";

while (<>) {
  m/lastname{(.+)}/;
  my $last = $1;

  m/firstname{(.+)}/;
  my $first = $1;

  m/address{(.+)}/;
  my $addr = $1;

  $firstname{$last} = $first;
  $address{$last} = $addr;
}

# print for verification
while ( my ($k,$v) = each %firstname ) {
    print "$k => $v\n";
}
while ( my ($k,$v) = each %address ) {
    print "$k => $v\n";
}

Last edited by puffinman; 09-22-2005 at 09:38 PM.
 
Old 09-23-2005, 12:25 AM   #3
PB0711
Member
 
Registered: Aug 2004
Location: London, UK
Distribution: Ubuntu 10.10, ubuntu 11.04, suse 9.2, OSX
Posts: 259

Original Poster
Rep: Reputation: 30
Oh ok! cool thank you, I see. But what do you mean by code tags? I though i had with the # stuff here to explain line

any who thanks,

PB
 
Old 09-23-2005, 03:14 AM   #4
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 30
Code tags make your code more readable on the forum.

Code:
This text is inside a code tag.
Use the editing buttons directly above where you're typing when you post a message; one of them says "Code".
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generating hash in perl ? MikeFoo1 Programming 3 05-21-2005 07:03 PM
Passing hash of hashes as a parameter (Perl) rose_bud4201 Programming 8 04-21-2005 08:18 PM
Perl: hash tying amnesty_puppy Programming 1 01-18-2005 12:03 AM
Hash-sort like in PERL in C++ nyk Programming 4 06-11-2004 09:40 AM
renaming ENV hash in Perl nbp Programming 4 11-01-2003 04:07 AM


All times are GMT -5. The time now is 02:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration