Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Storing first letter of each element of @p in $x

by renz (Scribe)
on Jan 18, 2005 at 16:48 UTC ( [id://423083]=perlquestion: print w/replies, xml ) Need Help??

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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://423083]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (8)
As of 2024-04-20 00:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found