character manipulation

L

lyoute

hi

how can i access character by character in a scalar?
just like using a $scalar as a character array in C.

thanks
 
J

Jürgen Exner

lyoute said:
how can i access character by character in a scalar?
just like using a $scalar as a character array in C.

perldoc -f substr
perldoc -f split

jue
 
S

Sam Holden

hi

how can i access character by character in a scalar?
just like using a $scalar as a character array in C.

substr($scalar,$index,1);

Or split them up into an array first:

@chars = $scalar=~m/(.)/sg

Or usually, do whataver you need to do in a more "perlish" fashion, which
usually doesn't involve treating strings as arrays of characters.
 
L

lyoute

Thanks a lot.

actually i reading some files like:

HA[9]AB[pd][dp][dd][pp][dj][pj][jp][jd][jj]
;W[mc];B[nc];W[nb];B[oc];W[ob];B[md];W[lc];B[ld];W[kd];B[kc];W[ke]
;B[kb];W[lb];B[pb];W[jc];B[jb];W[ic];B[ib];W[hc];B[hb];W[gc];B[gb];W[fc]
;B[eb];W[ma];B[fb];W[oa];B[ie];W[nd];B[me];W[od];B[pc];W[nf];B[ne];W[oe]

;W[mc] means White color on location y = (ord 'm') - (ord 'a') , x =
(ord 'c' - ord 'a')
;B[] means Black color on blar blar blar.

AB[pd][dp]...
means Black color on location [pd][dp][dd][pp][dj][pj][jp][jd][jj]

i just can't think of a clever way to build this map....
 
B

Ben Morrow

lyoute said:
actually i reading some files like:

HA[9]AB[pd][dp][dd][pp][dj][pj][jp][jd][jj]
;W[mc];B[nc];W[nb];B[oc];W[ob];B[md];W[lc];B[ld];W[kd];B[kc];W[ke]
;B[kb];W[lb];B[pb];W[jc];B[jb];W[ic];B[ib];W[hc];B[hb];W[gc];B[gb];W[fc]
;B[eb];W[ma];B[fb];W[oa];B[ie];W[nd];B[me];W[od];B[pc];W[nf];B[ne];W[oe]

;W[mc] means White color on location y = (ord 'm') - (ord 'a') , x =
(ord 'c' - ord 'a')
;B[] means Black color on blar blar blar.

AB[pd][dp]...
means Black color on location [pd][dp][dd][pp][dj][pj][jp][jd][jj]

# untested

my @data;

while (<DATA>) {
chomp;
s/^HA\[9\]//; # you don't specify what that means
s/A (.) ( (?:\[..\])* )/map ";$1[$_]", split /\]\[/, substr $2, 1, -1/egx;

# now, how do we want the data? I'll make an AoA, with 'B', 'W' or
# undef in each slot.

for(split /;/) {
$_ or next;
/(.) \[ (.)(.) \]/ or die "invalid data format";
$data[ ord($3) - ord('a') ]
[ ord($2) - ord('a') ] = $1;
}
}

__END__
HA[9]AB[pd][dp][dd][pp][dj][pj][jp][jd][jj]
;W[mc];B[nc];W[nb];B[oc];W[ob];B[md];W[lc];B[ld];W[kd];B[kc];W[ke]
;B[kb];W[lb];B[pb];W[jc];B[jb];W[ic];B[ib];W[hc];B[hb];W[gc];B[gb];W[fc]
;B[eb];W[ma];B[fb];W[oa];B[ie];W[nd];B[me];W[od];B[pc];W[nf];B[ne];W[oe]

Ben
 
B

Bob Walton

lyoute said:
Thanks a lot.

actually i reading some files like:

HA[9]AB[pd][dp][dd][pp][dj][pj][jp][jd][jj]
;W[mc];B[nc];W[nb];B[oc];W[ob];B[md];W[lc];B[ld];W[kd];B[kc];W[ke]
;B[kb];W[lb];B[pb];W[jc];B[jb];W[ic];B[ib];W[hc];B[hb];W[gc];B[gb];W[fc]
;B[eb];W[ma];B[fb];W[oa];B[ie];W[nd];B[me];W[od];B[pc];W[nf];B[ne];W[oe]

;W[mc] means White color on location y = (ord 'm') - (ord 'a') , x =
(ord 'c' - ord 'a')
;B[] means Black color on blar blar blar.

AB[pd][dp]...
means Black color on location [pd][dp][dd][pp][dj][pj][jp][jd][jj]

i just can't think of a clever way to build this map....
....

Well, if that is really literally the way your data is, you could do
something like:

use warnings;
use strict;
use Data::Dumper;
my %colorcode=(';B','black',';W','white','AB','black','AW','white');
my $in;
my @color;
{local $/;$in=<DATA>} #slurp
while($in=~/([A-Z;]{2})((?:\[[a-z][a-z]\])+)/g){
my $type=$1;
my $val=$2;
while($val=~/\[([a-z])([a-z])\]/g){
my $y=ord($1)-ord('a');
my $x=ord($2)-ord('a');
$color[$x][$y]=$colorcode{$type};
}
}
print Dumper(@color);

__END__
HA[9]AB[pd][dp][dd][pp][dj][pj][jp][jd][jj]
;W[mc];B[nc];W[nb];B[oc];W[ob];B[md];W[lc];B[ld];W[kd];B[kc];W[ke]
;B[kb];W[lb];B[pb];W[jc];B[jb];W[ic];B[ib];W[hc];B[hb];W[gc];B[gb];W[fc]
;B[eb];W[ma];B[fb];W[oa];B[ie];W[nd];B[me];W[od];B[pc];W[nf];B[ne];W[oe]
 
M

Matt Garrish

Robin said:
substr...

It's nice that you want to help others, and solving others' problems is a
good way to improve your own skills, but responses like the above are hardly
helpful. You haven't even shown that you know how to use substr to do what
he was asking. As you've already seen, you're also going to elicit a lot of
angry responses when you get things wrong, as everyone is aware you are are
a newbie and they don't want to be proofing your responses every time you
post. I would recommend you lurk quietly and improve your knowledge. You're
currently well on your way to replacing Godzilla, and that's not a good
thing:
http://groups.google.com/[email protected]&rnum=1

Matt
 

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
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top