maxl90 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to get only the  "[0] = 1" "[1] = 2" type data only, out of this line of code.
OP=SiDarkRefRead&DEV=SI_PHOTO_DEV&CMD=READ&STATUS=PASS&READ_DATA={ 3 1 +5 0 255 50000 1000 [0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5 [5] = 6 [6 +] = 7 [7] = 8 [8] = 9 [9] = 10 [10] = 1 [11] = 2 [12] = 3 [13] = 4 [1 +4] = 5 [15] = 6 [16] = 7 [17] = 8 [18] = 9 [19] = 10 etc...
So the output would look like:
[0] = 1 [1] = 2 [2] = 3 and so on..
As of now I'm suck. I was thinking about using the split command but I wouldn't be sure what to split it on. Let me know your thoughts. Thanks.
Jeff

Replies are listed 'Best First'.
Re: Splitting Data
by allolex (Curate) on Dec 08, 2003 at 20:04 UTC

    You'll probably be happier using a regular expression for this kind of thing.

    vougeot% cat 313208.pl #!/usr/bin/perl use strict; use warnings; my $string = 'OP=SiDarkRefRead&DEV=SI_PHOTO_DEV&CMD=READ&STATUS=PASS&R +EAD_DATA={ 3 15 0 255 50000 1000 [0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] += 5 [5] = 6 [6] = 7 [7] = 8 [8] = 9 [9] = 10 [10] = 1 [11] = 2 [12] = + 3 [13] = 4 [14] = 5 [15] = 6 [16] = 7 [17] = 8 [18] = 9 [19] = 10 et +c... '; my @matches = $string =~ m!\[\d+\]\s+=\s+\d+!g; # regex matches '[', one or more digits, # ']', one or more space characters, '=', # one or more space characters, one or more digits print join "\n", @matches; vougeot% perl 313208.pl [0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5 [5] = 6 [6] = 7 [7] = 8 [8] = 9 [9] = 10 [10] = 1 [11] = 2 [12] = 3 [13] = 4 [14] = 5 [15] = 6 [16] = 7 [17] = 8 [18] = 9

    --
    Allolex

Re: Splitting Data
by duff (Parson) on Dec 08, 2003 at 19:59 UTC
      This is a good candidate for a one liner.
      $ cat temp.txt | perl -ne 'print "$_\n" for /\[\d+\]\s*=\s*\d+/g' [0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5 [5] = 6 [6] = 7 [7] = 8 [8] = 9 [9] = 10 [10] = 1 [11] = 2 [12] = 3 [13] = 4 [14] = 5 [15] = 6 [16] = 7 [17] = 8 [18] = 9 [19] = 10

      --

      flounder

Re: Splitting Data
by Roger (Parson) on Dec 09, 2003 at 06:42 UTC
    Not to recommend this, but I want to bring to your attention that you can do this with split, perhaps you haven't thought about this yet.
    use Data::Dumper; my $str="OP=SiDarkRefRead&DEV=SI_PHOTO_DEV&CMD=READ&STATUS=PASS&READ_D +ATA={ 3 15 0 255 50000 1000 [0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5 [ +5] = 6 [6] = 7 [7] = 8 [8] = 9 [9] = 10 [10] = 1 [11] = 2 [12] = 3 [1 +3] = 4 [14] = 5 [15] = 6 [16] = 7 [17] = 8 [18] = 9 [19] = 10"; my @p = map { $_ =~ /\[/ ? $_ : () } split /(\[\s*\d+\s*\]\s*=\s*\d+)/ +, $str; # ^ only want [dd]=dd ^ split & capture print Dumper(\@p);
    And the output is as expected -
    $VAR1 = [ '[0] = 1', '[1] = 2', '[2] = 3', '[3] = 4', '[4] = 5', '[5] = 6', '[6] = 7', '[7] = 8', '[8] = 9', '[9] = 10', '[10] = 1', '[11] = 2', '[12] = 3', '[13] = 4', '[14] = 5', '[15] = 6', '[16] = 7', '[17] = 8', '[18] = 9', '[19] = 10' ];
Re: Splitting Data
by maxl90 (Sexton) on Dec 10, 2003 at 23:31 UTC
    Thanks. Works great. - Jeff