Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Print from array element to another array element

by Dr Manhattan (Beadle)
on Apr 07, 2013 at 09:25 UTC ( #1027340=perlquestion: print w/replies, xml ) Need Help??
Dr Manhattan has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

I have a word split up by letters into a array. Let's say my word is "catfood", so then 'c' would be $array[0], and 'd' would be $array[6]

I want to print the word 'food' from my array

I tried this-

my $end = $#array; print "$array[3..$end]\n";

and this

my $end = $#array; print "$array[3]..$array[$end]\n";

but it doesn't work. Any help would be appreciated!

Replies are listed 'Best First'.
Re: Print from array element to another array element
by kcott (Chancellor) on Apr 07, 2013 at 09:34 UTC

    G'day Dr Manhattan,

    What you're attempting to use here are called Array Slices. They take the form: @array_name[<list of indices>]. See perldata - Slices. Your code should look something like this:

    $ perl -Mstrict -Mwarnings -E ' my $word = q{catfood}; my @array = split // => $word; say join q{} => @array[3 .. $#array]; ' food

    -- Ken

Re: Print from array element to another array element
by Loops (Curate) on Apr 07, 2013 at 09:35 UTC
    use feature 'say'; my @array = split //, 'catfood'; say @array[3..$#array];

      Hi Loops

      I used the 'say' feature and it works correctly. My next question is- How do I lookup from one element in a array to another one?

      For instance the word "food" is a element in a hash. I want to lookup and if the combined elements of my array exists in the hash it should be printed

      I tried this-

      if (exists $hash{@array[3..$end]}) { say @array[3..$end]; }
        The keys in a hash are always strings. You cannot use an array slice as a hash key. You have to join the list to form a string:
        exists $hash{ join q(), @array[3 .. $end] }
        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        If you want to find one string inside another, just use index

        So, something along these lines :-

        my $text ='catfood'; # returns -1 if not found say index($text,'food',0);
Re: Print from array element to another array element
by choroba (Chancellor) on Apr 07, 2013 at 10:02 UTC
    The problem is that the range operator .. is not recognised in double quotes. This should work, though:
    print @array[3 .. $end], "\n";

    It might be faster and easier, though, not to split the string at all and work with substr and length.

    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Print from array element to another array element
by BillKSmith (Vicar) on Apr 08, 2013 at 02:56 UTC
    You are attempting to write C, not perl. In perl, the word 'catfood' is a string, not an array. We use built-in functions and regular expressions for processing strings. It is possible to split a string into an array of characters, but reference still cannot be mamnipulated like a C pointer.
Re: Print from array element to another array element
by reaper9187 (Scribe) on Apr 08, 2013 at 05:58 UTC
    The easiest way to do it would be to use substr
    use strict; use warnings; my $string = "catfood"; $string = substr($string,3,4); print "$string\n";
    You could also work on a copy of the original string by assigning the value to a separate string say :
    my $new_string = substr($string,3,4);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1027340]
Front-paged by Corion
[holli]: is there anybody out there?
[shmem]: the world is full of people - out there...
[holli]: are you sure? or are they mere prjections of my hallucinating mind?
shmem feels uneasy over his guess of the tune for silent night, perl 6 night
[holli]: https://www. v=6H_9QEc6vA8
[holli]: meh, silent night

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2017-09-22 20:51 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (269 votes). Check out past polls.