Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Jun 2002
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts

    String Manipulation

    I was wondering if there was a built in way to split a string into an array of characters? Or are strings already considered an array of characters (like in C++)?

    Also, is there a function that can check if a character is alphanumeric?

    Is it also possible to use PERL to write a webpage within the code, create forms inside the code, and receive the input to those forms directly? I'm trying to write code for a login screen and I want the page to change dynamically based on user input. I figured that's the best way to create a gateway page: to have the page be dynamic.
    Last edited by ObiwanJebroni; 07-05-2002 at 02:57 PM.
    -Obiwan Jabroni
    May the Schwartz be With You

  • #2
    Regular Coder Feyd's Avatar
    Join Date
    May 2002
    Location
    Los Angeles, CA Maxim: Subvert Society
    Posts
    403
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Three questions in one post? Sheesh...

    1)
    Use split:
    Code:
    $info = "This:is:splitting:me:up:inside";
    @personal = split(/:/, $info);
    You can also split a word into characters, sentence into words, and paragraph into sentences:
    Code:
    @chars = split(//, $word);
    @words = split(/ /, $sentence);
    @sentences = split(/\./, $paragraph);

    2) Use some of these:
    $string =~ s/[^a-zA-Z0-9]//g; #^ means all things NOT in this range
    $string =~ s/\W//g; #anything not alphanumeric
    $string =~ s/[^\w ]//g; #alphanumeric with spaces


    3) It'd be a bit more complex and ugly than PERL needs to be, and it wouldn't be as efficient, but you could use various tricks like looking for query strings or form field values in order to display certain information...Of course, sessions and cookies could make this easier on you...and of course, PHP would make it much easier, but it can be done in PERL.
    Moderator, Perl/CGI Forum
    shadowstorm.net - subvert society

  • #3
    New Coder
    Join Date
    Jun 2002
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Heh heh, I guess I'm just very inquisitive . Yep, I've definitely been using this forum a lot. I also decided that if I split the three questions into three threads...it might be considered flaming !

    Thanks for the suggestions. I guess I should try doing it in PHP. I have a friend who says using PHP to do it would be pretty simple. Is there anywhere I can look for tutorials and such?
    -Obiwan Jabroni
    May the Schwartz be With You

  • #4
    New Coder
    Join Date
    Jun 2002
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts
    One more thing, could you explain to me what the s///g thing does? I've seen that in several programs, and I've looked around, but it's never really been covered. I've seen tr/// (which is the closest thing that I've found that looks anything close to what you have). I'd appreciate it . Thanks.
    -Obiwan Jabroni
    May the Schwartz be With You

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts
    A s/// thing is a substitution, whilst m/// is a match (you can leave off the 'm' on the front - which is what most people do...), and tr/// is a transliteration.

    For example:
    $string =~ s/foo/bar/;

    Replaces the first instance of the letters 'foo' with 'bar'.
    If you add a 'g' on the end (called a modifier), it means to a global search and replace, i.e. change every instance of 'foo' with 'bar'.

  • #6
    New Coder
    Join Date
    Jun 2002
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah, thank you.
    -Obiwan Jabroni
    May the Schwartz be With You

  • #7
    New Coder
    Join Date
    Jun 2002
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts
    But then how do you check to see if a string has a certain character or characters? In other words, how can you put this in an if statement? Like:

    if ($FORM{'email'} =~ /\@/g)

    Is that how you do it?

    Oh yeah, and what exactly does the "=~" do? I still don't quite understand that.
    -Obiwan Jabroni
    May the Schwartz be With You

  • #8
    Regular Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well the =~ is just the operator that tells Perl there's a regular expression of some sort coming up.

    About your first question, yes that is how you'd do it - but there's no need for the /g modifier in this case.

    If you want to find out more about regular expressions this is the place to go.

  • #9
    New Coder
    Join Date
    Jun 2002
    Posts
    70
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, thanks. I've been to that site, but the reading was pretty high level and it sorta gave me a head ache :P
    -Obiwan Jabroni
    May the Schwartz be With You

  • #10
    Regular Coder
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    126
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There's a thread on the PageResource.com forums where I explained a bit of regex stuff, nothing complex, but it might help you a bit. I think this is it.

    The perlretut page is definitely better, but it is a little heavy in some places.

    Once you get the basic stuff of regexes you'll grow to like them


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •