inserting dot

Discussion in 'Perl Misc' started by cooldaddy, Oct 12, 2005.

  1. cooldaddy

    cooldaddy Guest

    How to insert a dot character in a string at each 10th postition ? For
    example: I wanna place a dot at the 10th, 20th and 30th position in the
    string named $global
    cooldaddy, Oct 12, 2005
    #1
    1. Advertising

  2. cooldaddy

    cooldaddy Guest

    well, I havent got a clue which command to use for this. But if you can
    give me the name of the command, i can google for the answer.
    cooldaddy, Oct 12, 2005
    #2
    1. Advertising

  3. cooldaddy wrote:
    > How to insert a dot character in a string at each 10th postition ? For
    > example: I wanna place a dot at the 10th, 20th and 30th position in the
    > string named $global
    >


    As in "please write a program for me that does what I need"?

    What have you tried so far and where did it not produce the desired result?

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Oct 12, 2005
    #3
  4. cooldaddy

    Anno Siegel Guest

    cooldaddy <> wrote in comp.lang.perl.misc:
    > How to insert a dot character in a string at each 10th postition ? For
    > example: I wanna place a dot at the 10th, 20th and 30th position in the
    > string named $global


    There must be at least a half-dozen ways. What have you tried and how
    did it fail?

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Oct 12, 2005
    #4
  5. cooldaddy

    cooldaddy Guest

    well, I couldnt try anything yet, cause I don't know what commands i
    could use. I've got a little perl book here at home, but nothing in
    there on how to insert something at a given position. Also if i google
    something like:
    perl insert, it doesnt show me what i need.. All i need to know from
    you guys is what command i should look at..
    cooldaddy, Oct 12, 2005
    #5
  6. cooldaddy

    Anno Siegel Guest

    cooldaddy <> wrote in comp.lang.perl.misc:
    > well, I havent got a clue which command to use for this. But if you can
    > give me the name of the command, i can google for the answer.


    There isn't a single "command" to use. Solutions could involve join,
    unpack, substr, regular expressions and substitution operations, and
    more.

    Fishing for hints and googling for answers is no way to solve a programming
    problem. Learn the language you're using.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Oct 12, 2005
    #6
  7. cooldaddy

    Babacio Guest

    "cooldaddy" <> writes:

    > well, I couldnt try anything yet, cause I don't know what commands i
    > could use. I've got a little perl book here at home, but nothing in
    > there on how to insert something at a given position. Also if i google
    > something like:
    > perl insert, it doesnt show me what i need.. All i need to know from
    > you guys is what command i should look at..


    I think you have the perldoc in your perl installation. Try to have a
    look at the perlfunc section, it may help. The first thing would be to
    look for 'string' in its text. You would find easily something like:

    Functions for SCALARs or strings
    "chomp", "chop", "chr", "crypt", "hex", "index", "lc", "lcfirst",
    "length", "oct", "ord", "pack", "q/STRING/", "qq/STRING/",
    "reverse", "rindex", "sprintf", "substr", "tr///", "uc", "ucfirst",
    "y///"


    --
    Bé erre hue ixe eu elle, Bruxelles.
    Babacio, Oct 12, 2005
    #7
  8. cooldaddy wrote:
    > How to insert a dot character in a string at each 10th postition ? For
    > example: I wanna place a dot at the 10th, 20th and 30th position in
    > the string named $global


    See "perldoc -q comma":

    How can I output my numbers with commas added?

    jue
    Jürgen Exner, Oct 12, 2005
    #8
  9. cooldaddy wrote:
    > well, I havent got a clue which command to use for this. But if you
    > can give me the name of the command, i can google for the answer.


    Why on earth would you do that instead of using the readily provided Perl
    manual?

    jue
    Jürgen Exner, Oct 12, 2005
    #9
  10. cooldaddy wrote:
    > well, I havent got a clue which command to use for this. But if you can
    > give me the name of the command, i can google for the answer.
    >


    If _I_ cannot find a ready-made command, I'll write one myself, using
    whatever skills I already have. Often, I later find a much better
    solution and, if necessary, I can adapt my program to use the better ideas.
    Even with a limited knowledge of Perl, you should be able to come up
    with a program that attempts to solve your problem using whatever
    elements of Perl you do know. If you stumble across a specific problem,
    consult the documentation or come back to ask. The answer you'll get,
    however, may be a concise

    "perldoc -f substr"

    or the like.

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Oct 12, 2005
    #10
  11. cooldaddy

    cooldaddy Guest

    thanks for your replies, I looked into the substr thingy, and made this
    little script.. but i'm getting this strange error message: useless use
    of addition (+) in void context at test.pl line 7, and then it hangs...
    how come ?

    #!/usr/bin/perl -w
    use CGI;
    print "Content-type: text/html\n\n";
    $tekst="abcdefghijklmnopqrstuvwxyz";

    for ($count=1; $count<=length($tekst); $count+3){
    substr($tekst,$count,0)=".";
    }
    print $tekst;
    cooldaddy, Oct 12, 2005
    #11
  12. cooldaddy

    Paul Lalli Guest

    cooldaddy wrote:
    > thanks for your replies, I looked into the substr thingy, and made this
    > little script.. but i'm getting this strange error message: useless use
    > of addition (+) in void context at test.pl line 7, and then it hangs...
    > how come ?


    Because you're uselessly using addition in a void context on or about
    line 7.

    > #!/usr/bin/perl -w


    You're missing "use strict;". Have you read the posting guidelines for
    this group?

    > use CGI;
    > print "Content-type: text/html\n\n";


    These lines are irrelevant to any test of inserting a period into a
    string. Why did you include them?

    > $tekst="abcdefghijklmnopqrstuvwxyz";
    >
    > for ($count=1; $count<=length($tekst); $count+3){
    > substr($tekst,$count,0)=".";
    > }
    > print $tekst;


    What exactly do you think the statement "$count+3" is doing? Were you
    expecting that to in some way update $count?

    Paul Lalli
    Paul Lalli, Oct 12, 2005
    #12
  13. cooldaddy wrote:
    > thanks for your replies, I looked into the substr thingy, and made this
    > little script.. but i'm getting this strange error message: useless use
    > of addition (+) in void context at test.pl line 7, and then it hangs...
    > how come ?
    >
    > #!/usr/bin/perl -w
    > use CGI;


    use warnings;
    use strict;

    > print "Content-type: text/html\n\n";
    > $tekst="abcdefghijklmnopqrstuvwxyz";


    Dutchman? B-{)
    >
    > for ($count=1; $count<=length($tekst); $count+3){


    This "$count+3" is just a sum, but there should be a side-effect in that
    last part. It hangs, because $count will eternally be "1".
    Better use "$count += 3".

    > substr($tekst,$count,0)=".";
    > }
    > print $tekst;


    You're almost there!

    Josef
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Oct 12, 2005
    #13
  14. cooldaddy

    Robert Chug Guest

    "cooldaddy" <> wrote in
    news::

    > How to insert a dot character in a string at each 10th postition ? For
    > example: I wanna place a dot at the 10th, 20th and 30th position in the
    > string named $global
    >
    >


    use warnings;
    use strict;
    my $global="abcdefghijklmnopqrstuvwxyz";
    my @globe=split//,$global;
    my $final;
    my $len=scalar @globe;
    my $r;
    print "$global\n";
    foreach my $i (@globe)
    {
    if ((($r % 10)==0) & ($r>0))
    {
    $final=$final.".".$i;
    $r++
    }
    else
    {
    $final="$final"."$i";
    $r++;
    }
    }
    print "\n$final";

    this is a very crude example , but you are still adviced to follow what
    other have mentioned,
    Robert Chug, Oct 12, 2005
    #14
  15. cooldaddy

    Paul Lalli Guest

    Robert Chug wrote:
    > "cooldaddy" <> wrote in
    > news::
    >
    > > How to insert a dot character in a string at each 10th postition ? For
    > > example: I wanna place a dot at the 10th, 20th and 30th position in the
    > > string named $global

    >
    > use warnings;
    > use strict;
    > my $global="abcdefghijklmnopqrstuvwxyz";
    > my @globe=split//,$global;
    > my $final;
    > my $len=scalar @globe;
    > my $r;
    > print "$global\n";
    > foreach my $i (@globe)
    > {
    > if ((($r % 10)==0) & ($r>0))
    > {
    > $final=$final.".".$i;
    > $r++
    > }
    > else
    > {
    > $final="$final"."$i";


    Why did you correctly not use useless quotes in the above block, but
    use them here?
    This could better be written any of three ways:
    $final = $final . $i;
    $final = "$final$i";
    $final .= $i;

    > $r++;
    > }
    > }
    > print "\n$final";
    >


    Gah. What's that quote in either the Camel or Llama? Something about
    a Stravarious violin pounding in nails? This might be a decent
    algorithm if Perl was limited to, say, C's function set. It's not.

    Please look at the functions that directly manipulate Perl strings.
    Specifically substr()
    perldoc perlfunc
    perldoc -f substr

    Paul Lalli
    Paul Lalli, Oct 12, 2005
    #15
  16. cooldaddy

    Dr.Ruud Guest

    cooldaddy schreef:
    > thanks for your replies, I looked into the substr thingy, and made
    > this little script.. but i'm getting this strange error message:
    > useless use of addition (+) in void context at test.pl line 7, and
    > then it hangs... how come ?
    >
    > #!/usr/bin/perl -w
    > use CGI;
    > print "Content-type: text/html\n\n";
    > $tekst="abcdefghijklmnopqrstuvwxyz";
    >
    > for ($count=1; $count<=length($tekst); $count+3){
    > substr($tekst,$count,0)=".";
    > }
    > print $tekst;


    Not a bad attempt.

    1. You obviously didn't read (or remember) the posting guidelines:
    always code 'use strict;'.

    2. Some lines in your code are not needed for your test and some are
    missing, as Paul Lalli already said.

    3. You are inserting at every 2nd position (so not at every 10th as you
    first said) but start too soon, maybe because you don't know that the
    index starts at 0, see also your '<='.

    4. You didn't indent nor space your code properly.

    Some suggestions:

    #!/usr/bin/perl

    use strict;
    use warnings;

    my $tekst = "abcdefghijklmnopqrstuvwxyz";
    my $step = 5;

    for (my $count = $step; $count < length($tekst); ($count += $step)++)
    {
    substr($tekst, $count, 0) = ".";
    }

    print $step, ':', $tekst, "\n";


    Variant (that changes $step):

    for (my $count = $step++; $count < length($tekst); $count += $step) {

    --
    Affijn, Ruud <http://www.pandora.com/?sc=sh770781&cmd=tunermini>

    "Gewoon is een tijger."
    Dr.Ruud, Oct 12, 2005
    #16
  17. cooldaddy

    Dave Weaver Guest

    On Wed, 12 Oct 2005 13:34:16 GMT, Robert Chug <> wrote:
    > "cooldaddy" <> wrote in
    > news::
    >
    > > How to insert a dot character in a string at each 10th postition ? For
    > > example: I wanna place a dot at the 10th, 20th and 30th position in the
    > > string named $global
    > >
    > >

    >
    > use warnings;
    > use strict;
    > my $global="abcdefghijklmnopqrstuvwxyz";
    > my @globe=split//,$global;


    Having had to maintain other people's code, I'm a big fan of sensible
    names for variables. A "globe" is a sphere - what does this have to do
    with the contents of this variable?
    @chars or @letters would be a bit more sensible.
    But then you only use this array once, so it's not really needed (see
    below)

    > my $final;
    > my $len=scalar @globe;


    Note that the "scalar" is redundant here - the right hand side of the
    assignment is already in scalar context. $len is never used anyway,
    so really the whole statement is redundant.

    > my $r;
    > print "$global\n";
    > foreach my $i (@globe)
    > {
    > if ((($r % 10)==0) & ($r>0))

    ^
    oops!
    > {
    > $final=$final.".".$i;
    > $r++
    > }
    > else
    > {
    > $final="$final"."$i";
    > $r++;
    > }
    > }


    More neatly written as:

    my $count = 0;
    my $final = '';
    for my $char ( split //, $global ) {
    if ( $count and $count % 10 == 0 ) {
    $final .= '.';
    }
    $final .= $char;
    ++$count;
    }

    (untested)
    but see below for a better way of doing it.

    > print "\n$final";
    >
    > this is a very crude example , but you are still adviced to follow what
    > other have mentioned,


    I know you said it's crude, but there's not much point in enabling
    warnings if you're going to ignore them;

    Possible precedence problem on bitwise & operator at - line 12.
    Use of uninitialized value in modulus (%) at - line 11.
    Use of uninitialized value in numeric gt (>) at - line 11.
    Use of uninitialized value in string at - line 18.

    Warnings show that things aren't working as you intend, even if the
    end result is right.

    Why split the string into individual characters? Perl is much better
    operating on full strings than individual characters.

    For example:

    #!/usr/bin/perl
    use warnings;
    use strict;

    my $string = join '', 'A' .. 'Z', 'a' .. 'z';

    for ( my $pos = 9; $pos < length $string; $pos += 10 ) {
    $string = substr( $string, 0, $pos )
    . "."
    . substr( $string, $pos );
    }


    NB: the "spec" is ambiguous; is a "." wanted after every 10 characters
    of the input, or at every 10th location in the output? My code does
    the latter.
    Dave Weaver, Oct 12, 2005
    #17
  18. cooldaddy

    Matija Papec Guest

    X-Ftn-To: cooldaddy

    "cooldaddy" <> wrote:
    >well, I couldnt try anything yet, cause I don't know what commands i
    >could use. I've got a little perl book here at home, but nothing in
    >there on how to insert something at a given position. Also if i google
    >something like:
    >perl insert, it doesnt show me what i need.. All i need to know from
    >you guys is what command i should look at..


    Read "perldoc perlretut" and try to understand how s/// works,

    my $text = join '', 10..50;
    $text =~ s/ (.{10}) /$1 /xgs;


    there is also very good cookbook if you want some perl recipes,
    http://www.oreilly.com/catalog/perlckbk2/toc.html


    --
    Matija
    Matija Papec, Oct 12, 2005
    #18
  19. cooldaddy

    Robert Chug Guest

    Dave Weaver <> wrote in
    news:434d22ba$0$21190$:

    > On Wed, 12 Oct 2005 13:34:16 GMT, Robert Chug <>
    > wrote:
    >> "cooldaddy" <> wrote in
    >> news::
    >>
    >> > How to insert a dot character in a string at each 10th postition ?
    >> > For example: I wanna place a dot at the 10th, 20th and 30th
    >> > position in the string named $global
    >> >
    >> >

    >>
    >> use warnings;
    >> use strict;
    >> my $global="abcdefghijklmnopqrstuvwxyz";
    >> my @globe=split//,$global;

    >
    > Having had to maintain other people's code, I'm a big fan of sensible
    > names for variables. A "globe" is a sphere - what does this have to do
    > with the contents of this variable?
    > @chars or @letters would be a bit more sensible.
    > But then you only use this array once, so it's not really needed (see
    > below)
    >
    >> my $final;
    >> my $len=scalar @globe;

    >
    > Note that the "scalar" is redundant here - the right hand side of the
    > assignment is already in scalar context. $len is never used anyway,
    > so really the whole statement is redundant.
    >
    >> my $r;
    >> print "$global\n";
    >> foreach my $i (@globe)
    >> {
    >> if ((($r % 10)==0) & ($r>0))

    > ^
    > oops!
    >> {
    >> $final=$final.".".$i;
    >> $r++
    >> }
    >> else
    >> {
    >> $final="$final"."$i";
    >> $r++;
    >> }
    >> }

    >
    > More neatly written as:
    >
    > my $count = 0;
    > my $final = '';
    > for my $char ( split //, $global ) {
    > if ( $count and $count % 10 == 0 ) {
    > $final .= '.';
    > }
    > $final .= $char;
    > ++$count;
    > }
    >
    > (untested)
    > but see below for a better way of doing it.
    >
    >> print "\n$final";
    >>
    >> this is a very crude example , but you are still adviced to follow
    >> what other have mentioned,

    >
    > I know you said it's crude, but there's not much point in enabling
    > warnings if you're going to ignore them;
    >
    > Possible precedence problem on bitwise & operator at - line 12.
    > Use of uninitialized value in modulus (%) at - line 11.
    > Use of uninitialized value in numeric gt (>) at - line 11.
    > Use of uninitialized value in string at - line 18.
    >
    > Warnings show that things aren't working as you intend, even if the
    > end result is right.
    >
    > Why split the string into individual characters? Perl is much better
    > operating on full strings than individual characters.
    >
    > For example:
    >
    > #!/usr/bin/perl
    > use warnings;
    > use strict;
    >
    > my $string = join '', 'A' .. 'Z', 'a' .. 'z';
    >
    > for ( my $pos = 9; $pos < length $string; $pos += 10 ) {
    > $string = substr( $string, 0, $pos )
    > . "."
    > . substr( $string, $pos );
    > }
    >
    >
    > NB: the "spec" is ambiguous; is a "." wanted after every 10 characters
    > of the input, or at every 10th location in the output? My code does
    > the latter.
    >
    >


    thanks for your comments,
    BTW I did not got any warnings like you mentioned, what could I have been
    missing , I use perIDE for writing and compiling and version is v5.8.3
    Robert Chug, Oct 12, 2005
    #19
  20. cooldaddy

    Dave Weaver Guest

    Robert Chug <> wrote:
    >
    > thanks for your comments,
    > BTW I did not got any warnings like you mentioned, what could I have been
    > missing , I use perIDE for writing and compiling and version is v5.8.3


    Unfortunately I am unfamiliar with perlIDE; I suggest you check
    whatever configuration options it has to see if anything is preventing
    you seeing warnings.

    I saw the warnings using perl v5.8.0 on Linux, using the good old
    command-line.

    Try compiling your program from the command line and see what you get:

    perl -c yourprog.pl
    Dave Weaver, Oct 13, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Samuël van Laere

    To dot or not to dot?

    Samuël van Laere, Oct 16, 2003, in forum: HTML
    Replies:
    8
    Views:
    422
    Samuël van Laere
    Oct 16, 2003
  2. Christopher M. Lusardi

    volatile struct in dot h vs dot c

    Christopher M. Lusardi, May 11, 2004, in forum: C Programming
    Replies:
    3
    Views:
    472
    Peter Shaggy Haywood
    May 15, 2004
  3. Nathan Sokalski
    Replies:
    11
    Views:
    701
    AAaron123
    Aug 14, 2009
  4. krishnan

    Dot Net Project Execution without Dot Net and Framework....

    krishnan, Jan 7, 2006, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    184
    krishnan
    Jan 7, 2006
  5. Replies:
    6
    Views:
    242
    Thomas 'PointedEars' Lahn
    Dec 12, 2005
Loading...

Share This Page