Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

2 basic questions

by Anonymous Monk
on Apr 29, 2013 at 13:45 UTC ( #1031208=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I am pretty new to Perl. Kindly answer me if any of you can... :)

I have 2 questions today,

1)

---------

$check = "a"; $check++; print $check; exit;

This will give me answer as "b", but what should i do to get "d" or "e" (I mean to pass over 3 or 4 alphabets).

Next to Second question,

2

--------------

I have a string which have 2 alphabets, I need to trim it to 1 alphabet, ie, I only want the right alphabet, the left one should be trimmed out.

Example

$input= "ab"; $output = "b";

The alphabets can be any, not just "ab", it can be "cd" or "sp", In that case output should be "d" & "p" resp.

Thanks Monks.

Comment on 2 basic questions
Select or Download Code
Re: 2 basic questions
by tobyink (Abbot) on Apr 29, 2013 at 13:55 UTC
    use v5.12; my $check = "a"; $check++ for 1..3; # three letters say $check; $check++ for 1..24; # another 24 letters, but there's only 26 in the +alphabet, say $check; # so what happens now we've run off the end? # hmmm... that's handy. # it takes us to the next part of your question... my $input = $check; my $output = substr($input, 1, 1); # skip one character, take one char +acter say $output;
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
      Thx Toby
Re: 2 basic questions
by toolic (Chancellor) on Apr 29, 2013 at 13:56 UTC
    For 1 you can use a for loop, for 2 you can use substr
    use warnings; use strict; my $check = "a"; $check++ for 1..3; print $check; print "\n"; my $input= "ab"; print substr $input, -1, 1; print "\n"; __END__ d b
      thx Monk...
Re: 2 basic questions (perlintro)
by Anonymous Monk on Apr 29, 2013 at 13:56 UTC
Re: 2 basic questions
by choroba (Abbot) on Apr 29, 2013 at 14:00 UTC
    1. The string increment is magical, but no other string operations are. Therefore, if you do not want to resort to ord arithmetics, you have to repeat the increment:
      my $check = 'a'; $check++ for 1 .. 5;
    2. TIMTOWTDI. You can for example use substr:
      my $check = 'cd'; substr $check, 0, 1, q();

      Or, use a substitution:

      my $check = 'sp'; $check =~ s/^.//;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: 2 basic questions
by ww (Bishop) on Apr 29, 2013 at 14:14 UTC
    Very basic and better answered for yourself by reading perldoc perlintro (from your command line)... and -- as is often the case -- correctly answered in many different ways. Here is a (partial) answer for each question:
    my $check = "A"; for (1..2) { $check++; } say $check; #2: my $input = "xy"; $input = reverse ($input); chop $input; say $input;
    The first, obviously, is a simplied loop using the range operator (alternate syntax is shown by other replies above); the second takes advantage of the otherwise little-used chop. Note that it will "work" even if your charpair is NOT an ordered pair.

    Please take these for examples, and set yourself the task of finding at least two other ways to answer each of your questions.


    If you didn't program your executable by toggling in binary, it wasn't really programming!

      Thank you...
Re: 2 basic questions
by karlgoethebier (Curate) on Apr 29, 2013 at 14:35 UTC

    Another TIMTOWTDI:

    my $result = (split "", "ab")[(length(qq(ab))-1)]; print $result;

    Please note: with subtraction ;-)

    Update:

    Two more...

    $result = pop @{ [ split( "", "ab" ) ] }; print qq($result\n); $result = splice( @{ [ split( "", "ab" ) ] }, -1 ); print qq($result\n);

    Perhaps this is the reason why some folks hate perl?

    Regards, Karl

    P.S.:

    «The Crux of the Biscuit is the Apostrophe»

Re: 2 basic questions
by Random_Walk (Parson) on Apr 29, 2013 at 14:58 UTC

    Just playing, may not work in your character set:

    perl -le "while(1){$a=$a?$a:ord'a';print chr$a;$a=++$a%ord'{';sleep 1} +"

    This probably will though

    perl -le "$_='x';while(1){s/aa/a/;print$_++;sleep 1}"

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1031208]
Approved by Corion
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2014-09-20 08:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (157 votes), past polls