Perl Loop Test Condition.
FedoraForum.org - Fedora Support Forums and Community
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2012
    Location
    America
    Posts
    215
    Windows 7 Firefox 64.0

    Perl Loop Test Condition.

    I'm using Perl to loop through an array of strings and parse out certain patterns. I have a loop condition like this to test when all the items in the array have been inspected:

    Code:
    #Process the input line. 
    while ( $word = shift @fileWords )
    {
        #look for start of data to parse here 
    }
    The problem is that if a word that evaluates to 0 is read ( like "0" ) the loop breaks. I need to keep strings that evaluate to 0 from breaking the loop ( yeah , I know I can just use a "foreach" loop to do this ) but is there a way to prevent this behavior ? Thanks for any info...
    Last edited by cmanL; 10th January 2019 at 11:15 PM.

  2. #2
    Join Date
    Nov 2006
    Location
    Detroit
    Posts
    6,749
    Linux (Fedora) Firefox 63.0

    Re: Perl Loop Test Condition.

    You could do this:
    Code:
    while ( my ($index, $word) = each @fileWords ) {
       # Do something with $word
    }
    The nice thing is that you get the index of the string in the array (0-based), in case you need it.
    OS: Fedora 29 x86_64 | Machine: Lenovo ThinkCentre M91P | CPU: Intel Core i5-2500 3.30GHz | RAM: 8GB PC3-12800 DDR3 | Disk: 500GB SATA | Video: Intel HD Graphics 2000 128MB | Sound: Realtek ALC662 | Ethernet: Intel 82579LM

  3. #3
    Join Date
    Jul 2012
    Location
    America
    Posts
    215
    Windows 7 Firefox 64.0

    Re: Perl Loop Test Condition.

    Ok , thanks for the information , I haven't seen that trick. I've programmed "C" for many years now and I seem to try to program Perl in the same way ( even though there's often more efficient ways to accomplish things in other languages ).

Similar Threads

  1. [FC9] perl and required modules (perl(Test::More)
    By Inception in forum Using Fedora
    Replies: 0
    Last Post: 15th May 2008, 11:44 AM
  2. New: perl-Test-MockObject
    By fedora-package-announce-admin@fedora.us in forum Advisories & Updates
    Replies: 0
    Last Post: 18th June 2004, 07:32 AM
  3. New: perl-Test-Manifest
    By fedora-package-announce-admin@fedora.us in forum Advisories & Updates
    Replies: 0
    Last Post: 18th June 2004, 07:32 AM
  4. New: perl-Test-Pod
    By fedora-package-announce-admin@fedora.us in forum Advisories & Updates
    Replies: 0
    Last Post: 17th May 2004, 12:55 PM
  5. New: perl-Test-Builder-Tester
    By fedora-package-announce-admin@fedora.us in forum Advisories & Updates
    Replies: 0
    Last Post: 17th May 2004, 12:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •