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

Hello dear Monks!
I wanted to ask something, which I know that can be done extremely easily using a regular expression, BUT the thing is that I have to do this for an assignment and regular expressions will be taught in the next lesson :P
The task is, if you have a line like this:
ID SP96_DICDI STANDARD; PRT; 600 AA.

how can you isolate only SP96_DICDI from it, but without the obvious regexp?

Replies are listed 'Best First'.
Re: A maybe rather silly question...
by tobyink (Canon) on Feb 27, 2014 at 15:00 UTC

    Are each of the fields of a fixed length? That is, does the field you wish to isolate always start at character 7 of the line, and is it always 10 characters long?

    If so, read up on the substr function.

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
      In substr you can't specify "Start from character 7 and continue until you find a blank space", right? Or can you?

        But you could read from character 7 till the end, and then use split to get the part before the first whitespace ...

        ... or use index to search for the first blank after position 7, and adjust your substr accordingly ...

        so many possibilities ;-)

Re: A maybe rather silly question...
by kennethk (Abbot) on Feb 27, 2014 at 16:58 UTC
    There are 3 general approaches to parsing out strings:
    1. Regular expressions; obviously, this is out based on the spec. It's possible however that the intent is to use split, which might not be considered a regular expression for the purposes of your assignment. Split on \s+, and the second term is your target.

    2. unpack works for fixed-width formats, which this appears to be. Your template would consist of a series of A#, where each # would be the appropriate field width. The results will truncate trailing whitespace automatically. Again, grab the second term.

    3. substr and index. This could work for either fixed-width fields or variable width fields. The most obvious choice would be to grab from the 6th character on, use index on the result to find the first space, and then truncate in a second step.

    Good luck on your assignment, and good luck in your class.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: A maybe rather silly question...
by dwm042 (Priest) on Feb 27, 2014 at 19:12 UTC

    The split function seems the simplest approach to me:

    #! /usr/bin/perl while(<DATA>) { my @field = split " ", $_; print "Field 2 => ",$field[1], "\n"; } __DATA__ ID SP96_DICDI STANDARD; PRT; 600 AA.

    And the output is:

    C:\scripts>perl field_split.pl Field 2 => SP96_DICDI
      Thanks so much guys!
      I think I will (must) go with susbtr, because, technically, we don't "know" regexps and arrays yet!