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

Hello Perl Monks,

So to begin, I have very little experience with Perl and was just handed an assignment for class.

What we need to accomplish is:
We are given a text document with some info in the form of <FIRST NAME> <LAST NAME> <NUMBERS>

We need to take this file and produce another that has simple
<first 4 of FIRSTNAME><last four of NUMBERS>

I've gotton as far as basic hello world in Perl and read some input from a file. I was wondering if someone out there could poit me to some brief tutorials that can help me to finish up how to do this, or if someone can explain a type of concept to complete this to me.

I was thinking putting each line into an array, and counting up to the 5th spot, and deleting until i reached the n-4th element and stopping... Since the data is already roughly in format.

One of the things i'm told Perl is great for is pattern matching and the sort, but how would I say "delete everything after the first 4 characters, until the 4th to last character? Any help or guidance or point to some good resource to help on this topic?

  • Comment on First Perl Assignment... First taste of Perl: File, String Edit

Replies are listed 'Best First'.
Re: First Perl Assignment... First taste of Perl: File, String Edit
by marto (Cardinal) on Oct 04, 2010 at 13:19 UTC
Re: First Perl Assignment... First taste of Perl: File, String Edit
by JavaFan (Canon) on Oct 04, 2010 at 13:17 UTC
    I was thinking putting each line into an array, and counting up to the 5th spot, and deleting until i reached the n-4th element and stopping...
    This is Perl, not C. In Perl, strings are first class citizens. Do not think of strings as arrays of integers.
    delete everything after the first 4 characters, until the 4th to last character?
    The point of the exercise here isn't deleting. It's selecting. Selecting based on character count. Use substr for that. And since it's a class assignment, I leave it to this.

      Indeed:

      print "$1 things\n" if "The point of the exercise here isn't deleting. It's selecting. +" =~ m/(select)ing/;
Re: First Perl Assignment... First taste of Perl: File, String Edit
by GrandFather (Saint) on Oct 04, 2010 at 20:18 UTC

    We can help you a lot more if you show us the code you have tried. Even your hello world program will show us something of how you are thinking that will help guide our answers.

    It may also help us to know what text book you are using and what reference materials you have access to in addition to the documentation that comes with Perl. At this stage one of the most useful tools available to you may be perldoc.

    True laziness is hard work
Re: First Perl Assignment... First taste of Perl: File, String Edit
by Anonymous Monk on Oct 04, 2010 at 20:16 UTC
    Hi, First some general advice - always use strict and use warnings

    For this problem have alook at split and substr

    Always show what you have tried so far, PerlMonks help those who have helped themselves.

    J.C.