Looking at a character

I

ipellew

Hi all;

Whats the fasted way to look at a single character?

my $s = "a1b2";
if ( $s[0] eq "a" ) {
print "we got it";
} else {
print "Not to start";
}

Of course this is not valid.

Regards
Ian
 
J

Jürgen Exner

Whats the fasted way to look at a single character?

There are no single characters in Perl.
The closest thing to such a concept would be a string of length 1.
my $s = "a1b2";
if ( $s[0] eq "a" ) {

You don't have an array @s, therefore $s[0] should give you an error message
under strictures.

Now, if you would explain what you mean by "look at a single character",
them maybe we could suggest a solution.

- Do you want to retrieve the character at position n to print it (I guess
this is the only way to "look at a character")?
- Do you want to check if the character at position n matches/equals/doesn't
equal a given character/RE/character class/... (that's what your code
suggests)?
- Something completely different?

jue
 
S

Sherm Pendley

Whats the fasted way to look at a single character?

my $s = "a1b2";
if ( $s[0] eq "a" ) {

In the above example (which is more C than Perl, by the way, but I think
you knew that) you check to see if the first character of $s is "a". You
can do that with a simple regular expression, with no need to "look at"
the first character as a separate variable:

if ($s =~ /^a/) { ... }

Or, if the character is part of a pattern that might appear at any point
within the string, you could use "(.)" within a regex to capture the
character in a subexpression:

my ($first, $last) = ($s =~ /^(.).*(.)$/);

If you want an array with all of the characters as elements, split() the
string with an empty delimiter:

my $chars = split(//, $s);

Of course, you could use substr() to extract a single character from a
string, if you know the index at which it appears within the string:

my $first = substr($s, 0);

And that's without considering the handful of oddball functions for
doing various things to single characters within a string, for example
ucfirst() or lcfirst().

In short, there are many ways to "look at" a single character. Which one
is best is very much dependent on *why* you want to look at it.

sherm--
 
P

Paul Lalli

daniel kaplan said:
so if you are coming from that backround, just wanted to say, keep a pad
with the proper ways to do "what you do reflexivly in C" in Perl....saved me
grief

You may also want to keep
perldoc perltrap
handy. It lists common traps among Perl programmers who came from other
languages - including C. (Although, somewhat surprisingly, the == vs eq
trap is not listed...)

Paul Lalli
 
S

Sherm Pendley

Paul said:
You may also want to keep
perldoc perltrap
handy. It lists common traps among Perl programmers who came from other
languages - including C. (Although, somewhat surprisingly, the == vs eq
trap is not listed...)

I suppose it'd be more like the strncmp() vs. cmp trap - but you're
right, it should be in there.

sherm--
 
P

Paul Lalli

Sherm Pendley said:
I suppose it'd be more like the strncmp() vs. cmp trap - but you're
right, it should be in there.

Well, the specific trap I'd be thinking of is the following:
#include <iostream>
#include <string>
using namespace std;

int main (){

string str1 = "foo";
string str2 = "bar";

if (str1 == str2){
cout << "Equal" << endl;
} else {
cout << "Not Equal" << endl;
}

return 0;
}

(yes, I know it's C++ rather than C)

as compared to:
#!/usr/bin/env perl
use strict;

my $str1 = "foo";
my $str2 = "bar";

if ($str1 == $str2){
print "Equal\n";
} else {
print "Not Equal\n";
}

__END__

Now, granted, perl *will* give a warning for this, if they're enabled.
But I would still consider it a trap of sorts.

Paul Lalli
 
S

Sherm Pendley

Paul said:
(yes, I know it's C++ rather than C)

I've been trying to forget about C++. :)

I wasn't thinking of C++'s string classes and an overloaded ==. I was
thinking in plain ol' C.

sherm--
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,268
Messages
2,571,095
Members
48,773
Latest member
Kaybee

Latest Threads

Top