spickles has asked for the wisdom of the Perl Monks concerning the following question:
Monks -
I am trying to perform a regex to detect whether a user entered a space into a word. I read the input from STDIN and then split to an array. I step through the array and look for an entry that is a space. I have tried splitting with //, / /, /''/, /' '/,/\s/,/ */ and none of them appear to work correctly. I'm sure it really is working, it's just something I'm missing. If I print the stored array, I only see the first element, and not the others. I have also run an example I found on the web, and when it prints it shows all of the elements but VERY slowly. The subroutine I'm having a problem with is vlan_name. Is there something maybe going on with buffering? Here's the full code:
I am trying to perform a regex to detect whether a user entered a space into a word. I read the input from STDIN and then split to an array. I step through the array and look for an entry that is a space. I have tried splitting with //, / /, /''/, /' '/,/\s/,/ */ and none of them appear to work correctly. I'm sure it really is working, it's just something I'm missing. If I print the stored array, I only see the first element, and not the others. I have also run an example I found on the web, and when it prints it shows all of the elements but VERY slowly. The subroutine I'm having a problem with is vlan_name. Is there something maybe going on with buffering? Here's the full code:
#!c:/perl/bin/perl use strict; use warnings; use Net::Ping::External qw(ping); my $mgt_address = ''; my $dhcp_pool = ''; my $dhcp_network = ''; my $dhcp_mask = ''; my $dhcp_dgw = ''; my $wism_slot = ''; my $service_vlan = ''; my $native_vlan = ''; my $slot_list = ''; my @slots; my $current_host = ''; my @ip_hosts; my $vlan_list = ''; my %vlans; my @network; my $apply_status = ''; my $i; my $status; my $MYFILE; my $quit = 'n'; my $vlan_number; my $vlan_name; my @pieces; my @vlan_numbers; my @vlan_names; my @different; our $config_string; vlans(); do_split(); system ("cls"); sub vlan_number { if ($_[0]) { print "Enter the WiSM $_[0] VLAN:\n"; if ($_[0] eq 'native') { chomp ($native_vlan = <>); $vlan_number = $native_vlan; } else { chomp ($service_vlan = <>); $vlan_number = $service_vlan; } } else { print "Enter the VLAN number:\n"; chomp ($vlan_number = <>); } @pieces = split (//,$vlan_number); #foreach (@pieces) { # print "$_ \n"; #} foreach (@pieces) { if ($_ =~ /[a-zA-Z]/) { print "Your VLAN number can only contain digits 0-9.\n" +; vlan_number(); } } if ($_[0] eq 'native') { if (($vlan_number ~~ [1..1001]) || ($vlan_number ~~ [1006..4 +094])) { return $vlan_number; } else { print "VLAN number $vlan_number is invalid.\n"; print "Valid VLANs are 1-1001 and 1006-4094.\n"; vlan_number(); } } if (($vlan_number ~~ [2..1001]) || ($vlan_number ~~ [1006..4094]) +) { return $vlan_number; } else { print "VLAN number $vlan_number is invalid.\n"; print "Valid VLANs are 2-1001 and 1006-4094.\n"; vlan_number(); } } #End vlan_number sub vlan_name { print "Enter the VLAN name (without spaces):\n"; chomp ($vlan_name = <>); print "You entered: $vlan_name\n"; @different = split (/ */,$vlan_name); print join(':', split(/ */, 'hi there')), "\n"; print join(':', split(/ */, $vlan_name)), "\n"; for (my $j=0; $j<scalar(@different); $j++) { print "$different[$j] \n"; if ($different[$j] =~ /[^a-zA-Z0-9_-]/) { print "WARNING:\n"; print "Your VLAN name can only contain letters, numbers +, underscore and hyphen.\n"; print "Do not use special characters [~`!@#$%^&*()+=?\| +[]{};:'\",.<>]\n"; vlan_name(); } if ($different[$j] =~ /\s/) { print "WARNING: Your VLAN name cannot contain spaces.\ +n"; vlan_name(); } else { return; } } } sub vlans { vlan_number("native"); push (@vlan_numbers, $vlan_number); push (@vlan_names, "WiSM_Native"); vlan_number("service"); push (@vlan_numbers, $vlan_number); push (@vlan_names, "WiSM_Service"); print "Setup data VLANs. Enter 'q' when finished.\n\n"; while ($quit ne 'q') { #$array{$key} = $value; vlan_number(); vlan_name(); push (@vlan_numbers, $vlan_number); push (@vlan_names, $vlan_name); print "Add another? 'q' to quit, ENTER to continue...\n\n"; chomp ($quit = <>); } for (my $j = 0; $j<scalar(@vlan_numbers); $j++) { print "$vlan_numbers[$j] => $vlan_names[$j]\n"; } } #End VLANs sub quit_check { #my $key = getc(STDIN); #print "Entered value is $key\n"; chomp ($quit = <>); if (($quit eq '') || ($quit eq 'q')) { return; } else { print "Press ENTER to continue, or 'q' to QUIT.\n"; quit_check(); } } sub do_split { my $str = "I'm not as think as you stoned I am"; # split into individual words on whitespace delimiter # and store in array @words my @words = split (/ /, $str); for (my $j=0; $j<scalar(@words); $j++) { print "$words[$j]\n"; sleep 5; } } __END__
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Save Split to Array
by NetWallah (Canon) on Aug 27, 2009 at 17:49 UTC | |
Re: Save Split to Array
by bv (Friar) on Aug 27, 2009 at 16:39 UTC | |
Re: Save Split to Array
by ssandv (Hermit) on Aug 27, 2009 at 16:22 UTC | |
Re: Save Split to Array
by merlyn (Sage) on Aug 27, 2009 at 20:39 UTC | |
Re: Save Split to Array
by CountZero (Bishop) on Aug 27, 2009 at 21:13 UTC | |
Re: Save Split to Array
by NetWallah (Canon) on Aug 28, 2009 at 05:49 UTC | |
by spickles (Scribe) on Aug 28, 2009 at 16:13 UTC | |
by NetWallah (Canon) on Aug 28, 2009 at 20:39 UTC | |
Re: Save Split to Array
by Grey Fox (Chaplain) on Aug 28, 2009 at 16:05 UTC |
Back to
Seekers of Perl Wisdom