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

If I have a char "1" but want to add 1 to it so I get 2, how would I do this(convert the string or character into a number)? Thanks in advance. Below is my code:
$newfile = "0001"; $char = substr($newfile,0, 4); $char =~ s/0//; print"$char\n"; $test = $$char + "1"; print"$test\n";

Replies are listed 'Best First'.
Re: adding 1 to a char of 1
by Moron (Curate) on Jun 20, 2006 at 17:12 UTC
    Perl is designed to make string and numeric scalars indistinguishable. It also has a '++' (increment) operator which not only works on numerical strings as well as numbers, but on any string. e.g.
    $x = 1; ++$x; # produces 2 or "2" (transparent which) $x = "1"; ++$x; # the same $x = "A"; ++$x; # produces B
    To strip leading zeros:
    $char =~ s/^(0)+//;
    see the perlop documentation for more details.

    -M

    Free your mind

      Thanks everyone for your help, it works fine now.... One last question.... now that I have the correct number, how do I only grab the last 4 digits so for example:
      my original number was 9 but now with the code u guys gave me, we incr +emented into 10. I need to plug this number back into the file name b +ut creating a new file called "name_0010" (originally it was "name_00 +09"). Here is the code I currently have, I know theres a more efficie +nt way to do this... $newfile = substr($newfile_n,0, 4); $newfile =~ s/^(0)+//; ++$newfile; print"$newfile\n"; $newfile = "000".$newfile; #if it gets to 100 then it will look like t +his "0100" substr($newfile, 1, 4);
        Not sure why you're using substr.  Let me suggest that you keep the index and the basename separate.

        For example:

        my $index = 0; for (my $i = 0; $i < 100; $i++) { $index++; my $fullname = sprintf "base_%04d", $index; printf "fullname = '%s'\n", $fullname; }

        will print 100 different names, starting with "name_0001" and ending with "name_0100".  The %04d achieves this result; the "0" left-pads the integer value with zeroes, and the "4" gives you 4 decimal places.

        Read up on sprintf to get more details:  perldoc -f sprintf.

        If you absolutely must get the last 4 digits of a string, you can use "substr" like this:

        $last_4_chars = substr($my_string, -4);

        This has the effect of taking the "rest" of the string, starting at the 4th character from the end (since the offset -4 is negative) rather than from the beginning.


        s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

        Use substr to extract and replace:

        my $oldfile = "name_0009"; my $num = substr($oldfile, -4); ++$num; my $newfile = substr($oldfile, 0, -4) . $num;

        Or use the lvalue form to simplify things:

        my $oldfile = 'name_0009'; substr(my $newfile = $oldfile, -4)++;
Re: adding 1 to a char of 1
by ikegami (Patriarch) on Jun 20, 2006 at 16:59 UTC

    Use the string increment operator:

    $newfile = "0001"; # Initialize to 0001 ++$newfile; # Now contains 0002

    Same thing, but longer:

    $newfile = "0001"; # Initialize to 0001 $newfile = sprintf("%04d", $newfile+1); # Now contains 0002

    If you just want 2 as opposed to 0002, then it's just:

    $newfile = "0001"; # Initialzie to 0001 $newfile += 1; # Now contains 2
Re: adding 1 to a char of 1
by MonkE (Hermit) on Jun 20, 2006 at 17:01 UTC
    You haven't given us much to go on, but I'm guessing that you have some kind of serialized filename (a dataset name perhaps?), and you want to produce the next filename in the series. In such a case, the following approach would work for you:
    use strict; my $oldfile = '0001'; my $newfile = sprintf("%4.4d", $oldfile + 1); print "New filename = $newfile\n";
      Yes thats exactually what Im doing..
Re: adding 1 to a char of 1
by Zaxo (Archbishop) on Jun 20, 2006 at 19:26 UTC

    substr and string increment can make that even easier,

    my $file = 'name_0009'; substr($file, 5, 4)++; print $file, "\n";
    which prints "name_0010".

    The substr function is an lvalue, so any sort of mutator can be applied to it.

    After Compline,
    Zaxo

A reply falls below the community's threshold of quality. You may see it by logging in.