1

I was trying to match the following line

      5474c2ef012a759a c11ab88ae8daa276 63693b53799c91f1 be1d8c8738733d80 

with

  if(/[[:xdigit:]{8}[:xdigit:]{8}\s]{4}/)

Is there anyway I populate the automatic variables $1,$2,$3..$8 etc with half of each of those words. i.e

  $1=5474c2ef
  $2=012a759a
  $3=c11ab88a
  $4=e8daa276
  $5=63693b53
  $6=799c91f1
  $7=be1d8c87
  $8=38733d80
Jean
  • 21,665
  • 24
  • 69
  • 119

4 Answers4

1

You could capture them in an array:

use strict;
use warnings;
use Data::Dumper;

$_ = '5474c2ef012a759a c11ab88ae8daa276 63693b53799c91f1 be1d8c8738733d80 '; 

my @nums = /\G(?:([[:xdigit:]]{8})([[:xdigit:]]{8})\s)/g;
if (@nums >= 8) {
    print Dumper(\@nums);
}

(may behave differently than the original if there are more than four or if there're earlier 16-hex-digit sequences separated by more than just a space).

ysth
  • 96,171
  • 6
  • 121
  • 214
  • I really dislike `Data::Dumper`. – tchrist May 06 '11 at 00:47
  • Wherefore I don’t use it. (*wherefore* means “which is why” or “and so”) – tchrist May 06 '11 at 00:55
  • I don’t use it because I find its output horrible to read. Within the Perl CORE modules, `Dumpvalue` serves my purposes better, which is reading the output. But `Data::Dump` [ *sic:* no -er] from CPAN is even nicer, because it doesn’t have a miserable interface. – tchrist May 06 '11 at 01:03
  • fortunately my English allows words multiple meanings :) – ysth May 06 '11 at 01:22
  • I usually use Data::Dumper with Terse and Useqq set, so not much different (other than Data::Dump not allowing indentation control) – ysth May 06 '11 at 01:27
  • That’s what Humpty-Dumpty said: a word can mean anything he wants it to mean. Which is a lot like saying a program can be made as fast as you want if you don’t care about correctness. Here’s [the OED entry on wherefore](http://www.oed.com/view/Entry/228218?redirectedFrom=wherefore#eid). :) – tchrist May 06 '11 at 01:28
  • I find all the `$VAR1` nonsense annoying. I am not a compiler. I want to read how the data is laid out. – tchrist May 06 '11 at 01:29
  • OED is paywalled. 1913 Webster, gcide, wordnet all include a "why" definition. Terse removes unneeded $VAR1 nonsense. – ysth May 06 '11 at 01:40
  • I. Interrogative uses. 1. For what? esp. for what purpose or end? 2. For what cause or reason? on what account? why? II. Relative uses. 3. For which. Now distinguished by stress and spelling (*whereˈfor*). †without anything wherefore, without a return or equivalent; †to do wherefore, to make a return, give an equivalent. 4. On account of or because of which; in consequence or as a result of which. 5a. Introducing a clause expressing a consequence or inference from what has just been stated: On which account; for which reason; which being the case; and therefore. (Now always *ˈwherefore.*) – tchrist May 06 '11 at 01:57
  • I only use it as a relative myself, not an interrogative. So I was completing the relative clause. Sorry. – tchrist May 06 '11 at 01:58
1

How about:

my $pat = '([[:xdigit:]]{8})\s?' x 8;
# produces: ([[:xdigit:]]{8})\s?([[:xdigit:]]{8})\s?....
/$pat/;

Update if you need to be strict on the spacing requirement:

my $pat = join('\s', map{'([[:xdigit:]]{8})' x 2} (1..4));
# produces: ([[:xdigit:]]{8})([[:xdigit:]]{8})\s....
/$pat/;
dwarring
  • 4,794
  • 1
  • 26
  • 38
0
use strict;
use warnings;
use Data::Dumper;

$_ = '5474c2ef012a759a c11ab88ae8daa276 63693b53799c91f1 be1d8c8738733d80 '; 

if (/((?:[[:xdigit:]]{16}\s){4})/) {
   my @nums = map {  /(.{8})(.{8})/  } split /\s/, $1;
   print Dumper(\@nums);
}

__END__

$VAR1 = [
          '5474c2ef',
          '012a759a',
          'c11ab88a',
          'e8daa276',
          '63693b53',
          '799c91f1',
          'be1d8c87',
          '38733d80'
        ];
toolic
  • 57,801
  • 17
  • 75
  • 117
0

Yes, there is, but you don’t want to.

You just want to do this:

 while ( /(\p{ahex}{8})/g ) { print "got $1\n" }
tchrist
  • 78,834
  • 30
  • 123
  • 180
  • 1
    I want to :) Because I am planning to compute $1+$8, $2+$7 etc – Jean May 06 '11 at 00:56
  • Fine, then say `@nums = /\p{ahex}{8}/g`, and then you have your set, which you can then sum together pairwise as `hex($num[0]) + hex($num[7])`, `hex($num[1]) + hex($num[6])`, `hex($num[2]) + hex($num[5])`, and `hex($num[3]) + hex($num[4])` — assuming those are the pairs you want combined. Wait, do you mean string concatenate not arithmetic sum? – tchrist May 06 '11 at 00:58