How to split 6 digits into 3 lots of 2

2003-01-28 Thread Phil Pereira
Hey all,

I heard there were a few people here who knew a bit about Perl :)

Is there an easy way to split "123456" into "12-34-56"?

I've been splitting with a basic // into an array, and then printing 2 array elements 
at a time, sorta like:

$array[0] . $array[1] 

So - is there an easier way?

Thanks in advance.

-- 
Phil.
---
   (_ )
UNIX is "user-friendly",\\\", ) ^
it's just picky about its friends!\/, \(
 cXc_/_)
---




Re: How to split 6 digits into 3 lots of 2

2003-01-28 Thread Paul Makepeace
On Tue, Jan 28, 2003 at 11:22:48PM +, Phil Pereira wrote:
> Is there an easy way to split "123456" into "12-34-56"?

$ perl -lne 'print "$1-$2-$3" if /(\d\d)(\d\d)(\d\d)/'
123456
12-34-56
$

This should start a good TIMTOWTDI thread :)

Paul

-- 
Paul Makepeace ... http://paulm.com/

"What is six pence halfpenny? Two cabbages and a giraffe."
   -- http://paulm.com/toys/surrealism/




Re: How to split 6 digits into 3 lots of 2

2003-01-28 Thread Paul Johnson
On Tue, Jan 28, 2003 at 11:35:29PM +, Paul Makepeace wrote:
> On Tue, Jan 28, 2003 at 11:22:48PM +, Phil Pereira wrote:
> > Is there an easy way to split "123456" into "12-34-56"?
> 
> $ perl -lne 'print "$1-$2-$3" if /(\d\d)(\d\d)(\d\d)/'
> 123456
> 12-34-56
> $
> 
> This should start a good TIMTOWTDI thread :)

If you insist:

echo 123456 | perl -lne 'print join "-", /../g'

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net




Re: How to split 6 digits into 3 lots of 2

2003-01-28 Thread Jody Belka
Paul Johnson said:
> On Tue, Jan 28, 2003 at 11:35:29PM +, Paul Makepeace wrote:
>> On Tue, Jan 28, 2003 at 11:22:48PM +, Phil Pereira wrote:
>> > Is there an easy way to split "123456" into "12-34-56"?
>>
>> $ perl -lne 'print "$1-$2-$3" if /(\d\d)(\d\d)(\d\d)/'
>> 123456
>> 12-34-56
>> $
>>
>> This should start a good TIMTOWTDI thread :)
>
> If you insist:
>
> echo 123456 | perl -lne 'print join "-", /../g'

Or just for fun:

echo 123456 | perl -lne 'print join "-", unpack "a2a2a2", $_'


Jody






Re: How to split 6 digits into 3 lots of 2

2003-01-28 Thread Damian Conway
Paul Johnson wrote:


 If you insist:

echo 123456 | perl -lne 'print join "-", /../g'


Are you *mad*?! Do you think keystrokes grow on *trees*??!!
There's about to be a war on, you know! Start conserving those
characters now, citizen!

  echo 123456 | perl -ple's/..(?=.)/$&-/g'


;-)

Damian





Re: How to split 6 digits into 3 lots of 2

2003-01-28 Thread Paul Makepeace
On Tue, Jan 28, 2003 at 04:35:46PM -0800, Damian Conway wrote:
> Paul Johnson wrote:
> 
> > If you insist:
> >
> >echo 123456 | perl -lne 'print join "-", /../g'
> 
> Are you *mad*?! Do you think keystrokes grow on *trees*??!!
> There's about to be a war on, you know! Start conserving those
> characters now, citizen!
> 
>   echo 123456 | perl -ple's/..(?=.)/$&-/g'

Hmm, time to whip out the mind-twisting metacharacters,

echo 123456 | perl -ple's/..\B/$&-/g'

Patriotically yours,
Paul

-- 
Paul Makepeace ... http://paulm.com/

"What is the swirly thing in my coffee trying to tell me? If higher,
 then fewer."
   -- http://paulm.com/toys/surrealism/




Re: How to split 6 digits into 3 lots of 2

2003-01-28 Thread Damian Conway
Paul Makepeace wrote:


Hmm, time to whip out the mind-twisting metacharacters,

echo 123456 | perl -ple's/..\B/$&-/g'

>
> Patriotically yours,
> Paul

Well done, thet mahn! See cheps, thets the spirit! Aht this rayte we'll
jolly well heve Johnny Foreigner orn the bally wropes in noh tahme! Tally ho!

Damian





Re: How to split 6 digits into 3 lots of 2

2003-01-29 Thread Mark Fowler
On Tue, 28 Jan 2003, Phil Pereira wrote:

> Is there an easy way to split "123456" into "12-34-56"?
>
> I've been splitting with a basic // into an array, and then printing 2
> array elements at a time, sorta like:  $array[0] . $array[1]

Golf solutions aside (where people try and solve it in the fewest possible
keystrokes) lets see what you're trying to do.

Ideally you'd want an array that looks like

 @array = ( "12", "34", "56" );

As then you can simply use the join operator to join this list together to
get the output you want

 $output = join "-", @array;

Of course, there's more than one way to solve it. One way to solve this
problem is to use a loop to take the first two chars from the string each
time you run over the loop.

  my @array;
  while (length($input))
  {
# take the first two chars from the string
# each loop and replace it with the empty string
my $twochar = substr $input, 0, 2, "";

# add it to the array
push @array, $twochar;
  }

This will of course destroy your input string, so you might want to copy
it first.  See "perldoc -f substr" for more info.

Another way is to use a loop with a regular expression to match two chars
with the funky 'g' option to tell it to match each loop starting from
where it left off at the end of the previous loop (so it moves slowly
along the input string).

  my @array;
  while ($input =~ /(..)/g)  # while we match.
  {
# store the thing in the ( ) in the regex in @array.
push @array, $1;
  }

This regular expression uses the "." pattern, meaning 'match any char' and
uses the braces ( ) so that the .. is placed in each loop $1.  See the
perlrequick and perlregex documentation for more info.

The last one I can think of (though there will be many more) is to use the
unpack operator.  This is somewhat like a regular expression where you
define a "template" that the string will be broken up into.

  my @array = unpack "(A2)*", $input;

This uses a simple template.  'A' means an ASCII char, '2' means two of
them, and then we use the '( )*' to indicate that it should repeat that
pattern to consume the whole input.  See perldoc -f unpack and perldoc -f
pack for more info.

Mark.

-- 
#!/usr/bin/perl -T
use strict;
use warnings;
print q{Mark Fowler, [EMAIL PROTECTED], http://twoshortplanks.com/};




Re: How to split 6 digits into 3 lots of 2

2003-01-29 Thread Dave Cross

From: Mark Fowler <[EMAIL PROTECTED]>
Date: 1/29/03 10:21:18 AM

> Another way is to use a loop with a regular expression to 
> match two chars with the funky 'g' option to tell it to 
> match each loop starting from where it left off at the end
> of the previous loop (so it moves slowly along the input 
> string).
>
>  my @array;
>  while ($input =~ /(..)/g)  # while we match.
>  {
># store the thing in the ( ) in the regex in @array.
>push @array, $1;
>  }

And that can be simplified to

my @array = $input =~ /(..)/g;

Dave...

-- 


"Let me see you make decisions, without your television"
   - Depeche Mode (Stripped)








Re: How to split 6 digits into 3 lots of 2

2003-01-29 Thread Robin Berjon
Mark Fowler wrote:

The last one I can think of (though there will be many more) is to use the
unpack operator.  This is somewhat like a regular expression where you
define a "template" that the string will be broken up into.

  my @array = unpack "(A2)*", $input;

This uses a simple template.  'A' means an ASCII char, '2' means two of
them, and then we use the '( )*' to indicate that it should repeat that
pattern to consume the whole input.


One of my long-standing Perl weaknesses has been pack and unpack, so I'm curious 
(and perldoc isn't always that helpful here). Does ASCII here mean ASCII or will 
it work on random Unicode strings marked as UTF-8 and containing multibyte 
chars? The U template command is pitched as "A Unicode character number" and 
indeed my fears (and/or incompetence) seem confirmed:

print map { "$_\n" } unpack("A", "A"),  # A
 unpack("U", "A"),  # 65
 unpack("A", "é"),  # Ã
 unpack("A2", "é"), # é (196, 169)
 unpack("U", "é");  # 233

So given the same problem and using unpack, but provided the data is arbitrary 
UTF-8, what is the right template?

--
Robin Berjon <[EMAIL PROTECTED]>
Research Engineer, Expwayhttp://expway.fr/
7FC0 6F5F D864 EFB8 08CE  8E74 58E6 D5DB 4889 2488




Re: How to split 6 digits into 3 lots of 2

2003-01-29 Thread Mark Fowler
On Wed, 29 Jan 2003, Dave Cross wrote:

> And that can be simplified to
>
> my @array = $input =~ /(..)/g;

And that can be simplified in turn to

my $output = join '-', $input =~ /(..)/g;

As operators can give each other list context, which is a whole other
conversation (one that's in Chapter 3.8 of Learning Perl 3rd Edition.)

Mark.

-- 
#!/usr/bin/perl -T
use strict;
use warnings;
print q{Mark Fowler, [EMAIL PROTECTED], http://twoshortplanks.com/};




Re: How to split 6 digits into 3 lots of 2

2003-01-29 Thread Paul Johnson

Mark Fowler said:

> On Wed, 29 Jan 2003, Dave Cross wrote:
>
>> And that can be simplified to
>>
>> my @array = $input =~ /(..)/g;
>
> And that can be simplified in turn to
>
> my $output = join '-', $input =~ /(..)/g;

And that can be simplified in turn to

my $output = join '-', $input =~ /../g;

Which is basically what I wrote last night :-)

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net





Re: How to split 6 digits into 3 lots of 2

2003-01-29 Thread David Cantrell
On Wed, Jan 29, 2003 at 12:49:40AM +0100, Paul Johnson wrote:
> On Tue, Jan 28, 2003 at 11:35:29PM +, Paul Makepeace wrote:
> > On Tue, Jan 28, 2003 at 11:22:48PM +, Phil Pereira wrote:
> > > Is there an easy way to split "123456" into "12-34-56"?
> > $ perl -lne 'print "$1-$2-$3" if /(\d\d)(\d\d)(\d\d)/'
> > This should start a good TIMTOWTDI thread :)
> If you insist:
> echo 123456 | perl -lne 'print join "-", /../g'

Harrumph.  Back in my day we didn't have perl, we had to chisel our
programs into lumps of coal in just the right way that the patterns
in the smoke would display the right result.  We used incantations
like this:

echo 123456 | awk 'BEGIN {FS=""} {print $1$2"-"$3$4"-"$5$6}'

-- 
David Cantrell | Member of the Brute Squad | http://www.cantrell.org.uk/david

Usenet is a co-operative venture, backed by nasty people.
Follow the standards.
  -- Chris Rovers, in the Monastery




Re: How to split 6 digits into 3 lots of 2

2003-01-29 Thread Joel Bernstein
On Wed, Jan 29, 2003 at 01:31:32PM +, David Cantrell wrote:
> On Wed, Jan 29, 2003 at 12:49:40AM +0100, Paul Johnson wrote:
> > On Tue, Jan 28, 2003 at 11:35:29PM +, Paul Makepeace wrote:
> > > On Tue, Jan 28, 2003 at 11:22:48PM +, Phil Pereira wrote:
> > > > Is there an easy way to split "123456" into "12-34-56"?
> > > $ perl -lne 'print "$1-$2-$3" if /(\d\d)(\d\d)(\d\d)/'
> > > This should start a good TIMTOWTDI thread :)
> > If you insist:
> > echo 123456 | perl -lne 'print join "-", /../g'
> 
> Harrumph.  Back in my day we didn't have perl, we had to chisel our
> programs into lumps of coal in just the right way that the patterns
> in the smoke would display the right result.  We used incantations
> like this:
> 
> echo 123456 | awk 'BEGIN {FS=""} {print $1$2"-"$3$4"-"$5$6}'

Which of course rolls even neater to:

echo 123456 | awk -F "" '{print $1$2"-"$3$4"-"$5$6}';

these are, of course, synonymous, but mine wins the golf prize. 

Setting OFS to "-" in Awk might make it possible to shrink stuff even more,
but I can't be bothered.

/joel




Re: How to split 6 digits into 3 lots of 2

2003-01-30 Thread hh14067
please stop emails


Re: How to split 6 digits into 3 lots of 2

2003-01-30 Thread Earle Martin
On Thu, Jan 30, 2003 at 07:55:14PM -0500, [EMAIL PROTECTED] wrote:
> please stop emails

Sorry, only the Internet Controller can do that, and he's very busy right
now.


-- 
$x='4a75737420616e6f74686572205065726c'#Earle Martin
.'206861636b65720d0a';for(0..26){print #http://downlode.org/
chr(hex(substr($x,$y,2)));$y=$y+2;}#   http://grault.net/grubstreet/