in reply to how to splice an array?

sorry for not being clear in my previous post. i have an array
@array="P H A G E P H A G E P Q K R E P H A G E P W S Q E P H A G E P +R D L E P H A G E"
i cant help it being this way, coz this is the out put which i get from the previous step of the program. i got to split this into 5 characters each and then compare whether any of the pattern matches with "PHAGE". so i want to make it like
PHAGE PHAGE PQKRE PHAGE PWSQE PHAGE PRDLE PHAGE.
and then pattern match it PHAGE. finally i should be printing only 5 PHAGES. hope its clear.

Replies are listed 'Best First'.
Re: a clearer query for how do i splice an array
by ikegami (Patriarch) on Oct 11, 2006 at 17:13 UTC

    Are you sure @array is holding nothing but a string? If so why are you using an array?

    my $str = "P H A G E P H A G E P Q K R E P H A G E P W S Q E P H A G E + P R D L E P H A G E"; $str =~ s/\s+//g; my @words = $str =~ /.{1,5}/g;

    Or do you mean:

    my @array = ('P', 'H', 'A', 'G', 'E', 'P', 'H', 'A', 'G', 'E', 'P', 'Q', 'K', 'R', 'E', 'P', 'H', 'A', 'G', 'E', 'P', 'W', 'S', 'Q', 'E', 'P', 'H', 'A', 'G', 'E', 'P', 'R', 'D', 'L', 'E', 'P', 'H', 'A', 'G', 'E'); my @words; while (@array) { push(@words, join('', splice(@array, 0, 5))); }

    Then, for the second part,

    foreach (@words) { next unless $_ eq 'PHAGE'; print("$_\n"); }
Re: a clearer query for how do i splice an array
by brian_d_foy (Abbot) on Oct 11, 2006 at 19:24 UTC

    What task are you trying to accomplish? Are you simply trying to count the occurences of runs of P H A G E? Do the occurances have to happens at particular points in the string (for instance, starting at a position that is a multiple of 5)?

    If you have a string (or can make it a string), you can use index to find them and count them. Remember that index returns -1 when it doesn't find the substring, and that's a true value.

    #!/usr/bin/perl use strict; use warnings; my $string = "PHAGEPHAGEPQKREPHAGEPWSQEPHAGEPRDLEPHAGE"; my $substring = "PHAGE"; my $count = 0; my $last_pos = -1; NAKED: { $last_pos = index( $string, $substring, $last_pos + 1 ); last if $last_pos == -1; print "Found $substring at $last_pos\n"; $count++; redo; } print "Found $count instances of $substring\n"; __OUTPUT__ Found PHAGE at 0 Found PHAGE at 5 Found PHAGE at 15 Found PHAGE at 25 Found PHAGE at 35 Found 5 instances of PHAGE

    If you have an array and you need to find the occurences among consecutive array entries (perhaps, because the array is huge and you want to avoid a lot of copying), you can do the same sort of thing, although you have to invent your own code to find the indices. Scoot along the array and try the subarray at each position. Once it fails, move the starting position (that's $offset) and try again. Do that until you run out of array.

    #!/usr/bin/perl use strict; use warnings; my @array = split //, "PHAGEPHAGEPQKREPHAGEPWSQEPHAGEPRDLEPHAGE"; my @subarray = split //, "PHAGE"; my $offset = 0; my $count = 0; NAKED: { foreach my $index ( 0 .. $#subarray ) { next if $array[ $offset + $index ] eq $subarray[ $index ]; $offset += 1; redo NAKED; } print "Found @subarray at $offset\n"; $count++; $offset += @subarray; last if $offset + @subarray > @array; redo; } print "Found $count instances of @subarray\n"; __OUTPUT__ Found P H A G E at 0 Found P H A G E at 5 Found P H A G E at 15 Found P H A G E at 25 Found P H A G E at 35 Found 5 instances of P H A G E
    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review
Re: a clearer query for how do i splice an array
by graff (Chancellor) on Oct 12, 2006 at 01:16 UTC
    Maybe you are confused about the perl syntax for initializing an array. Did you mean something like this:
    @array = qw/P H A G E P H A G E P Q K R E P H A G E P W S Q E P H A G +E P R D L E P H A G E/;
    The "qw" quoting operator provides a short-cut for placing quotations marks around each space-delimited token (and putting commas between them as well). So what I've shown above will cause @array to contain 40 elements, with each element being a single letter.

    By contrast, your  @array = "P H A G E ..."; approach causes @array to contain only one element, which is a string of 79 characters (40 letters separated by 39 spaces).

    So if your "previous step of the program" (whatever that may be) is really producing an array of 40 elements with one letter per element, maybe the following would do what you want:

    my $start = 0; my $end = 4; while ( $end <= $#array ) { my $string = join( "", @array[$start..$end]; if ( $string eq 'PHAGE' ) { print "$string found in elements $start .. $end\n"; } $start += 5; $end += 5; }