f



Getting Values

I have an array $family  there are 30 keys and associated values.

I want to return the values from the last 8 items ( keys 'child1 - child8') 
and I am returning the age associated with each without referencing the keys 
just the element number 22 .. 30

print $family{'child1'} ==> 28 ** age
print $family[22] ==> child1=29

I have attempted to get this info from the perldoc but without success. 
Might not be phrasing the question properly.  I have not seen anything in 
the books I have.  Help appreciated.

Frank 


0
FJRussonc (5)
2/9/2006 5:19:54 PM
comp.lang.perl.misc 33233 articles. 2 followers. brian (1246) is leader. Post Follow

5 Replies
540 Views

Similar Articles

[PageSpeed] 22

"Frank J. Russo" <FJRussonc@earthlink.net> wrote:

> I have an array $family

$family is a scalar

>  there are 30 keys and associated values.

ok (associative array, or hash for short)

> I want to return the values from the last 8 items ( keys 'child1 -
> child8')

define last 8 items? How do you want your keys to be ordered? 

> and I am returning the age associated with each without
> referencing the keys just the element number 22 .. 30
> 
> print $family{'child1'} ==> 28 ** age
> print $family[22] ==> child1=29

Try to describe what you want in words. Don't think in datastructures 
and program code, especially when you don't master the programming 
language.

What is 22 in the above context? Why is 22 associated with child1 ? If 
you want to have each child (1..8) to have an age, you set up a hash:

my $child2age = (

    	child1 => 28,
    	child2 => 17,
    	etc...
);

if you want also each child to have a number, you can create a second 
look up table (hash):

my $number2child = (

    	22 => 'child1',
    	etc.
);

and then your second line, if I understand it right, could be written 
as:

print $child2age{ $number2child{ 22 } };

It depends a bit on your exact requiremends, which you should give.

> I have attempted to get this info from the perldoc but without
> success. Might not be phrasing the question properly.  I have not seen
> anything in the books I have.  Help appreciated.

I guess you have been browsing your books like crazy. I always recommend 
to read a book front to cover before sitting behind the computer.

-- 
John                   Small Perl scripts: http://johnbokma.com/perl/
               Perl programmer available:     http://castleamber.com/
                                        I ploink googlegroups.com :-)
                        
0
John
2/9/2006 5:28:19 PM
Frank J. Russo wrote:
> I have an array $family

$family is a scalar, not an array.

>  there are 30 keys and associated values.

An array does not have keys and values.  A hash (also known as an
"associative array" does).

> I want to return the values from the last 8 items ( keys 'child1 - child8')

"last" is a meaningless word when it comes to hashes.  Hashes are
unordered.  What do you mean by "last"?

> and I am returning the age associated with each without referencing the keys
> just the element number 22 .. 30

Hashes do not have element numbers.  They have keys only.  Perhaps you
are thinking of the wrong language?

> print $family{'child1'} ==> 28 ** age
> print $family[22] ==> child1=29

Did you mean that 'child1' is the 22nd element you inserted into
%family?  That's all well and good, but the hash does not store that
information in any way at all.

> I have attempted to get this info from the perldoc but without success.
> Might not be phrasing the question properly.  I have not seen anything in
> the books I have.  Help appreciated.

You might want to look at the Tie::IxHash module on CPAN, which keeps
your hashes sorted by Insertion order.

Paul Lalli

0
Paul
2/9/2006 5:53:00 PM
I am receiving from a web site form 30 pieces of information.( 30 keys / 30 
values )

I know that the child1 - child8 keys are found in the 22 - 30 elements.  I 
want a way to directly refer to the values for these elements without 
refering to the keys.

I was able to use a split and return the value.  Adds an additional 2 steps 
but it works.  I just wanted to find a more direct way to get the values.

($key, $val) = split(/=/,$family[$i],2); # splits on the first =.

Frank 


0
Frank
2/9/2006 7:33:49 PM
"Frank J. Russo" <FJRussonc@earthlink.net> wrote in
news:xOMGf.16393$vU2.10974@newsread3.news.atl.earthlink.net: 

[ Please quote some context when you reply. Have you read the 
  posting guidelines yet? ]

> I am receiving from a web site form 30 pieces of information.( 30 keys
> / 30 values )
> 
> I know that the child1 - child8 keys are found in the 22 - 30
> elements.  I want a way to directly refer to the values for these
> elements without refering to the keys.
> 
> I was able to use a split and return the value.  Adds an additional 2
> steps but it works.  I just wanted to find a more direct way to get
> the values. 
> 
> ($key, $val) = split(/=/,$family[$i],2); # splits on the first =.

Your last two posts are possibly some of the most cryptic I have seen. 
It is very hard to discern what you are talking about.

Are you talking about a CGI script? If so, are you using CGI.pm? If not, 
you should.

What is in @family? 

If I am guessing correctly, you have multiple input fields that are 
related, and named child1-child8. Just as it is a bad idea in Perl to 
use variables $child1-$child8 instead of an array @children with 8 
elements, it is a bad idea to have eight fields child1-child8 in your 
form.

Instead, you should use:

<p>
Child1: <input type="text" name="children"><br>
Child2: <input type="text" name="children"><br>
Child3: <input type="text" name="children"><br>
Child4: <input type="text" name="children"><br>
Child5: <input type="text" name="children"><br>
Child6: <input type="text" name="children"><br>
Child7: <input type="text" name="children"><br>
Child8: <input type="text" name="children"><br>
</p>

in your form. Then, you can get all the submitted fields by simply 
using:

my @children = $cgi->param('children');

This way, your CGI script would not have to be changed every time the 
form is changed.

Sinan

PS: Have you read the posting guidelines yet?
-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

0
A
2/9/2006 8:09:07 PM
Frank J. Russo wrote:
> I am receiving from a web site form 30 pieces of information.( 30 keys =
/ 30=20
> values )
>=20
> I know that the child1 - child8 keys are found in the 22 - 30 elements.=
  I=20
> want a way to directly refer to the values for these elements without=20
> refering to the keys.
>=20
> I was able to use a split and return the value.  Adds an additional 2 s=
teps=20
> but it works.  I just wanted to find a more direct way to get the value=
s.
>=20
> ($key, $val) =3D split(/=3D/,$family[$i],2); # splits on the first =3D.=

>=20
> Frank=20
>=20
>=20

Athough I agree with Sinan, I have a slight notion about what you want=20
and I guess references is the key (pun intended).
You will have a hash with e.g. 'Betty' as a key and the value is a=20
reference to an (anonymous) hash containing that Betty's data (age,=20
=2E..). Then you have an array and member number 22 refers to the same=20
anonymous hash as the 'Betty' hash entry:

my %family =3D (
     Betty =3D> { age =3D> 28 }
);
my @family;
$family[22] =3D $family{'Betty'};	# Child#22 is Betty

print "child Betty: ", $family{'Betty'}->{age}, "\n";
print "child#22: ", $family[22]->{age}, "\n";
$family{'Betty'}->{age}++;	# Betty's birthday!
print "child Betty: ", $family{'Betty'}->{age}, "\n";
print "child#22: ", $family[22]->{age}, "\n";


--=20
Josef M=F6llers (Pinguinpfleger bei FSC)
	If failure had no penalty success would not be a prize
						-- T.  Pratchett

0
Josef
2/10/2006 8:21:36 AM
Reply:

Similar Artilces:

deisng perl perl programmibng langs in perl
then liberate big war zones with notes on their download pages. -r www.thevoid1.net/ ...

get values from the textbox in perl TK
Hai I want to get the values from the text box when u typed it in the run time. how to get the values and how to put the same in the another same text. regards, Jayashree maami wrote: > Hai > I want to get the values from the text box when u typed it in the > run time. > how to get the values and how to put the same in the another same text. The get() and insert() methods are described in the Tk::Text pods. --Ala On Fri, 14 May 2004 04:53:45 -0400, maami wrote: > I want to get the values from the text box when u typed it in the > run time. > how ...

How to get cell value excel in perl
Hi good people: I need to get value of the cell so I can split by : and do some calculation,the format of numbers is like 12:03:20. Is there any way that I can get the value of the cell in perl...thanks in advance.. my $workbook = Spreadsheet::WriteExcel->new($out); my $worksheet = $workbook->add_worksheet("Result"); my $t1 = xl_rowcol_to_cell(2, 4); # E2 my $t2 = xl_rowcol_to_cell(2, 5); # F2 my ($t1_h, $t1_min, $t1_sec) = split (/\:/, $t1); my ($t2_h, $t2_min, $t2_sec) = split (/\:/, $t1); my $delta= $worksheet->write('G2', '=$t2_h - t1_h...

Perl XPath : How to get the value of the attribute?
Hi, I have the following XML : <checkIn bug="1111111"> <regression> </regression> </checkIn> How do i get the value "1111111" for bug ? I am able to get everything under <checkIn> using my $bug = $xPath->find("/checkIn[\@bug]"); TIA -- Abhinav Abhinav wrote: > I have the following XML : > > <checkIn bug="1111111"> > <regression> > </regression> > </checkIn> > > How do i get the value "1111111" for bug ? > > I am able to get everything...

get perl's return value
Hi, I am not sure if I should post this question in newsgroup for Java or Perl. If I want to call a Perl script in Java, but I want to get the return value from that Perl script, how can I do that? Right now what I am doing is just using Runtime.getRuntime.exec("perl script.pl"), but oviously I can't get the return value from that perl script. Thanks in advance. Wilson Chew On Tue, 27 Jul 2004 16:32:14 -0700, "Wilson Chew" <wchew@qualcomm.com> wrote: >Hi, > >I am not sure if I should post this question in newsgroup for Java or Perl. > >If I ...

Getting values in between the values of a vector?
Say I have a vector that's like this: [-6 -3 0 3 6] How can I get it get the number in between each of the two values to return something like: [-4.5 -1.5 1.5 4.5] Is there a built-in function that can do this? I'm asking because I'd like to avoid a loop if possible (but I'm thinking it's the only way). Thanks! "Brandon Rodriguez" <coasters2k@yahoo.com> wrote in message <gsfp69$19b$1@fred.mathworks.com>... > Say I have a vector that's like this: > > [-6 -3 0 3 6] > > How can I get it get the number in between each of the two v...

Get shell command return value in Perl program.
Hi all, I have a Perl script which includes a shell command, I want to get the return result from this shell command, but when I check the $? value, it is not correct, can anyone tell me how I can get the return value? my perl code: opendir(DIR, "/log") || die ("Can not open log directory"); foreach $direntry (readdir(DIR)) { if ($direntry =~ /2007\d{4}/) { $ja_request = system("grep -i 'INDEX' $BASE/$direntry/ FILE.log | wc -l"); } } Basically, I want to search the "INDEX" string, and count the number of occ...

perl script for uploading photo and getting the return value of submit
i just can't make it .. ive made som perl script creating a form for uploading images but my concern is to retrieve the return value of the submit button.... can you give me a sample perl script that creates a form that uploads photo. and return the value of the submit button.. tnx!! ...

why does it not get the value in perl run shell script to call include php file?
two file in the same folder fun.php 775 1.pl #! /usr/bin/perl $i = 0; $decryptCC = `echo '<?php include( "/home/webserve.ca/eselect/ eselect_test/fun.php" ); echo "CC" . dcc( "$i", "" ) . "CC"; ?>' | php -q`; print $decryptCC; 2. fun.php <? function dcc($char) { return "test"; } ?> 3. 1.sh #!/usr/bin/php -q <?php include('fun.php'); echo dcc(1); ?> run ./1.sh get test. 2. run ....

getting offset of the value instead of the value
Hello all, I am very new to assembly programming. I'm trying to make a simple program that has a "graph" feature in it. There is an array declared in the DATA section, with length 26. It will contain the values for drawing the graph. The graph is a 26 green column, the height of each column will be loaded from the array. The problem is: When I load the value from the array, I don't get the value, instead, I get the offset of the value. At least that is what I think, I might be mistaken, and as I said, I'm new to assembly, and very poor in debugging. (BTW, what debugger ...

getting upper and lower values from a reference value
Hi, data m; input volume ; cards; 0.1234 0.2345 0.3435 0.4567 0.498 0.5345 0.6768 ; run; i need to get upper and lower values(considering 0.5 as my reference point) which are nearst to .5 value from the above example data the final result i have to get is 0.498(as upper value) and 0.5345(as lower value) because 0.5(imaginary reference value) lies in between these two values. Thanks in advance. On Nov 1, 10:59=A0pm, mahi <mehetrey.mahen...@gmail.com> wrote: > Hi, > > data m; > input volume ; > cards; > 0.1234 > 0.2345 > 0.3435 > 0.4567 > 0.498 > 0.53...

How to get inimum value from 16 values available
Dear all, I've a specific problem. Hope some one will answer me. I have a color image, say img1, of 512 X 512 size in CIELAB space. For each pixel in img1, I've 16 sample pixels approximated as follows (E1 is another image obtained by some process) Lp1(x,y) = img1(x,y,1); ap1(x,y) = img1(x,y,2) + E1(x,y); bp1(x,y) = img1(x,y,3); Lp2(x,y) = img1(x,y,1); ap2(x,y) = img1(x,y,2) + E1(x,y)/sqrt(2); bp2(x,y) = img1(x,y,3) + E1(x,y)/sqrt(2); Lp3(x,y) = img1(x,y,1); ap3(x,y) = img1(x,y,2); ...

get a value in the servlet with get, but null with post
Hi, I am getting a value for a hidden field in my servlet when the method of the form is GET. But when I change it to POST, I get a null value for the same field, all things remaining the same. Does anybody have any idea why this could be happening? Thanks, Shiv "Shiv" <ss_panda@hotmail.com> wrote in message > Does anybody have any idea why this could be happening? Not without seeing some code... -- Wendy in Chandler, AZ try to find article named "High-Level Network Programming ", here is one part: ".... Consider the following HTML form: ...

Getting value from asp:label into javascript input value
Hello, I really hope that someone can help me resolve my problem, I've been working on it for the past few days, to no avail. I have an asp:label which gets its value from the database, but what I'm trying to do is to get that value (from the asp:label) and place it inside the javascript input value. I can post sample code if required. TIA D "Dwizz" <Dwizz20@yahoo.co.uk> wrote in message news:1123236443.969946.4850@g47g2000cwa.googlegroups.com... > Hello, > I really hope that someone can help me resolve my problem, I've been > working on it for the p...

Must GET/POST Parameters Have Values? (And What is the Syntax for GET, If NOT)
Are GET and POST parameters required to have values, i.e. are either of these legal? http://www.mydomain.com/index.php?this&that http://www.mydomain.com/index.php?this=&that= In my application, I have a GET/POST parameter that is significant just by its presence (it doesn't need a value), and rather than say this=1&that=1 I was wondering if it is legal to specify them without values. Thanks, Dave. David T. Ashley wrote: > Are GET and POST parameters required to have values, i.e. are either of > these legal? > > http://www.mydomain.com/index.php?this&am...

Web resources about - Getting Values - comp.lang.perl.misc

National Review Sheds Subscribers Over ‘Don’t Vote Donald Trump’ Editorial
Chants of “Don’t vote Donald Trump” have resounded from both sides of political aisle since his entry into the race, and now National Review, ...

Stranded Motorist Kills Man Trying to Help Him, Sheriff Says
New York Times Stranded Motorist Kills Man Trying to Help Him, Sheriff Says New York Times The authorities in North Carolina accused a motorist ...

Police charge 17-year-old in Canada after 4 shot dead
Police on Saturday charged a 17-year-old boy with four counts of first-degree murder and seven counts of attempted murder in a mass shooting ...

Duquesne Basketball Team, Stuck on Turnpike, Plays Cards and Makes a Snow Angel
ABC News Duquesne Basketball Team, Stuck on Turnpike, Plays Cards and Makes a Snow Angel New York Times When the team bus lurched to a halt ...

Blue Origin Executes First Successful Relaunch And Landing Of Reusable Rocket
The private sector space race is heating up, and Amazon.com co-founder Jeff Bezos’ Blue Origin is back out in front with the first successful ...

Kerry confident Syria talks can proceed
Reuters Kerry confident Syria talks can proceed Reuters RIYADH U.S. Secretary of State John Kerry said on Saturday he was confident Syria ...

Jurors will start deliberation Thursday in the sex crimes trial for a fired Oklahoma City police officer ...
The jury deliberated into the night Wednesday for the third time in the sex crimes trial of fired Oklahoma City police officer Daniel Ken Holtzclaw. ...

John Legend & ‘Underground’ EP On “Slow Moving Change” Of Oscars & Diversity – Sundance
"I think what the Academy did this week is a step in the right direction but it's going to be a slow moving change because the Academy reflects ...

Stunning Star Wars visual effects breakdown shows how Maz Kanata was brought to life
Much was made of The Force Awakens' return to practical effects after the overly-sanitized look of the prequel trilogy, but make no mistake: ...

Kerry to press China over North Korea, urge ASEAN unity over South China Sea
By David Brunnstrom RIYADH (Reuters) - U.S. Secretary of State John Kerry begins a visit to East Asia on Sunday in which he plans to press China ...

Resources last updated: 1/24/2016 4:28:11 AM