Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^3: Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...) (js)

by tye (Sage)
on Sep 12, 2007 at 17:45 UTC ( #638627=note: print w/replies, xml ) Need Help??


in reply to Re^2: Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)
in thread Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)

I couldn't get that javascript version to work. Sure, it looks nice. It looks very much like something many tried as a first stab in Perl. But I'm not convinced it works in javascript any better than it works in Perl. I was not able to get it to return anything (while in Perl it returns too much).

Update: Thanks, eB. I was eventually able to get your example to demonstrate that the code works.

- tye        

  • Comment on Re^3: Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...) (js)

Replies are listed 'Best First'.
Re^4: Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...) (js)
by erroneousBollock (Curate) on Sep 13, 2007 at 14:47 UTC
    It works just fine. Try pasting the following into your url bar:

       javascript:alert("ZBBBCZZ".match(/((.)\2*)/g))

    That should alert the output of .toString() called on the array result.
    If you have Firefox use the following to see the unadulterated array source:

       javascript:alert(("ZBBBCZZ".match(/((.)\2*)/g)).toSource())

    -David

    Update: Does that imply that Javascript has different capture semantics than perl?

      Does that imply that Javascript has different capture semantics than perl?

      No. It is just that match() returns the matched substrings, not the captured substrings. If //g in a list context could be told to return the matched substrings (what it does when there are no capturing parens), then the same solution would work in Perl.

      A better JavaScript solution is actually: "ZBBBCZZ".match(/(.)\1*/g). No need for the second set of parens.

      - tye        

        Telling m//g in list context to forget about the captures and just return the matches:

        @list = "ZBBCZZ" =~ /(??{'(.)\1*'})/g;

        or probably more exact: there are no captures m// can see

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://638627]
help
Chatterbox?
[james28909]: here is a question, that hopefully i wont be made fun of for asking, but what exactly is a dereference?
[james28909]: isnt that like breaking some kind of link between something...
[james28909]: in google it says dereference means "obtain from (a pointer) the address of a data item held in another location."
[shmem]: dereferencing is getting the thing a reference points to.
[james28909]: but to me that sounds like it is actually referencing that data instead of dereferencing it. to me dereference means to break some kind of link or something
[james28909]: the dereference is the physical address of the data needed referenced?
[shmem]: "reference" something means making a reference to something. "dereference" means the invers, i.e. getting the thing the reference references.
[james28909]: or the act of getting the data that way is called dereference?

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2018-05-20 15:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?