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

I have three variables:
$fname = first name
$lname = last name
$id = student ID
I am trying to concantinate them to form another variable called $filename which I want to consist of last name, first initial, and student ID.
My problem is splitting the first name in order to get the first initial.
I've tried:
$firstinit = split(/\w?/, $fname)
$firstinit = split(/\w{1}/, $fname)
$firstinit = split(/(?=\w)/, $fname)
and others. The script I'm using to concantinate is:
$filename = $lname . $firstinit . $id;
My output usually looks something like this:
smith6123456
where 'smith' is $lname, '6' is $firstinit, and '123456' is $id.
How can I split off the first letter of the $fname?
Thanks, chris

Replies are listed 'Best First'.
Re: splitting a word
by xylus (Pilgrim) on Jan 16, 2002 at 00:47 UTC
    Try considering substr() instead of split. Example: my $firstinit = substr($filename, 0 ,1);
    
    ----------------------------------------------------------
    #!/usr/bin/perl
    @==qw/p e r l m o n k s/;$|*=1;@;=qw/8 15 7 9 -1 7 7 2 0/;
    foreach$-(@=){for(++$|..$;[$:++]){$-++}$..=$-}$.=~s/m/l/g;
    $*=$;[4]+1;for($;[9]..$;[0]/2){$,.=substr($.,$*++,1);$*++;
    }print$,;#http://www.perlmonks.org/index.pl?node_id=98506;
    
      substr would definately work, don't get me wrong, but just because this guy seems to be doing things in a regex sort of manner already, and it just so happens that I love regexs very much, I thought I would throw this little tidbet out there.
      $fname = 'thomas'; $fname =~ /^(\w{1})/;
      $1 is now equal to "t" hence the answer he was looking for. TrAdEz Awwww the power of perlre
Re: splitting a word
by broquaint (Abbot) on Jan 16, 2002 at 00:58 UTC
    You can 'split' off the first letter using a variety of methods
    use strict; my $fname = 'Larry'; my $fi = substr($fname, 0, 1); # $fi = 'L' my ($fi) = $fname =~ /^(.)/; # $fi = 'L' my $fi = (split //, $fname)[0]; # $fi = 'L'
    Although it's probably best to use the function that fits the situation the most (being substr() in this instance). The reason that split didn't work is that it was being used incorrectly. The split() function splits a string (the second argument) according to the delimiter supplied in the first argument, returning a list of strings.
    HTH

    broquaint

Re: splitting a word
by grep (Monsignor) on Jan 16, 2002 at 00:55 UTC
    You should use 'substr' as pointed out above, but I would also recommend you look for a different method of making the filename. Preferably using some non-identifying data like a seq number and then tracking that to the user name. (e.g. what if someone changes their name? get married?)

    grep
    grep> cd pub grep> more beer
Re: splitting a word
by Parham (Friar) on Jan 16, 2002 at 02:36 UTC
    my $firstname = 'Parham'; #set first name $firstname =~ /^(\w)/; #grab the first letter print $1; #print the first letter
    i think the syntax of:
    my ($variable) = $variable =~ /regex/;
    disregards the purpose of the $(number) variables which is why i stay away from them.

      I think the syntax ($match) = $var =~ /regex/;

      is a concise and flexible way to extract parts of a string. The following code should illustrate this nicely - note especially the usage with the /g modifier.

      #!/usr/bin/perl -w use strict; $_ = q/one two three four five/; my ($match, @matches); ($match) = /\w+\W+(\w+)/; print $match, "\n"; # prints two @matches = /(\w+)\W+(\w+)/; print join('|', @matches), "\n"; # prints one|two @matches = /(\w+)\W+(\w+)/g; print join('|', @matches), "\n"; # prints one|two|three|four @matches = /(\w+)/g; print join('|', @matches), "\n"; # prints one|two|three|four|five

      -- Hofmator