Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Putting special elements from a row into an array

by ultibuzz (Monk)
on Mar 22, 2007 at 12:40 UTC ( #606004=perlquestion: print w/ replies, xml ) Need Help??
ultibuzz has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,
after great help from tye and ambros on CB i get this code to work(regex done by tye and ambros)

#!"C:\perl\bin\perl.exe" use warnings; use strict; open(INFILE, '<', 'test_data.txt') or die("open failed: $!"); while (<INFILE>) { my ($ezb, $numbers) = split(';',$_); my( $pre, $in, $post )= $numbers =~ /^ ([^()]*) \( ([^()]*) \) ([^ +()]*) $/x; #my @number_split = split(',', $numbers); print "$pre\t$in\t$post\n"; }
here is some data
201.1;201(27, 28, 3, 4, 83, 84, 890, 897, 898, 9), 2054 201.2;201(1, 20-26, 29, 80-82, 891-896, 899) 201.3;201(52-54, 6, 75, 76, 85-88) 201.4;201(50, 51, 55-59, 70-74, 77-79)

the above code works for the mentioned data.


now i get something like this data

2302.1;2302, 2304(3-8, 92-99), 2305(2, 4-9) 231.1;2301, 2303, 2304(1, 2, 90, 91), 2305(1, 3), 2306, 2307, 2308

best woud be if i can place every NUMBER(numbers) in one array and the rest in one array, the stuff infront of the ; can be ignored will be splited away

idears and help greatly welcome
i tryed splitting at ( and count and recombine but this ends in a mess of nonsens
kd ultibuzz

Comment on Putting special elements from a row into an array
Select or Download Code
Replies are listed 'Best First'.
Re: Putting special elements from a row into an array
by agianni (Hermit) on Mar 22, 2007 at 12:59 UTC

    It's unclear what you are trying to do. Can you give an example of the output you are hoping for?

    split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(join(q{},map +{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79*36997,13 +*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));

      sure
      Output

      i want 2 arrays based on the row that is in the while atm one shoud contain @a=('2302') the other shoud contain @b=('2304(3-8, 92-99)', '2305(2, 4-9)')
      this is what i need on row basis

      kd ultibuzz

        Split isn't going to get your far on this. What you need is a good regex. Try this:

        while ( my $line = <DATA>){ my( @a, @b ); my ( $junk, $useful ) = split /\;/, $line; my @elements = $useful =~ m/ (\d+ # some numbers (?: # followed by (don't capture) \(.*?\) # other stuff (non-greedy) in parens )? # (optionally) ) # end capture /smxg; for my $element ( @elements ){ if ( $element =~ /\(.*\)/ ){ push @b, $element; } else{ push @a, $element; } } # do something with @a and @b } __DATA__ 2302.1;2302, 2304(3-8, 92-99), 2305(2, 4-9) 231.1;2301, 2303, 2304(1, 2, 90, 91), 2305(1, 3), 2306, 2307, 2308

        I'm sure you could do this more compactly, but I didn't take the time to optimize. The first RE does the split you're looking for, followed by a check for each item to see if it contains parens.

        perl -e 'split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(joi +n(q{},map{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79 +*36997,13*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));'
Re: Putting special elements from a row into an array
by shmem (Canon) on Mar 22, 2007 at 14:44 UTC
    best woud be if i can place every NUMBER(numbers) in one array and the rest in one array, the stuff infront of the ; can be ignored will be splited away
    Your specs are unclear; your working example does not do that. Instead, the first \d+ ends up in $pre, the stuff in parens in $in and the remainder in $post.

    I guess you want something that keeps doing that, but stuffs the \d+ cuddled to an opening paren into $in if there are more numbers up front. Quick shot:

    #!/usr/bin/perl use warnings; use strict; while (<DATA>) { chomp; my ($ezb, $numbers) = split(';',$_); my( $pre, $in, $post )= $numbers =~ /^(\d+(?=\()|[\d+, ]+?)(\d*\(. +*\))(.*)/; print "$_:\n \$pre = '$pre\n \$in = '$in'\n \$post = '$post' +\n"; } __DATA__ 201.1;201(27, 28, 3, 4, 83, 84, 890, 897, 898, 9), 2054 201.2;201(1, 20-26, 29, 80-82, 891-896, 899) 201.3;201(52-54, 6, 75, 76, 85-88) 201.4;201(50, 51, 55-59, 70-74, 77-79) 2302.1;2302, 2304(3-8, 92-99), 2305(2, 4-9) 231.1;2301, 2303, 2304(1, 2, 90, 91), 2305(1, 3), 2306, 2307, 2308

    outputs

    201.1;201(27, 28, 3, 4, 83, 84, 890, 897, 898, 9), 2054: $pre = '201 $in = '(27, 28, 3, 4, 83, 84, 890, 897, 898, 9)' $post = ', 2054' 201.2;201(1, 20-26, 29, 80-82, 891-896, 899): $pre = '201 $in = '(1, 20-26, 29, 80-82, 891-896, 899)' $post = '' 201.3;201(52-54, 6, 75, 76, 85-88): $pre = '201 $in = '(52-54, 6, 75, 76, 85-88)' $post = '' 201.4;201(50, 51, 55-59, 70-74, 77-79): $pre = '201 $in = '(50, 51, 55-59, 70-74, 77-79)' $post = '' 2302.1;2302, 2304(3-8, 92-99), 2305(2, 4-9): $pre = '2302, $in = '2304(3-8, 92-99), 2305(2, 4-9)' $post = '' 231.1;2301, 2303, 2304(1, 2, 90, 91), 2305(1, 3), 2306, 2307, 2308: $pre = '2301, 2303, $in = '2304(1, 2, 90, 91), 2305(1, 3)' $post = ', 2306, 2307, 2308'

    Is that what you seek to accomplish?

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

      there are 2 data models, ure script will work with both of them
      wich is nice ;), but the other also works great because these 2 datamodels shoud not be mixed up ever
      kd ultibuzz

        there are 2 data models, ure script will work with both of them
        It is either "ur" or "your", but I prefer the latter.

        sry scnr kk cu

        ;-)

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://606004]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2015-07-08 08:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (96 votes), past polls