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

Problem: I need a means of storing only the first letter of an element of @p in $x, Super Search reveals nothing, and being new to perl I am unsure where I should begin solving this..

I guess the best way of illustrating the issue is to say that imagine that I need to generate an acronym:
my(@p) = qw(Internet Control Message Protocol);
I would need a way to print "ICMP" ...

Thanks,
/renz.

Update: Thanks for the help. It works fine, and I'll be busy for a bit reading up on substr and map for future use.

Replies are listed 'Best First'.
Re: Storing first letter of each element of @p in $x
by Tanktalus (Canon) on Jan 18, 2005 at 16:51 UTC
    my $str = join '', map { substr($_, 0, 1) } @p;

    In short: each array entry, we take the first letter (substring - starting at 0, for 1 character), join them together with nothing between them, and we get the string. "print $str" would work from here, or whatever you need to do.

      Thanks a lot -- that works perfectly.
Re: Storing first letter of each element of @p in $x
by Sidhekin (Priest) on Jan 18, 2005 at 16:53 UTC

    Get the first letter (perldoc -f substr),
    for each element (perldoc -f map),
    then make a string from these characters (perldoc -f join).

    my $x = join'', map {substr $_, 0, 1} @p;

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!