Perl Split Pattern

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aemado
    New Member
    • Sep 2007
    • 17

    Perl Split Pattern

    I am writing my very first Perl program, and am still lost with how to use split.
    I have several lines of data that I want to read in, for example:

    333444555 Smith Ana Ms RN
    777666555 Jones Tom Mr MD

    These data fields have certain restrictions. The first number always must be exactly 9 digits. The second and third can be up to 9 (anything less)..and the 3rd/4th must be exactly 2. There can be more than one space (or infinitely many spaces) as long as all data is on the same line. How could I use split here to determine this? Any suggestions you may have would be greatly appreciated!
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    For split to be utilized you really need a delimiter. A delimiter can be just about anything ie: whitespace, tab, comma, pipe, dash and so on. If you don't have a common delimiter then I would suggest that you use a regex to capture the data.

    [CODE=perl]my $string = '333444555 Smith Ana Ms RN';
    # Split the string on the whitespace
    my ($id, $lname, $fname, $suffix, $abrv) = split(/ /, $string);[/CODE]

    --Kevin

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Well, assuming you can get the input line into a scalar, you can use split like this:

      [CODE=perl]my @line_vals = split ' ', $the_input;[/CODE]

      Now @line_vals will have (123456789, 'Lastname', 'Firstname', 'TITLE', 'OTHERVALUE') like your file, and you can perform checks and such on these values as normal.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        If you use a regexp like eWish suggests, make sure you account for the fact that more than one space might be there:

        [CODE=perl]split / +/, $string;[/CODE]

        I'm not sure if it matters - I don't know if split will ever return an empty string (for the items between spaces).

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          split() will use spaces by default if no argument is used. This is perfectly valid:

          Code:
          while (<>) {
             @foo = split;
          }
          it will correctly split a file with multiple spaces between fields and leave off leading/trailing spaces. I am pretty sure it also removes the line endings unlike split(/ /) or split(/ +/) because the default split is equivalent to split(/\s+/) . the \s character class includes tabs and newlines and maybe other things too, like carraige returns.

          Comment

          Working...