<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to access hash values in &amp;quot;arrays of hases&amp;quot; ? in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837832#M97908</link>
    <description>&lt;!--!*#--&gt;As for your last question,&lt;BR /&gt;you may not have come accross array and hash slices.&lt;BR /&gt;These allow you to "slice" more than one list element in one go.&lt;BR /&gt;Remember in Perl (up until now, but they are going to change this in Perl6 because it seems to cause too much confusion among Perl beginners) that an "$" in a variable refers to always what you get, viz. a scalar,&lt;BR /&gt;while a "@" refers to a list.&lt;BR /&gt;So often in beginners' scripts you will see statements like&lt;BR /&gt; &lt;BR /&gt;$someval = @ary[$i];&lt;BR /&gt; &lt;BR /&gt;which in fact is calling for a slice.&lt;BR /&gt;But as you only index one element it doesn't make much sence.&lt;BR /&gt;Whereas if you are after, say the 3rd, 8th, and 12th to 14th element, you could reference them in one go&lt;BR /&gt; &lt;BR /&gt;($elem3, $elem8, $elem12, $elem13, $elem14) = @ary[3,8,12..14];&lt;BR /&gt; &lt;BR /&gt;or better&lt;BR /&gt; &lt;BR /&gt;@myslice = @ary[3,8,12..14];&lt;BR /&gt; &lt;BR /&gt;The same of course works for hash slices.&lt;BR /&gt;Say those keys exist, neglecting testing for their existence.&lt;BR /&gt; &lt;BR /&gt;@myslice = @loopdata{qw(interval type date)}&lt;BR /&gt;</description>
    <pubDate>Fri, 11 Aug 2006 02:40:19 GMT</pubDate>
    <dc:creator>Ralph Grothe</dc:creator>
    <dc:date>2006-08-11T02:40:19Z</dc:date>
    <item>
      <title>How to access hash values in "arrays of hases" ?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837818#M97894</link>
      <description>My code is as following. Just tell me will it work ?&lt;BR /&gt;&lt;BR /&gt; while(my $data = $sth-&amp;gt;fetchrow_hashref) {&lt;BR /&gt;        my %loopdata;&lt;BR /&gt;        ($Hrs,$Mins,$Secs) = &amp;amp;diffDateTime($data-&amp;gt;{'date'},$data-&amp;gt;{'time_in'},$times_data-&amp;gt;{'time_out'},$times_data-&amp;gt;{'date_out'});&lt;BR /&gt;        $loopdata{'type'}=$data-&amp;gt;{'type'};&lt;BR /&gt;        $loopdata{'interval'}= $Hrs:$Mins:$Secs;&lt;BR /&gt;        push(@intlist, \%loopdata);&lt;BR /&gt;    }&lt;BR /&gt;    my %hash;&lt;BR /&gt;    my $i=0;&lt;BR /&gt;    $hash{'total_lhrs'}='00:00:00';&lt;BR /&gt;    foreach my $interval (@intlist) {&lt;BR /&gt;        if($interval-&amp;gt;[++$i]) {&lt;BR /&gt;            my $t = $interval-&amp;gt;[$i];&lt;BR /&gt;            my $t2 = $interval-&amp;gt;[++$i];&lt;BR /&gt;            my ($hr, $min, $sec) = split /:/, $t;&lt;BR /&gt;            my ($hr2, $min2, $sec2) = split /:/, $t2;&lt;BR /&gt;            my ($hr1, $min1, $sec1) = split /:/, $hash{'total_lhrs'};&lt;BR /&gt;            my $hr3 = $hr + $hr2 + $hr1;&lt;BR /&gt;            my $min3 = $min + $min2 + $min1;&lt;BR /&gt;            my $sec3 = $sec + $sec2 + $sec1;&lt;BR /&gt;            my $elapsed = ($hr3 * 3600) + ($min3 * 60) + $sec3;&lt;BR /&gt;            my $hr4 = int $elapsed / 3600;&lt;BR /&gt;            my $new = $elapsed % 3600 ;&lt;BR /&gt;            my $min4 = int $new / 60;&lt;BR /&gt;            my $sec4 = $new % 60 ;&lt;BR /&gt;            $hash{'total_lhrs'}= sprintf ("%02d:%02d:%02d\n",$hr4,$min4,$sec4);&lt;BR /&gt;            $i++;&lt;BR /&gt;        }&lt;BR /&gt;    }&lt;BR /&gt;</description>
      <pubDate>Sat, 05 Aug 2006 01:28:48 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837818#M97894</guid>
      <dc:creator>Pankaj Yadav_1</dc:creator>
      <dc:date>2006-08-05T01:28:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to access hash values in "arrays of hases" ?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837819#M97895</link>
      <description>Hi:&lt;BR /&gt;&lt;BR /&gt;No, in fact it doesn't even compile.&lt;BR /&gt;&lt;BR /&gt;You should always use the 'strict' and 'warnings' pragma.  They will save you hours of grief and suffering!&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/perl&lt;BR /&gt;use strict;&lt;BR /&gt;use warnings;&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Sat, 05 Aug 2006 07:53:26 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837819#M97895</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-08-05T07:53:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to access hash values in "arrays of hases" ?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837820#M97896</link>
      <description>I have used a hash %loopdata. After that I have pushed the hash in an array.&lt;BR /&gt;&lt;BR /&gt;Can anyone tell me how can I access the values by interval-&amp;gt;[][] or in other words the values of the hash when I am inside "foreach" loop?</description>
      <pubDate>Mon, 07 Aug 2006 00:32:28 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837820#M97896</guid>
      <dc:creator>Pankaj Yadav_1</dc:creator>
      <dc:date>2006-08-07T00:32:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to access hash values in "arrays of hases" ?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837821#M97897</link>
      <description>&lt;BR /&gt;The basic syntax is: &lt;BR /&gt;&lt;BR /&gt;print $array[element]-&amp;gt;{key}&lt;BR /&gt;&lt;BR /&gt;There is probably a better way to do it, but this is one way. &lt;BR /&gt;&lt;BR /&gt;example: &lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/perl&lt;BR /&gt;&lt;BR /&gt;%flintstones = (&lt;BR /&gt;                dad =&amp;gt; fred,&lt;BR /&gt;                mom =&amp;gt;  wilma,&lt;BR /&gt;                daughter=&amp;gt; pebbles,&lt;BR /&gt;                dog =&amp;gt; dino,&lt;BR /&gt;               );&lt;BR /&gt;&lt;BR /&gt;push @people, \%flintstones;&lt;BR /&gt;&lt;BR /&gt;@flint_keys=keys %flintstones;&lt;BR /&gt;&lt;BR /&gt;for ($i=-1; $i &amp;lt; $#people; ++$i){ &lt;BR /&gt;   foreach $key (@flint_keys) {&lt;BR /&gt;   print "$people[$i]-&amp;gt;{$key}\n";&lt;BR /&gt;  }&lt;BR /&gt;}</description>
      <pubDate>Mon, 07 Aug 2006 08:02:45 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837821#M97897</guid>
      <dc:creator>Marvin Strong</dc:creator>
      <dc:date>2006-08-07T08:02:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to access hash values in "arrays of hases" ?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837822#M97898</link>
      <description>&lt;!--!*#--&gt;Just looking at the code there are a few things that immediately seem to fail.&lt;BR /&gt;As already mentioned it is strongly recommended to use the strict pragma, also warnings, and diagnostics may help.&lt;BR /&gt;First, we can't see what diffDateTime is expecting as arguments, and what it is returning in list context.&lt;BR /&gt;Let's assume that it is correctly assigning $Hrs, $Mins, Secs.&lt;BR /&gt;But we still don't know if the hashref $times_data is correctly defined.&lt;BR /&gt; &lt;BR /&gt;Then this assignment should be put in double quotes&lt;BR /&gt; &lt;BR /&gt;$loopdata{'interval'}= "$Hrs:$Mins:$Secs";&lt;BR /&gt; &lt;BR /&gt;You could also use the concat operator "." which would require quoting ':'&lt;BR /&gt;or a join(":", $Hrs, $Mins, $Secs).&lt;BR /&gt;&lt;BR /&gt;Then I am not sure what your @intlist array holds since you were pushing hashrefs of %loopdata on it.&lt;BR /&gt;It might have been already containing other elements, but this would be a very ugly data mix.&lt;BR /&gt;Better be explicit and undefine it before the the while loop over fetchrow_hashref&lt;BR /&gt;&lt;BR /&gt;@intlist = ();&lt;BR /&gt;&lt;BR /&gt;or &lt;BR /&gt;&lt;BR /&gt;$#intlist = -1; &lt;BR /&gt;&lt;BR /&gt;So if @intlist really only contains fetched elements from above then the&lt;BR /&gt;if statement should rather read&lt;BR /&gt; &lt;BR /&gt;if ($interval[++$i]) {&lt;BR /&gt; &lt;BR /&gt;Btw. are you really sure about the pre-increment of the array index $i?&lt;BR /&gt;Since this appears a second time,&lt;BR /&gt;and even a third time at the end of the loop&lt;BR /&gt;you may well run out of array bounds.&lt;BR /&gt;&lt;BR /&gt;In Perl, C-style for loops should be avoided&lt;BR /&gt;(the ones where you iterate over an index like&lt;BR /&gt; &lt;BR /&gt;for ($i=0; $i&amp;lt;=$#intlist; $i++)&lt;BR /&gt; &lt;BR /&gt;better loop the Perl way like you tried&lt;BR /&gt;&lt;BR /&gt;foreach my $interval (@intlist)&lt;BR /&gt; &lt;BR /&gt;but then don't mix both, like you did in the loop's body later on.&lt;BR /&gt; &lt;BR /&gt;&lt;BR /&gt;These were only a few conspicous strange bits in your code snippet.&lt;BR /&gt;</description>
      <pubDate>Mon, 07 Aug 2006 10:50:53 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837822#M97898</guid>
      <dc:creator>Ralph Grothe</dc:creator>
      <dc:date>2006-08-07T10:50:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to access hash values in "arrays of hases" ?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837823#M97899</link>
      <description>I have used the  if($interval-&amp;gt;[++$i]) {} statement for bounds checking but that won't work.&lt;BR /&gt;&lt;BR /&gt;Can anyone tell me how can I do bounds checking in the foreach loop ?</description>
      <pubDate>Tue, 08 Aug 2006 01:26:51 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837823#M97899</guid>
      <dc:creator>Pankaj Yadav_1</dc:creator>
      <dc:date>2006-08-08T01:26:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to access hash values in "arrays of hases" ?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837824#M97900</link>
      <description>In the foreach loop I am adding two values.&lt;BR /&gt;&lt;BR /&gt;Please tell me the answer of the above question with reference to my CODE.</description>
      <pubDate>Tue, 08 Aug 2006 02:24:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837824#M97900</guid>
      <dc:creator>Pankaj Yadav_1</dc:creator>
      <dc:date>2006-08-08T02:24:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to access hash values in "arrays of hases" ?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837825#M97901</link>
      <description>&lt;!--!*#--&gt;As for array bounds checking,&lt;BR /&gt;the matter in Perl is quite easy.&lt;BR /&gt;Referring to an array in scalar context yields its dimension, and referring to it by prepending $# to its name yields the index of its last element.&lt;BR /&gt;Besides, Perl lets you access the last array element (or any counting from the end) by negative indexing.&lt;BR /&gt; &lt;BR /&gt;e.g.&lt;BR /&gt; &lt;BR /&gt;my $dim = @intlist;&lt;BR /&gt;my $last_idx = $#intlist;&lt;BR /&gt;my $last_elem = $intlist[-1];&lt;BR /&gt;  &lt;BR /&gt;But if you simply foreach loop&lt;BR /&gt;(the same works for simply "for", but the more common idiom is foreach)&lt;BR /&gt;there is no need to guard your array indices.&lt;BR /&gt;This is also a strong pro, apart from better readability.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 08 Aug 2006 03:24:07 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837825#M97901</guid>
      <dc:creator>Ralph Grothe</dc:creator>
      <dc:date>2006-08-08T03:24:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to access hash values in "arrays of hases" ?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837826#M97902</link>
      <description>I move back to my first question.&lt;BR /&gt;&lt;BR /&gt;Marvin Strong had suggested a way to access the values in the hash.&lt;BR /&gt;But as you can see my hash is temporary so I can't use it as suggested by Marvin.&lt;BR /&gt;&lt;BR /&gt;So please look up my code and tell me how can access the {interval} values in @intlist inside the FOREACH loop.&lt;BR /&gt;</description>
      <pubDate>Wed, 09 Aug 2006 04:26:09 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837826#M97902</guid>
      <dc:creator>Pankaj Yadav_1</dc:creator>
      <dc:date>2006-08-09T04:26:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to access hash values in "arrays of hases" ?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837827#M97903</link>
      <description>&lt;!--!*#--&gt;foreach my $intlist_href (@intlist) {&lt;BR /&gt;    my $interval = $intlist_href-&amp;gt;{interval};&lt;BR /&gt;}&lt;BR /&gt;</description>
      <pubDate>Wed, 09 Aug 2006 08:57:29 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837827#M97903</guid>
      <dc:creator>Ralph Grothe</dc:creator>
      <dc:date>2006-08-09T08:57:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to access hash values in "arrays of hases" ?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837828#M97904</link>
      <description>Another issue you're going to have is with the line:&lt;BR /&gt;&lt;BR /&gt;my %loopdata;&lt;BR /&gt;&lt;BR /&gt;being inside of your while loop. I'm thinking that having it inside rather than outside is going to cause that variable to be defined each time you go through the loop. For instance, try these two small examples to see the difference:&lt;BR /&gt;&lt;BR /&gt;my $j = 0;&lt;BR /&gt;my $k;  # $k defined on the outside&lt;BR /&gt;while ($k &amp;lt; 10) {&lt;BR /&gt;  $j++;&lt;BR /&gt;  $k = $j;&lt;BR /&gt;  print "k = $k\n";  # prints 1 - 10&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;--- vs. ---&lt;BR /&gt;&lt;BR /&gt;my $j = 0;&lt;BR /&gt;while ($k &amp;lt; 10) {&lt;BR /&gt;  my $k; # $k defined on the inside&lt;BR /&gt;  $j++;&lt;BR /&gt;  $k = $j;&lt;BR /&gt;  print "k = $k\n"; # prints 1 - infinity&lt;BR /&gt;}</description>
      <pubDate>Wed, 09 Aug 2006 15:12:31 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837828#M97904</guid>
      <dc:creator>Greg Gerke</dc:creator>
      <dc:date>2006-08-09T15:12:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to access hash values in "arrays of hases" ?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837829#M97905</link>
      <description>I want to know that how can I access two consecutive values of the array when I am inside the FOREACH loop.</description>
      <pubDate>Thu, 10 Aug 2006 00:17:55 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837829#M97905</guid>
      <dc:creator>Pankaj Yadav_1</dc:creator>
      <dc:date>2006-08-10T00:17:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to access hash values in "arrays of hases" ?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837830#M97906</link>
      <description>do you mean something that won't change the value of your $i along the way...?&lt;BR /&gt;&lt;BR /&gt;my @array=(80,70,60,50,40,30,20,10);&lt;BR /&gt;my $i = 3;&lt;BR /&gt;&lt;BR /&gt;print "i before = $i\n";&lt;BR /&gt;print "array elements $i = $array[$i]\n";&lt;BR /&gt;print "array elements " . ($i + 1) . " = " . $array[$i + 1] . "\n";&lt;BR /&gt;print "i after  = $i\n";</description>
      <pubDate>Thu, 10 Aug 2006 15:40:10 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837830#M97906</guid>
      <dc:creator>Greg Gerke</dc:creator>
      <dc:date>2006-08-10T15:40:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to access hash values in "arrays of hases" ?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837831#M97907</link>
      <description>Thanks a lot to all of you.&lt;BR /&gt;I got what I wanted.&lt;BR /&gt;&lt;BR /&gt;I have given you the points.</description>
      <pubDate>Thu, 10 Aug 2006 23:37:20 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837831#M97907</guid>
      <dc:creator>Pankaj Yadav_1</dc:creator>
      <dc:date>2006-08-10T23:37:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to access hash values in "arrays of hases" ?</title>
      <link>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837832#M97908</link>
      <description>&lt;!--!*#--&gt;As for your last question,&lt;BR /&gt;you may not have come accross array and hash slices.&lt;BR /&gt;These allow you to "slice" more than one list element in one go.&lt;BR /&gt;Remember in Perl (up until now, but they are going to change this in Perl6 because it seems to cause too much confusion among Perl beginners) that an "$" in a variable refers to always what you get, viz. a scalar,&lt;BR /&gt;while a "@" refers to a list.&lt;BR /&gt;So often in beginners' scripts you will see statements like&lt;BR /&gt; &lt;BR /&gt;$someval = @ary[$i];&lt;BR /&gt; &lt;BR /&gt;which in fact is calling for a slice.&lt;BR /&gt;But as you only index one element it doesn't make much sence.&lt;BR /&gt;Whereas if you are after, say the 3rd, 8th, and 12th to 14th element, you could reference them in one go&lt;BR /&gt; &lt;BR /&gt;($elem3, $elem8, $elem12, $elem13, $elem14) = @ary[3,8,12..14];&lt;BR /&gt; &lt;BR /&gt;or better&lt;BR /&gt; &lt;BR /&gt;@myslice = @ary[3,8,12..14];&lt;BR /&gt; &lt;BR /&gt;The same of course works for hash slices.&lt;BR /&gt;Say those keys exist, neglecting testing for their existence.&lt;BR /&gt; &lt;BR /&gt;@myslice = @loopdata{qw(interval type date)}&lt;BR /&gt;</description>
      <pubDate>Fri, 11 Aug 2006 02:40:19 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/how-to-access-hash-values-in-quot-arrays-of-hases-quot/m-p/3837832#M97908</guid>
      <dc:creator>Ralph Grothe</dc:creator>
      <dc:date>2006-08-11T02:40:19Z</dc:date>
    </item>
  </channel>
</rss>

