Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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:

#!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__

In reply to Save Split to Array by spickles

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-03-29 14:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found