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


Just trying to do below on array stuff

actually iam stuck with the below , so iam putting the following piece of code from my main script
while (<process file delimited by ~ symbol>){ @data = split(/\~/,$_); # will have around 30 elems # $data[23] will have user preference column selection # user can enter 3,3-7,4,9-13 etc anything he desires $data[23] =~ s/-/\.\./; # convert - to .. perl syntax # 23rd elements contains the range entered by user # for ex: 2,3-8,11-20 # i need to extract only above range ignoring rest push(@range,eval $array[23]); # i expanded here @range = map { --$_ } @range; # decrementing since array starts + from 0 @range = grep { !$seen{$_}++ } @range; # remove duplicates in c +ase user enters like 2,1-10 etc @range = sort { $a <=> $b } @range; # Sort the indices in ascen +ding order like 1,2,3,4,5,6 and so on my @finalarray = @data[@range];


iam having trouble when i print final array i get all junk values, iam sure something is wrong or there can be another good approach to achieve above.


you help is highly appreciated and helpful for me.

i have spent quite sometime on above today

Replies are listed 'Best First'.
Re: Array Exclusion Operationss
by dragonchild (Archbishop) on Jul 09, 2008 at 13:57 UTC
    Add the following to the top:
    use strict; use warnings FATAL => 'all';
    Fix all the problems that arise. Your problems will be much clearer now.

    As for your code, there are modules that deal with that. You really don't want to use eval. If you can't use modules for some reason, here's a version that should work for you (untested!):

    sub get_numbers_from_ranges { my $spec = shift; my %numbers; foreach my $x ( split ',', $spec ) { if ( $x =~ /(\d+)-(\d+)/ ) { $numbers{$_} = undef for $1 .. $2; } else { $numbers{$x} = undef; } } return map { $_ - 1 } sort { $a <=> $b } keys %numbers; }

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

      dragonchild, you know what, your solution is excellent, i should have used map and hash in this way , i got what i intended
Re: Array Exclusion Operationss
by pc88mxer (Vicar) on Jul 09, 2008 at 13:48 UTC
    Could it be:
    $data[23] =~ s/-/\.\./; # convert - to .. perl syntax ^^^^^ ... push(@range,eval $array[23]); # i expanded here ^^^^^^
    Also, you are forgetting a /g modifier on your regex:
    $data[23] =~ s/-/\.\./g;
    Those two changes should fix your problem.
Re: Array Exclusion Operationss
by moritz (Cardinal) on Jul 09, 2008 at 13:45 UTC
    There's no way we can tell you what's wrong without seeing runnable code, sample input, sample output and desired output. See also How (Not) To Ask A Question.

    One thing that strikes me is a bad is the usage of eval without sanity checks on the input (security risk), and not checking $@ afterwards to see if it went wrong.


      Sorry, i didnt put data because its huge, i was looking more into approach i.e. if i have done something wrong interms of array operations

      Thx for pointing out eval one, good one, noted it
        Sorry, i didnt put data because its huge
        --- [from wn] --- sample n 1: a small part of something intended as representative of the whole