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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
09-22-2005, 06:25 PM
|
#1
|
Member
Registered: Aug 2004
Location: London, UK
Distribution: Ubuntu 10.10, ubuntu 11.04, suse 9.2, OSX
Posts: 259
Rep:
|
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";
}
|
|
|
09-22-2005, 09:24 PM
|
#2
|
Member
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217
Rep:
|
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.
|
|
|
09-23-2005, 12:25 AM
|
#3
|
Member
Registered: Aug 2004
Location: London, UK
Distribution: Ubuntu 10.10, ubuntu 11.04, suse 9.2, OSX
Posts: 259
Original Poster
Rep:
|
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
|
|
|
09-23-2005, 03:14 AM
|
#4
|
Member
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217
Rep:
|
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".
|
|
|
All times are GMT -5. The time now is 03:50 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|