Trying to get Understanding on filehandle and split function in Perl
Okay I'm still working on my perl studies when I can and I am a little confused on how this is working.
PHP Code:
Code:
Smith,John (248)-555-9430 jsmith@aol.com Now this is where I'm not quite understanding on line 4,5,and 6. PHP Code:
line 1: open customers.txt for reading line 2: begins the loop untill it run out of lines in customers.txt line3: removes all the new lines off of each line line 4: take something like Quote:
now from what I understand the split is splitting on pattern whie spaces to the end of line and [1,2] Is 1 for the number and 2 for the email. So does that mean 0 would be for name? Really I just need a better explaination on how line 2-6 work out so I can modify the code to search by name. |
You might need to get [0] , split that on the , amd then match on the result
|
Building a name search function into perl program
grr I can't edit the title of the post.
Okay I got this code allow me to search by name but I have to put a whitespace in front of all lines in customers.txt PHP Code:
Quote:
|
Code:
($name, $number, $email) = (split(/s+/, $_))[1,2,3]; Code:
($name, $number, $email) = (split(/\s+/, $_)); So you need something like Code:
$Names{$number} = $name; Code:
if ($name) { The original code uses email and phone as keys for two different hashes. I would say this is bad because you could have two customers with the same phone number (different people in the same building). Anyway it makes more sense to have a single hash for a single object - but you'd need to get to grips with hashes of hashes. |
Actually I got it to work last night with this
PHP Code:
Code:
Smith,John (248)-555-9430 jsmith@aol.com Thanks for the help |
You are retrieving the components of the line as a list construct. See http://perldoc.perl.org/perldata.html for more info on lists.
Also see http://perldoc.perl.org/perlvar.html for a better understanding of $_. You really need to use one hash to contain all the elements of a customer's record. And then build an array of hashes like this : Code:
#!/usr/bin/perl the second occurrence of John Smith. See http://perldoc.perl.org/perldsc.html which discusses generating arrays of hashes. Each element of your final array will be a hash (aka an associative array) containing one person's details. Then rework the remainder of your code to use these data structures. Good luck :) |
BTW.
Code:
split(/s+/, $_ (It requires more typing, most un-perl) Code:
split split by default splits $_ on whitespace. And furthermore, to split on whitespace you only need to do split " " as this is a special case in perl. split |
Quote:
|
Quote:
|
Hi,
you are losing hashes if you don't create a new hash for every record! Otherwise you will always use the same hash and push the same hash with new data onto the array which results in an array with all entries the same. The normal way to use an array of hashes in perl is to use references to hashes. This means you create a new hash for every record and push a reference to the new hash onto the array. Markus |
Quote:
The code I gave earlier declares a new hash for each record, from within the file reading loop. After all, you've one record per line and you need a hash for each record, yes? Plus you then need to push the hash Code:
push @CustomerRecords, { %CustomerRecord }; Code:
push @CustomerRecords, \%CustomerRecord ; You could just use a reference within the loop, Code:
while(<PH>) { Code:
$CustomerRecords[0]->{'Name'} Code:
$CustomerRecords->[0]->{'Name'} |
Quote:
|
Quote:
http://www.linuxquestions.org/questi...41#post4535941 Can a moderator perhaps lock this thread so all new post go to the other? |
done
|
All times are GMT -5. The time now is 09:09 PM. |