character manipulation

Discussion in 'Perl Misc' started by lyoute, Jan 12, 2004.

  1. lyoute

    lyoute Guest

    hi

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

    thanks
     
    lyoute, Jan 12, 2004
    #1
    1. Advertising

  2. lyoute wrote:
    > 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
     
    Jürgen Exner, Jan 12, 2004
    #2
    1. Advertising

  3. lyoute

    Sam Holden Guest

    On Mon, 12 Jan 2004 09:13:48 +0800, lyoute <> wrote:
    > 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.


    --
    Sam Holden
     
    Sam Holden, Jan 12, 2004
    #3
  4. lyoute <> wrote:

    > how can i access character by character in a scalar?



    foreach my $char ( split //, $some_string ) { # untested
    print "$char\n";
    }


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jan 12, 2004
    #4
  5. lyoute

    lyoute Guest

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

    lyoute wrote:

    > hi
    >
    > how can i access character by character in a scalar?
    > just like using a $scalar as a character array in C.
    >
    > thanks
     
    lyoute, Jan 12, 2004
    #5
  6. lyoute

    Ben Morrow Guest

    lyoute <> wrote:
    > 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

    --
    $.=1;*g=sub{print@_};sub r($$\$){my($w,$x,$y)=@_;for(keys%$x){/main/&&next;*p=$
    $x{$_};/(\w)::$/&&(r($w.$1,$x.$_,$y),next);$y eq\$p&&&g("$w$_")}};sub t{for(@_)
    {$f&&($_||&g(" "));$f=1;r"","::",$_;$_&&&g(chr(0012))}};t #
    $J::u::s::t, $a::n::eek:::t::h::e::r, $P::e::r::l, $h::a::c::k::e::r, $.
     
    Ben Morrow, Jan 12, 2004
    #6
  7. lyoute

    Bob Walton Guest

    lyoute wrote:

    > 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]

    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
     
    Bob Walton, Jan 12, 2004
    #7
  8. lyoute

    Robin Guest

    substr...

    --
    peace,
    Robin
    --------------

    Please CC a copy of your message to me.
    --------------

    "lyoute" <> wrote in message
    news:4001f4d3$-cable.com...
    > hi
    >
    > how can i access character by character in a scalar?
    > just like using a $scalar as a character array in C.
    >
    > thanks
     
    Robin, Jan 13, 2004
    #8
  9. lyoute

    Matt Garrish Guest

    "Robin" <> wrote in message
    news:btvpfp$fca$...
    >
    > 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/groups?q=s...elm=&rnum=1

    Matt
     
    Matt Garrish, Jan 13, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Velvet
    Replies:
    9
    Views:
    14,834
    Joerg Jooss
    Jan 19, 2006
  2. raavi
    Replies:
    2
    Views:
    913
    raavi
    Mar 2, 2006
  3. Integer and character manipulation

    , Sep 5, 2005, in forum: C Programming
    Replies:
    10
    Views:
    570
    Lawrence Kirby
    Sep 6, 2005
  4. Replies:
    12
    Views:
    626
    Keith Thompson
    Oct 4, 2006
  5. Adelson Anton

    Character Manipulation

    Adelson Anton, Apr 24, 2004, in forum: Javascript
    Replies:
    9
    Views:
    136
    Thomas 'PointedEars' Lahn
    May 4, 2004
Loading...

Share This Page