Nother Quick Question

Expand Messages
• I have a string, a large string, and I want to put it in an array 2 chars at a time, so AABBGGHHSS.... would be transefered into like @Array=
Message 1 of 5 , Aug 31, 2003
I have a string, a large string, and I want to put it in an array 2 chars at a
time, so "AABBGGHHSS...." would be transefered into like @Array=
("AA","BB","GG",....)
How would I do that?

What I tried just to see was:

@squares=split(/.{2}/, \$line);

GIDustin

--
Alternate: http://www.civ3files.com
--
When NASA first started sending up astronauts, they quickly discovered that
ballpoint pens would not work in zero gravity. To combat the problem, NASA
scientists spent a decade and 12 Billion Dollars to develop a pen that writes
in zero gravity, upside down, underwater, on almost any surface including
glass
and at temperatures ranging from below freezing to 300C.

The Russians just used a pencil.
--
The 10 Commandments consist of 279 words.
The Declaration of Independence is 300 words long.
The EU Ordinance on the Import of Caramel Sweets consists of 25911 words...
• ... split() is the wrong tool for the job. Use this instead: my @squares = \$line =~ /.{2}/g; -- Jeff japhy Pinyan japhy@pobox.com
Message 2 of 5 , Sep 1 12:45 AM
On Aug 31, Dustin Smith AKA GIDustin said:

>I have a string, a large string, and I want to put it in an array 2 chars
>at a time, so "AABBGGHHSS...." would be transefered into like @Array=
>("AA","BB","GG",....) How would I do that?

>@squares=split(/.{2}/, \$line);

split() is the wrong tool for the job. Use this instead:

my @squares = \$line =~ /.{2}/g;

--
Jeff "japhy" Pinyan japhy@... http://www.pobox.com/~japhy/
RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
[ I'm looking for programming work. If you like my work, let me know. ]
• ... To expand on this slightly, the *reason* that split is the wrong tool for the job is that the pattern you use with split is the delimiter, and the
Message 3 of 5 , Sep 1 7:31 AM
3:45am, Jeff 'japhy' Pinyan wrote:

> On Aug 31, Dustin Smith AKA GIDustin said:
>
> >I have a string, a large string, and I want to put it in an array 2 chars
> >at a time, so "AABBGGHHSS...." would be transefered into like @Array=
> >("AA","BB","GG",....) How would I do that?
>
> >@squares=split(/.{2}/, \$line);
>
> split() is the wrong tool for the job. Use this instead:
>
> my @squares = \$line =~ /.{2}/g;
>

To expand on this slightly, the *reason* that split is the wrong tool for
the job is that the pattern you use with split is the delimiter, and the
delimiter is removed from the string. So you're saying every two characters
(much more simply written as just /../, BTW) are delimiters, so remove
them--leaving you with zilch.

Paul
• ... 2 chars ... tool for ... characters ... And if you retain the delimiters with: @squares=split(/(..)/, \$line); # Note brackets. you will get: AA , ,
Message 4 of 5 , Sep 1 8:06 AM
--- In perl-beginner@yahoogroups.com, Paul Archer <tigger@i...> wrote:
> 3:45am, Jeff 'japhy' Pinyan wrote:
>
> > On Aug 31, Dustin Smith AKA GIDustin said:
> >
> > >I have a string, a large string, and I want to put it in an array
2 chars
> > >at a time, so "AABBGGHHSS...." would be transefered into like @Array=
> > >("AA","BB","GG",....) How would I do that?
> >
> > >@squares=split(/.{2}/, \$line);
> >
> > split() is the wrong tool for the job. Use this instead:
> >
> > my @squares = \$line =~ /.{2}/g;
> >
>
> To expand on this slightly, the *reason* that split is the wrong
tool for
> the job is that the pattern you use with split is the delimiter, and the
> delimiter is removed from the string. So you're saying every two
characters
> (much more simply written as just /../, BTW) are delimiters, so remove
> them--leaving you with zilch.
>
> Paul

And if you retain the delimiters with:
@squares=split(/(..)/, \$line); # Note brackets.
you will get:
'AA', '', 'BB', '', 'CC', etc
i.e. the delimiters (AA, BB etc) are retained along with the items
between them (which are all zero length).
• Thank you to all who replied. Worked like a charm GIDustin ... -- My Home Page: http://www.gidustin.net Alternate: http://www.civ3files.com -- When NASA
Message 5 of 5 , Sep 1 1:23 PM
Thank you to all who replied. Worked like a charm

GIDustin

Quoting Damien Carbery <daymobrew@...>:

> --- In perl-beginner@yahoogroups.com, Paul Archer <tigger@i...> wrote:
> > 3:45am, Jeff 'japhy' Pinyan wrote:
> >
> > > On Aug 31, Dustin Smith AKA GIDustin said:
> > >
> > > >I have a string, a large string, and I want to put it in an array
> 2 chars
> > > >at a time, so "AABBGGHHSS...." would be transefered into like @Array=
> > > >("AA","BB","GG",....) How would I do that?
> > >
> > > >@squares=split(/.{2}/, \$line);
> > >
> > > split() is the wrong tool for the job. Use this instead:
> > >
> > > my @squares = \$line =~ /.{2}/g;
> > >
> >
> > To expand on this slightly, the *reason* that split is the wrong
> tool for
> > the job is that the pattern you use with split is the delimiter, and the
> > delimiter is removed from the string. So you're saying every two
> characters
> > (much more simply written as just /../, BTW) are delimiters, so remove
> > them--leaving you with zilch.
> >
> > Paul
>
> And if you retain the delimiters with:
> @squares=split(/(..)/, \$line); # Note brackets.
> you will get:
> 'AA', '', 'BB', '', 'CC', etc
> i.e. the delimiters (AA, BB etc) are retained along with the items
> between them (which are all zero length).
>
>
>
>
>
> Unsubscribing info is here:
> http://help.yahoo.com/help/us/groups/groups-32.html
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>
>

--
Alternate: http://www.civ3files.com
--
When NASA first started sending up astronauts, they quickly discovered that
ballpoint pens would not work in zero gravity. To combat the problem, NASA
scientists spent a decade and 12 Billion Dollars to develop a pen that writes
in zero gravity, upside down, underwater, on almost any surface including
glass
and at temperatures ranging from below freezing to 300C.

The Russians just used a pencil.
--
The 10 Commandments consist of 279 words.
The Declaration of Independence is 300 words long.
The EU Ordinance on the Import of Caramel Sweets consists of 25911 words...
Your message has been successfully submitted and would be delivered to recipients shortly.
»
«