Split Function 
Author Message
 Split Function


I am currently writing a perl script to search for regular expressions
within a text file.  On the command line for this script the user can
specify a -wx:y option where x is the number of lines to print from the file
before the matching line and y is the number of lines to print from the file
after the matching line.

In order to validate the user has specified both the x and y options I have
included the following code in my script:

    # Validate the format of the argument
    # Use the calculated lengtharg to extract the number portion
    # i.e. 12:19.  Needed to decrement two from the total length
    # to account for the -w portion.
    $bfafval= substr($ARGV[$loopcount], 2, ($lengtharg - 2));
    # Use split to obtain the number of lines to print both
    # before and after a matching line.

    # Validate that the argument was in the correct format based
    # on the array having two elements.  This will pick up both
         # incorrect delimiter used or two values for before and
         # after lines have not been entered
    if ($#nolinesprnt != 1)
           print "Error: invalid -w format \nUsage: $0 $USAGE \n";

length of the $bfafval and it is correct.

Any assistance would be appreciated.

Susan Entwisle

Wed, 18 Jun 1902 08:00:00 GMT  
 Split Function


> The above code does not work if -w:1 is entered - it appears that the

Yes, it would do that. In that situation, split() would return '' (the
empty string) and 1, as that is what you get when you split ':1' on the
colon. What you really want to do is use a regex:

if ($ARGV[$loopcount] =~ /(\d+):(\d+)/) {


else {
    print "Error: invalid -w format \nUsage: $0 $USAGE \n";


succesful match, and that you don't need to mess around with length() or
substr() first.


Wed, 18 Jun 1902 08:00:00 GMT  
 [ 2 post ] 

 Relevant Pages 

1. How to use split function to split on a backslash

2. Problem with join function (and split function)

3. using split function twice on same line

4. Split function

5. Using SPLIT function with a Period

6. Using a period as a delimiter in the split() function

7. split function that handles quoting..

8. Newbie, split function, and counter

9. Split function in Perl

10. Using a period as a delimiter in the split() function

11. Help - Split Function Blowing My Mind Away!!

12. An Efficient Split Function


Powered by phpBB® Forum Software