in reply to How to insert || and && in an if loop

Respencted Monks, Thank you for taking time to reply. I want to try the insertion of || Operator because currently my script looks like this:
#!/usr/bin/perl use warnings; use strict; print "Enter a for lowercase and colons. b to do it the other way around. q to quit "; my $choice = 0; my $wwn = 0; chomp ($choice = <STDIN>); if ($choice eq "a") { until ( $wwn eq "q") { print "Enter the wwn or q to quit: "; chomp ($wwn=<STDIN>); if ($wwn=~m/:/g) { print "WWN already contains :\n"; exit; }#closing for if ($wwn=~m/:/g) elsif (length($wwn)!=16) { print "Invalid WWN Length\n"; exit; }#closing for elsif (length($wwn)!=16) else { my @wwn = unpack ("(a2)*", lc($wwn)); @wwn = join (":", @wwn); print "@wwn\n"; } #closing for lowering case and adding : in wwn. }#closing for until ( $wwn eq "q") }#closing for if choice =a elsif ($choice eq "b") { until ($wwn eq "q") { print "Enter the wwn with : or q to quit: "; chomp ($wwn=<STDIN>); $wwn =~ s/://g; print lc($wwn), "\n"; } #closing for until ($wwn eq "q") }#closing for if choice =b elsif ($choice eq "q") { exit; }#closing for elsif ($choice eq "q")

What the script does is, it takes a 16 character long wwn and if I need it to be in lowercase and a ":" needs to be inserted, it does that. The second option is it removes the ":".

I need this functionality as these strings are WWNs aka World Wide Names that are used in the SAN Connectivity. Sometimes I need them in lowercase with ":" in them and sometimes I need them without ":". When I login to Cisco SAN Switches and search for these, I get them with ":". Other places where I need to put them, they should go without the ":".

My foremost apologies if the indentation is not good/confusing or if there are too many comments. I am newbie and hence still trying to come to terms with the way scripts are written. Please note that I have tried to present the script in the forum in the best possible manner and kindly pardon my mistakes.

Please direct me to pointers or guides where I can read from and improve on this script. If there is any guide or any article that shows how to write scripts in a better way indentation wise etc, kindly let me know. All of you have been very helpful and without your help, I would not be able to enjoy Perl.

Perl Version - (v5.14.2) MSWin32-x64-multi-thread on Windows 7 64 Bit.

Replies are listed 'Best First'.
Re^2: How to insert || and && in an if loop
by GrandFather (Saint) on Dec 18, 2011 at 20:56 UTC

    There are many ways that code could be tidied up, but keeping the logic similar I'd rewrite it as:

    #!/usr/bin/perl use warnings; use strict; print <<HEREDOC; Enter: a for lowercase and colons. b to do it the other way around. q to quit HEREDOC while (my $choice = lc <>) { chomp $choice; last if $choice eq 'q'; if ($choice !~ /^[ab]$/) { print "a, b or q expected: "; next; } my $wwn = lc <>; if ($choice eq 'b') { $wwn =~ s/://g; print "$wwn\n"; next; } if (-1 < index $wwn, ':') { print "WWN already contains :\n"; next; } if (16 != length $wwn) { print "WWN must be 16 characters\n"; next; } if ($wwn =~ /^[a-f0-9]/) { print "Non-hex characters are not allowed in WWN\n"; next } print join (':', unpack ("(a2)*", $wwn)), "\n"; }

    Note that the nesting has become shallow. Each of the tests is simple and the code associated with them is trivial. Early exits (in this case next and last) are used extensively to avoid the need for else clauses and consequent nesting.

    Update: Fixed hex digit match regex - thanks AnomalousMonk

    True laziness is hard work
Re^2: How to insert || and && in an if loop
by i5513 (Pilgrim) on Dec 18, 2011 at 12:17 UTC
    Hi,

    I would save wwn in raw format (without any ":"), and then I would format it with a function which do it (maybe with a parameter which indicate which format you want)

    I would recommend you using given statement (not if / else jungle)

    Regards,