in reply to Re: Turning a hash into a line of links
in thread Turning a hash into a line of links

A slightly different, but not necessarily better, way would be to count the pipes using tr, like this

my $numps = ($homes =~ tr{|}{});

and then find the "middle" one's position by using index in a loop

my $middle = int($numps / 2) + 1; my $pos = -1; my $found = 0; while (1) { $pos = index $homes, q{|}, $pos; last if ++ $found == $middle; $pos ++; } substr $homes, $pos, 1, q{<BR>};

As you say, it is fun trying these things out. As for checking return values, I should perhaps check that index doesn't return -1 and that substr does return the pipe that I think I'm replacing; if this was production code I probably would :)

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^3: Turning a hash into a line of links
by OfficeLinebacker (Chaplain) on Dec 13, 2006 at 17:43 UTC

    tr//--Now there's another element of perl with which I am not very familiar. When I was devising my plan, I decided that I wanted to only loop through/traverse the string once, thus the array of positions of the pipes. Does tr not loop through the string? I'll have to look that up. I was focused on the thought that I will have to loop through the string once to count all the pipes; might as well save the positions while I am there so I don't have to go back through to "find" the middle one. Yeah I had to use the number of elements in the @pipes array to do some funky arithmetic, but only one time through the string.

    I've seen the q{} contruct more than once now. Do you guys use a lot of strings that contain embedded quotes? What are the relative merits of q{hi} vs 'hi'?

    Thanks,
    Terrence

    UPDATE: I figured out what tr/// does. I kept bouncing back and forth between tr///, which says "same as y///" and y///, which says "same as tr///." Finally saw the part that says "see perlop." Thanks.


    I like computer programming because it's like Legos for the mind.
      I have hardly ever used tr but I bumped into it the other day when looking something else up in the Camel Book. I thought that it could be useful for just the thing you are doing. It would be interesting to benchmark the various ways of counting characters or strings within strings.

      Regarding q{} and qq{}, I am from a *nix background and had never had any problem with normal single- and double-quotes. Then I started trying to do one-liners under MS Windoze and struggled. The quoting operators made it all work nicely and since then, to avoid having to think where I am, I have got into the habit of using them all the time. I don't think that there is any particular merit either way other than normal quotes are widely familiar and take fewer keystrokes, quoting operators do the right thing at the DOS prompt.

      Cheers,

      JohnGG

        That's a great story, and it makes perfect sense. I remember one guy recruiting me to work for a perl company and he mentioned that the code is written in *nix but it has to work in both. If I ever end up interviewing for a job there, I'll already know a good trick for writing portable code. Thanks.

        I like computer programming because it's like Legos for the mind.