Helpful Information
 
 
Category: Perl/ CGI
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.

Three questions in one post? Sheesh...

1)
Use split:


$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:


@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.

Heh heh, I guess I'm just very inquisitive :D . 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 :eek: !

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?:eek:

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.

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'.

Ah, thank you.

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.

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 (http://www.perldoc.com/perl5.6.1/pod/perlretut.html) is the place to go.

Ok, thanks. I've been to that site, but the reading was pretty high level and it sorta gave me a head ache :P

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 (http://66.221.73.73/forums/showthread.php3?threadid=8245) 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 :)










privacy (GDPR)