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

Hello Monks, I am trying the following example:
#!/usr/bin/perl use warnings; use strict; my @refarray1 = (qw[this is the first line], qw[Second one is here], qw[squint your eyes to see the third], qw[fourth is surely near],); print "$refarray1[0]\n"; my $refarray2 = [qw[this is the first line], qw[Second one is here], qw[squint your eyes to see the third], qw[fourth is surely near],]; print "$refarray2->[0]\n";
Both produce the same result, the output is it prints out the word "this". What do I do if I want to print out the entire contents of one of the elements? I dont mean the whole four lines, but say, I just want to print out one line, how do I do that in case I have created an array of references or an array reference to a reference of arrays (whee, I'mgetting wordy here, and confused too)
Perl Version - (v5.14.2) built for MSWin32-x86-multi-thread on Windows 7 64 Bit. (Purposely using 32 Bit Perl)

Replies are listed 'Best First'.
Re: A question on array references.
by BrowserUk (Patriarch) on Dec 16, 2011 at 16:28 UTC

    This qw[this is the first line], produces a list of 4 words (hence qw ::= quote words), not a single line.

    Try switching all your qw[]s to just q[]s, and see what changes.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      Hi BrowserUK,

      Yes, that did it !!

      But thing is, I want an option in references where I can print out either one line or an element of the line using the -> notation.

      In other words if I say $refarray1[0] Then it should print the line "this is the first line", but if I want to print just "this" part, then it should be done using $refarray1[0]->[0], but thats not happening, because I think I am not understanding refences correctly and want something the wrong way.

      I tried the reference tutorial on perldocs, but not able to understand some concepts in it. Any pointers to some documentation or website that will shed more light on this will be helpful.

      Perl Version - (v5.14.2) MSWin32-x64-multi-thread on Windows 7 64 Bit.
        I want an option in references where I can print out either one line or an element of the line using the -> notation.

        Perl doesn't have the facility to index into strings by word. Or by char.

        You will either have to store each line as a string, and then split it when you want to access individual words:

        @AofLines = ( 'this is line one', 'this is line two' );; pp \@AofLines;; [ "this is line one", "this is line two" ] print $AofLines[ 0 ];; this is line one print +( split ' ', $AofLines[ 1 ] )[ 3 ];; two

        Or you store each line as an array of words, and the you will need to join them together to get the line back:

        @AofAofWords = ( [ qw[this is line one] ], [ qw[this is line two] ] ); +; pp \@AofAofWords;; [ ["this", "is", "line", "one"], ["this", "is", "line", "two"] ] print $AofAofWords[ 0 ][ 3 ];; one print join ' ', @{ $AofAofWords[ 1 ] };; this is line two

        Which is better for your application will depend upon whether you need to access the words or lines most frequently.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

        That's because each element is a string now but if you also want to index individual words then make the data structure a reference to an array of arrays by using square brackets around each qw{ ... }.

        knoppix@Microknoppix:~$ perl -Mstrict -wE ' > my $refArr2 = [ > [ qw{ this is the first line } ], > [ qw{ Second one is here } ], > [ qw{ squint your eyes to see the third } ], > [ qw{ fourth is surely near } ], > ]; > > say $refArr2->[ 1 ]->[ 0 ]; > say qq{@{ $refArr2->[ 1 ] }};' Second Second one is here knoppix@Microknoppix:~$

        I hope this is helpful.

        Cheers,

        JohnGG

        You can't have both a string and an array reference in the same scalar (array element).

        You can either store the entire string, and split it when you need an individual word, or store the words individually in an array, and join them when you want the entire string.

Re: A question on array references.
by choroba (Cardinal) on Dec 16, 2011 at 16:32 UTC
    Lists are flattened in Perl. Therefore,
    qw[this is the first line], qw[second one is here]
    is exactly the same as
    qw[this is the first line second one is here]
    How can Perl get your first line?

    You probably want this:

    my @refarray = ([qw/this is the first line/], [qw/second one is here/], ); print "@{$refarray[0]}\n";

    Updated.

Re: A question on array references.
by TJPride (Pilgrim) on Dec 16, 2011 at 16:53 UTC
    use strict; use warnings; ### Array of lines my @arr1 = (q|this is the first line|, q|this is the second line|); print "$arr1[0]\n"; # this is the first line ### Array of all words my @arr2 = (qw|this is the first line|, qw|this is the second line|); print "$arr2[0]\n"; # this print "@arr2\n"; # this is the first line this is the second line ### Nested array of words in each line my @arr3 = ([qw|this is the first line|], [qw|this is the second line|]); print "$arr3[0][0]\n"; # this print "@{$arr3[0]}\n"; # this is the first line ### Reference to array of lines my $arr1 = [q|this is the first line|, q|this is the second line|]; print "$arr1->[0]\n"; # this is the first line ### Reference to array of all words my $arr2 = [qw|this is the first line|, qw|this is the second line|]; print "$arr2->[0]\n"; # this print "@$arr2\n"; # this is the first line this is the second line ### Reference to nested array of words in each line my $arr3 = [[qw|this is the first line|], [qw|this is the second line|]]; print "$arr3->[0][0]\n"; # this print "@{$arr3->[0]}\n"; # this is the first line
Re: A question on array references.
by TomDLux (Vicar) on Dec 16, 2011 at 20:10 UTC

    What does Data::Dumper tell you about the structure of the refarrays? What about if you use the debugger and e'x'amine the variables?

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Re: A question on array references.
by perl514 (Pilgrim) on Dec 16, 2011 at 17:44 UTC

    Respected Monks,

    Thank you. Thank you for your replies.

    You are all truely helpful.

    No wonder Perl rules.

    Perl Version - (v5.14.2) MSWin32-x64-multi-thread on Windows 7 64 Bit.