CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Taking input multiples times and storing into array

 



tbone587
User

Mar 4, 2011, 6:09 AM

Post #1 of 16 (2271 views)
Taking input multiples times and storing into array Can't Post

Hey everyone I have a question that as of now I have no idea how to accomplish. I am taking input of a number, lets say 3. Then I want to take more input asking for a username, and have it ask for a different username 3 times. Then it will store each input into an array that I can manipulate later. So for example, I enter 3, then it prompts me to enter a user name 3 times. It will then store the input into a variable.


rovf
Veteran

Mar 4, 2011, 8:17 AM

Post #2 of 16 (2266 views)
Re: [tbone587] Taking input multiples times and storing into array [In reply to] Can't Post

You forgot to tell where exactly you have a problem. For example, you could post the code you have so far and explain where you get stuck; or you write the solution which you intend to implement in pseudo-code (or in another programming language you are familiar to), and explain where you have problems translating it into Perl. But if you just tell the programming task, and give no other information, it's difficult to help.

Well, there is one thing I can help: No matter how you are going to implement this, the first three statements of every Perl program you write should be

use strict;
use warnings FATAL => qw(all);
use diagnostics;

Since you are a beginner in Perl, this will help you a lot. Once you get more experienced, you likely will want to remove the diagnostics pragma.


tbone587
User

Mar 4, 2011, 8:36 AM

Post #3 of 16 (2261 views)
Re: [rovf] Taking input multiples times and storing into array [In reply to] Can't Post

Please reference the following code. After it asks for how many users are being monitored, i want it to ask you to enter the first and last name of the monitored user that many times. Here is the example code


Code
my $EA_Mac_Counter = 0; 
until ($EA_Mac =~ /^(a|b|c|d|e|f|A|B|C|D|E|F|0|1|2|3|4|5|6|7|8|9){12}$/) #Checks for a valid Polycom MAC
{
if ($EA_Mac_Counter < 1)
{
print "Please enter the MAC address of the Executive Assistant: "; #Executive Assistant input
}


elsif ($EA_Mac_Counter > 2)
{
print "You have entered an invalid MAC address too many times! Script is closing....\n";
exit;
}

else
{
print "The MAC address you entered is invalid! Please enter again: ";
}

chomp($EA_Mac=<STDIN>); #This chomps the data for the if statement and the else statement.
$EA_Mac = lc($EA_Mac); #Converts MAC address to lower case
$EA_Mac_Counter++;
}

print "How many users are being monitored? "; #Amount of monitored users input
chomp($Mon_User_Count=<STDIN>);

print "Please enter the first and last name of monitored user: "; #Monitored user name input
chomp($Mon_UserName=<STDIN>);



FishMonger
Veteran / Moderator

Mar 4, 2011, 4:09 PM

Post #4 of 16 (2246 views)
Re: [tbone587] Taking input multiples times and storing into array [In reply to] Can't Post


Code
my @Mon_UserName; 

for my $i (1..$Mon_User_Count) {
print "Please enter the first and last name of monitored user: ";
chomp($Mon_UserName[$i-1]=<STDIN>);
}



FishMonger
Veteran / Moderator

Mar 5, 2011, 5:34 AM

Post #5 of 16 (2232 views)
Re: [tbone587] Taking input multiples times and storing into array [In reply to] Can't Post

Please post your responses here instead of a PM.


Quote
thanks a lot for your help. When I go to print out the input how to I print each one? For instance how would I print the second input? Also can you explainm the $i-1 part? I really appreciate any help.


Use a for/foreach loop to output each element of the array.

Arrays are zero indexed, meaning the first element is index 0. Since the for loop I used starts at 1 instead of 0, you need to subtract 1 from $i to access the proper index.

The second input, which is the second array element, is accessed as:

Code
$Mon_UserName[1]



(This post was edited by FishMonger on Mar 5, 2011, 5:39 AM)


tbone587
User

Mar 8, 2011, 7:53 AM

Post #6 of 16 (2192 views)
Re: [FishMonger] Taking input multiples times and storing into array [In reply to] Can't Post

I am having an issue with my next phase of this script. After I enter the amount of users to be monitored, the script should ask for you to enter the mac address for those inputted users, as well as checking to see if they are valid. For some reason it is always reading the mac is invald. Can someone please review my code and let me know what I am doing wrong, and if there is an easier/cleaner way?


Code
for my $Mon_UserName_Loop (1..$Mon_User_Count) 
{
print "Please enter the first and last name of monitored user: "; #Monitored user name input
chomp($Mon_UserName[$Mon_UserName_Loop-1]=<STDIN>); #Access proper Array Index
}


my $UserMac_Counter = 0;
for my $Mon_UserMac_Loop (1..$Mon_User_Count)
{
until ($Mon_UserMac[$UserMac_Counter] =~ /^(a|b|c|d|e|f|A|B|C|D|E|F|0|1|2|3|4|5|6|7|8|9){12}$/)
{

if ($UserMac_Counter < 1)
{
print "Please enter the MAC address of $Mon_UserName[$UserMac_Counter]: "; #Monitored user MAC input
}

elsif ($UserMac_Counter > 2)
{
print "You have entered an invalid MAC address too many times! Script is closing....\n";
exit;
}

else
{
print "The MAC address you entered is invalid! Please enter again: ";
}

chomp($Mon_UserMac[$Mon_UserMac_Loop-1]=<STDIN>);
#$Mon_UserMac = lc($Mon_UserMac); #Converts MAC Address to lower case
$UserMac_Counter++;
}
}



FishMonger
Veteran / Moderator

Mar 8, 2011, 8:09 AM

Post #7 of 16 (2189 views)
Re: [tbone587] Taking input multiples times and storing into array [In reply to] Can't Post

1) Don't use the alternation in the regex.

2) Use a character class.

Code
/^[abcdef\d]{12}$/i



tbone587
User

Mar 8, 2011, 8:14 AM

Post #8 of 16 (2187 views)
Re: [FishMonger] Taking input multiples times and storing into array [In reply to] Can't Post

That is definately cleaner way to do the regex, but I dont think thats where my issue is. I have used the other regex for other instances and it works fine. I think there is something wrong the logic.


FishMonger
Veteran / Moderator

Mar 8, 2011, 5:37 PM

Post #9 of 16 (2177 views)
Re: [tbone587] Taking input multiples times and storing into array [In reply to] Can't Post

First, is the mac address actually being entered in the format that you expect? When asking for a mac address, I would not expect it to be in the format that you're expecting.

These are the 2 common formats I would expect (ignoring case).
1A:4B:D6:75:C1:2C
1A-4B-D6-75-C1-2C

and a less common
1A;4B;D6;75;C1;2C

If you want to test for a single format, then I would first normalize the input then test the value.

The logic of your until block could be improved. To start with I'd adjust the if/elsif/else block by taking out the first print statement and put that prior to the until block. Next, I'd change the until loop to a for loop and exit out of it early as needed.


tbone587
User

Mar 8, 2011, 6:29 PM

Post #10 of 16 (2175 views)
Re: [FishMonger] Taking input multiples times and storing into array [In reply to] Can't Post

Well I am testing to make sure my error handling is working properly and it is not. No matter what I enter it tells me its invalid:( Any help would be greatly appreciated.


Karazam
User

Mar 9, 2011, 12:27 AM

Post #11 of 16 (2163 views)
Re: [tbone587] Taking input multiples times and storing into array [In reply to] Can't Post

I couldn't readily see what's wrong with your code, but if you dont mind I'd like to suggest
a somewhat different approach. Not thoroughly tested, but it seems to work.


Code
#!/usr/bin/perl  
use strict;
use warnings;

print "Enter nr of users: ";
my $nr = <>;

my @a;

for (1 .. $nr) {
print "Enter name of user nr $_\n";
my $name = <>;
chomp $name;
push @a, $name;
}

# pieces of the MAC regex
my $d = "[0-9A-Fa-f]";
my $dd = "$d$d";

my $max_tries = 3;

for my $i ( 0 .. $#a ) {
my $tries = 0;
LOOP: {
print "Enter MAC address for user $a[$i]\n";
my $mac = <>;
chomp $mac;

if ( $mac =~ /^$dd([:;-])$dd(\1$dd){4}$/ ) {
print "$a[$i] has MAC $mac\n";
}
else {
$tries++;
if ( $tries < $max_tries ) {
print "False MAC, try again.\n";
redo LOOP;
}
else {
print "Too many false tries. Bye.\n";
exit;
}
}
}
}


Hope this helps. Smile


(This post was edited by Karazam on Mar 9, 2011, 1:43 AM)


tbone587
User

Mar 9, 2011, 10:36 AM

Post #12 of 16 (2134 views)
Re: [Karazam] Taking input multiples times and storing into array [In reply to] Can't Post

Karazam, thank you so much! I slightly modified that for me needs and it worked great. My next step is I have to open/edit all mac files that are inputted and store information from those files to be printed out later. Can you point me in the right direction? For instance, if I enter 0004f2e724f3, and 0004f2e724f2 as a mac address it will try to open 0004f2e724f3.cfg and 0004f2e724f2.cfg. Then store information from those files into variables.


Karazam
User

Mar 9, 2011, 11:29 AM

Post #13 of 16 (2131 views)
Re: [tbone587] Taking input multiples times and storing into array [In reply to] Can't Post

Not sure if you mean to read the whole file into a variable first,
or extract info and put that into a variable.
First case, if $mac holds the MAC address, then


Code
open my $fh, '<', "/path/to/$mac.cfg" or die $!; 
my @cfg = <$fh>;
for my $line ( @cfg ) {
# do stuff with $line
}
close $fh;


In the second case then rather


Code
open my $fh, '<', "/path/to/$mac.cfg" or die $!; 
while (<$fh>) {
# each line is now in $_, do stuff with that
}
close $fh;


The three argument form of open with a lexical filehandle is the preferred way,
although you may often see the old "open(FH, '< filename')".
And always have a die after so you know if things go wrong.
See perlopentut for more details.


(This post was edited by Karazam on Mar 9, 2011, 11:32 AM)


tbone587
User

Mar 9, 2011, 12:07 PM

Post #14 of 16 (2128 views)
Re: [Karazam] Taking input multiples times and storing into array [In reply to] Can't Post

Thank you for the quick response. Here is a little more detail on what I want to do:


Example
- Enter the amount of users being monitored: 2
- Enter the name of monitored user: Bryan
- Enter the name of monitored user: Tristan
- Enter the mac Address for Bryan: 0004f2e4aea6
($Monitored_Macs[0])

- Enter the mac Address for Tristan: 0004f2e4aea5 ($Monitored_Macs[1])

(Open 0004f2e4aea6.cfg)
Store the reg.1.address & reg.1.auth.password into an array
(Close)

(Open 0004f2e4aea5.cfg)
Store the reg.1.address & reg.1.auth.password into an array
(Close)


tbone587
User

Mar 12, 2011, 7:57 PM

Post #15 of 16 (2097 views)
Re: [Karazam] Taking input multiples times and storing into array [In reply to] Can't Post

Thank you so much again for all of your help. This code has helped me go a long way. Can you quickly explain the following line on what the # signifies:


Code
for my $i ( 0 .. $#a ) {



Karazam
User

Mar 12, 2011, 11:48 PM

Post #16 of 16 (2092 views)
Re: [tbone587] Taking input multiples times and storing into array [In reply to] Can't Post

$#a is the index of the last element in @a.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives