curtisb has asked for the wisdom of the Perl Monks concerning the following question:

Afternoon,
I'm looking for some help. I have a while loop that runs while a file is open and I need to get a either a single character or a series of characters from the line based off of position in the line.
Here is an example of one of the lines
PMMDF-REC1 LOGSA MMDF 06114 24APRIL06 C22GEND +NO CURRENT PUBLICATIONS + + + + <b +r>
In this example, at the start of the line, P is one field and MMDF is another.
I know I can split on white spaces, but I need to be able to split up as I said in the example. Anyone has any suggestions, it would be greatly appreciated.
Thanks,
Bobby b.curtis@stanleyassociates.com

Replies are listed 'Best First'.
Re: Split a Line
by japhy (Canon) on May 25, 2006 at 15:46 UTC
    To split a string into chunks based on field LENGTH, not field DELIMITER, use unpack().
    my ($x, $y, $z) = unpack "A10 A3 A*", "example 987testing 1, 2, 3"; # $x = "example" # $y = 987 # $z = "testing 1, 2, 3"
    You'll notice $x had trailing spaces removed. If you want to keep trailing spaces, use "a" instead of "A" in the unpack format string. Read perldoc perlpacktut for more.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: Split a Line
by Herkum (Parson) on May 25, 2006 at 16:28 UTC
    To expand a little upon the unpack function (from Perl Best Practices)
    use Readonly; Readonly my $FIELD_LAYOUT => 'A10 A20 A10'; # 10 ASCII characters, 20 +ASCII characters, 10 ASCII characters; foreach my $line (@file) { my ($program, $junk, $date) = unpack $FIELD_LAYOUT, $line; print "Program: $program\n"; print "Date: $date\n"; }

    I found this code layout a bit easier to understand but it is still basically the same as what japhy Re: Split a Line posted.

Re: Split a Line
by ptum (Priest) on May 25, 2006 at 15:48 UTC

    So, you can just use split with an empty regex to build an array of characters, but I'm guessing your rules are more complex than that. You might want to:

    • carefully define your entire ruleset
    • post some code that shows your attempt to build a splitting regex

    No good deed goes unpunished. -- (attributed to) Oscar Wilde
Re: Split a Line
by superfrink (Curate) on May 25, 2006 at 18:58 UTC
    You can also extract a group of characters from a line with the substr function.
    my $mesg = "PMMDF-REC1"; my $cmd1 = substr $mesg, 0, 1; my $cmd2 = substr $mesg, 1, 4; my $cmd3 = substr $mesg, 6, 4; print $mesg, "\n"; print $cmd1, "\n"; print $cmd2, "\n"; print $cmd3, "\n";