LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-06-2009, 09:14 PM   #1
nobody123
LQ Newbie
 
Registered: Feb 2009
Location: Toronto, ON
Distribution: Fedora 10
Posts: 16

Rep: Reputation: 0
Sorting through associative array with user input


Hi there. Thanks for taking the time to read this question. It's probably something I'm missing on my part. Anyways, onto the question.

I'm working on an address book and storing the records via hashes. I want the users to be able to search for an address by using the contact number. Here's the code I came up with:

Code:
print "\nEnter the number you wish to search for";
$phoneSearch=<STDIN>;
chomp ($phoneSearch);
@LIST = grep($phoneSearch ,%addresses); #searches for records that have
                                        #the specified number and
                                        #assigns it to @LIST
                                         
if (@LIST != null) #checks if results are found
{
print "@LIST"; $#LIST = -1; #empties out array for reuse
} else {
print "No records found...";
}
The problem with this code? The array doesn't empty itself after it prints the array. So even though the user specifies a phone number not in the records, it will output the array regardless. Any help will be greatly appreciated.
 
Old 04-06-2009, 09:26 PM   #2
grunt547
Member
 
Registered: Nov 2006
Distribution: FreeBSD, OpenBSD
Posts: 30

Rep: Reputation: Disabled
To empty a Perl array:
Code:
@LIST = ();
Also your use of the bareword 'null' looks weird. To check if the list holds anything, you can just say:
Code:
if (@LIST) { ... }
Try adding "use strict;" to the beginning to catch things like that.
 
Old 04-06-2009, 11:39 PM   #3
nobody123
LQ Newbie
 
Registered: Feb 2009
Location: Toronto, ON
Distribution: Fedora 10
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grunt547 View Post
To empty a Perl array:
Code:
@LIST = ();
Also your use of the bareword 'null' looks weird. To check if the list holds anything, you can just say:
Code:
if (@LIST) { ... }
Try adding "use strict;" to the beginning to catch things like that.
Okay, after a ton of errors (mostly due to the fact that I hadn't declared anything) It still does not function correctly. I implemented your code but it just doesn't empty it (the LIST array). Thanks for the help though
 
Old 04-08-2009, 02:55 PM   #4
nobody123
LQ Newbie
 
Registered: Feb 2009
Location: Toronto, ON
Distribution: Fedora 10
Posts: 16

Original Poster
Rep: Reputation: 0
Update

Yeah, it works now. I modified my code a bit. There were a few errors (e.g. not greping properly). I'll post my code up for anyone who is interested...


Code:
print "\nEnter the number you wish to search for: ";
my $phoneSearch=<STDIN>;
chomp ($phoneSearch);
my @LIST = grep(/$phoneSearch/, %addresses);


if (@LIST)
{
   print "@LIST";
   print "\nPerform another search?(Y/N): ";
   $menuExit=<STDIN>;
   chomp ($menuExit);
   @LIST = ();
}
else
{
 print "\nNo records found...\n";
 print "\nPerform another search?(Y/N): ";
 $menuExit=<STDIN>;
 chomp ($menuExit);
}
 
  


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
C++: user input to an int array? andrw123 Programming 12 09-23-2008 12:55 AM
awk: associative array key names h/w Programming 2 10-17-2007 01:58 AM
two dimensional array sorting in C/C++ George2 Programming 10 11-19-2006 09:29 PM
I need help sorting an array in PHP! socceroos Programming 14 05-09-2006 03:37 AM
exchange value and key per element in associative array rblampain Programming 3 04-02-2006 10:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:58 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration