Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

How to access an Array Element

by barrycarlyon (Beadle)
on Apr 03, 2006 at 11:11 UTC ( [id://540894]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: How to access an Array Element
by wfsp (Abbot) on Apr 03, 2006 at 11:27 UTC
    We need to know what your array looks like. Try something like:

    use strict; use warnings; use Data::Dumper; my @array; # do whatever to populate the array print Dumper \@array;
    And show us a (short) sample of the output.

    Also, to help you on your way, have at look at these tutorials:

    • perlreftut - Mark's very short tutorial about references
    • perldsc - data structure complex data structure struct
    • perllol - Manipulating Arrays of Arrays in Perl
Re: How to access an Array Element
by GrandFather (Saint) on Apr 03, 2006 at 11:26 UTC

    I'm not sure that question makes much sense. What do you want to use for the hash key and what will the values be (where do they each come from). The array only supplies one - the key or the value.

    Perhaps you need to define your problem a little more? It may help to give a little sample data and show us the code that you have tried and are having problems with. It helps even more if you show us what your code prints and what you expected it to print.


    DWIM is Perl's answer to Gödel
Re: How to access an Array Element
by davido (Cardinal) on Apr 03, 2006 at 15:07 UTC

    Depending on whether you're dealing with an actual array, or an associative array (a hash), the answer could be either of the following:

    my $search = $data[2];

    ...or...

    my $search = $data{content};

    ...assuming your array or hash are named @data or %data.

    And this assumes also that I understood your ambiguously worded question. If I misunderstood it, please rephrase it in a followup so we can gain a better understanding of your need.


    Dave

Re: How to access an Array Element
by chargrill (Parson) on Apr 03, 2006 at 19:30 UTC

    barrycarlyon: you appear to have been working at this problem for some time now.

    Try posting some more of the code from your program, giving us a fully funcational version that operates on <DATA> or some other construct, with expected input, and output, as well as actual output (containing the problem you're running into) so that we can see exactly what you're doing, so perhaps we can help you better.

    If this node is related to your other node, then perhaps all you need to do is:

    $search = ( split / /, $data ); # or $search = @$data[2]; # or $search = $results[2];

    ... but really, it's hard to give an accurate answer to your queries without a better defined question! :-)

    The way this particular question is worded, it looks like you've got a hash whose value you'd like to get, which would be written as:

    $search = $array_name{'content'};

    Please, help us help you a bit better :)



    --chargrill
    $,=42;for(34,0,-3,9,-11,11,-17,7,-5){$*.=pack'c'=>$,+=$_}for(reverse s +plit//=>$* ){$%++?$ %%2?push@C,$_,$":push@c,$_,$":(push@C,$_,$")&&push@c,$"}$C[$# +C]=$/;($#C >$#c)?($ c=\@C)&&($ C=\@c):($ c=\@c)&&($C=\@C);$%=$|;for(@$c){print$_^ +$$C[$%++]}
Re: How to access an Array Element
by SamCG (Hermit) on Apr 03, 2006 at 16:32 UTC
    This is really an odd question, particularly given your stipulation that there is only one row of data. If this is really a stipulation, I'd be inclined to say $search='crikey';, but there has to be more to your question. An array would be something like @albums = qw(1 grovvy crikey 10/10/2006) (assuming the simplest case, and that you don't really mean a hash). Then @albums[2] would be 'crikey'. update: fixed typo

    ------------------
    s''limp';@p=split '!','n!h!p!';s,m,s,;$s=y;$c=slice @p[1];so brutally;d;$n=reverse;$c=$s**$#p;print(''.$c^chop($n))while($c/=$#p)>=1;
Re: How to access an Array Element
by murugu (Curate) on Apr 03, 2006 at 15:24 UTC
    Hi, The problem is not clear. Assuming @field is having the content of the whole row,
    my $search = $field[2];

    Regards,
    Murugesan Kandasamy
    use perl for(;;);

Re: How to access an Array Element
by SamCG (Hermit) on Apr 04, 2006 at 21:10 UTC
    Hmm, okay, you're using a database query method I'm not familiar with to get your array. If you were using something like:

    while (my @row = $sth->fetchrow_array){@search=($row[2]);}
    I'd understand a bit better. You specify in other post that your query only returns one row in this case, but what does your array look like if your query returns more than one row?

    So, you use my @currentpoll = LSRfm::Database::Data->search(field => 'currentpoll'); to get your array. Does this technique fetch rows sequentially? If so, put your fetch into a loop and then use $row[2] to access the content field, pushing it into an array with push @search, $row[2] if you need to compile a list for your next function. Of course, you'll also be safe doing this if you're sure you'll always get one row (which is what the OP looked like), but in that case I'm not sure why you need the single field in an array (though you can still do it).

    -----------------
    s''limp';@p=split '!','n!h!p!';s,m,s,;$s=y;$c=slice @p1;so brutally;d;$n=reverse;$c=$s**$#p;print(''.$c^chop($n))while($c/=$#p)>=1;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://540894]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-03-29 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found