[LA.pm] String handling like C array

Jeff smawhoo at yahoo.com
Sat Feb 4 17:59:03 PST 2006


Robin, 

I do not know how it applies to the swapXY example,
why
I brought up this unpack() is only because you
mentioned substr()
and length() and you want "walk through" the string.

Following is a very simple case, from a string, you
want
get the first 5-byte, skip 3, then grab 2 8-byte
strings, skip 4, then the rest, and you want store
them in @substrings.

@substrings = unpack("A5 x3 A8 A8 X4 A*", $string);

Instead of using substr() or split(),  usually I use
this kind of templates to fetch from well formatted
data.

Hope this explains,
- J

--- Robin Rowe <rower at movieeditor.com> wrote:

> Jeff,
> 
> Interesting. I've never used unpack. Can you show me
> what that could 
> look like if used in the idiom of my SwapXY example?
> 
> Robin
> 
> Jeff wrote:
> > The "array level" needs better definition.
> > 
> > Depends on what you want exactly,  in general, I
> guess unpack()
> > also traverse the string nicely.
> > 
> > Not sure if this answers your question.
> > 
> > - J
> > On Feb 3, 2006, at 2:31 PM, Robin Rowe wrote:
> > 
> >> I often walk through strings as arrays in C, an
> idiom something like
> >> this contrived example:
> >>
> >> void SwapXY(char* string)
> >> {  char* ptr=string;
> >>     while(*ptr)
> >>     {  const char c=*ptr;
> >>        if('x'==c)
> >>        {  *ptr='y';
> >>        }
> >>        else if('y'==c)
> >>        {  *ptr='x';
> >>        }
> >>        ptr++;
> >>     }
> >> }
> >>
> >> In Perl would I walk the string using substr and
> length or is there a
> >> better way to do array-level string manipulation?
> >>
> >> Robin
> >> _______________________________________________
> >> Losangeles-pm mailing list
> >> Losangeles-pm at pm.org
> >> http://mail.pm.org/mailman/listinfo/losangeles-pm
> > 
> > 
> > 
> > 
> _______________________________________________
> Losangeles-pm mailing list
> Losangeles-pm at pm.org
> http://mail.pm.org/mailman/listinfo/losangeles-pm
> 



More information about the Losangeles-pm mailing list