in reply to Re: A question on array references.
in thread A question on array references.

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.

Replies are listed 'Best First'.
Re^3: A question on array references.
by BrowserUk (Patriarch) on Dec 16, 2011 at 16:52 UTC
    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?

Re^3: A question on array references.
by johngg (Canon) on Dec 16, 2011 at 16:55 UTC

    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

Re^3: A question on array references.
by Eliya (Vicar) on Dec 16, 2011 at 16:49 UTC

    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.