29243

perl: using push() on an array inside a hash

Question:

Is it possible to use Perl's push() function on an array inside a hash?

Below is what I believe to be the offending part of a larger program that I am working on.

my %domains = (); open (TABLE, "placeholder.foo") || die "cannot read domtblout file\n"; while ($line = <TABLE>) { if (!($line =~ /^#/)) { @split_line = split(/\t/, $line); # splits on tabs - some entries contain whitespace if ($split_line[13] >= $domain_cutoff) { push($domains{$split_line[0]}[0], $split_line[19]); # adds "env from" coordinate to array push($domains{$split_line[0]}[1], $split_line[20]); # adds "env to" coordinate to array # %domains is a hash, but $domains{identifier}[0] and $domains{$identifier}[1] are both arrays # this way, all domains from one sequence are stored with the same hash key, but can easily be processed iteratively } } }

Later I try to interact with these arrays using

for ($i = 0, $i <= $domains{$identifier}[0], $i++) { $from = $domains{$identifier}[0][$i]; $to = $domains{$identifier}[1][$i]; $length = ($to - $from); $tmp_seq =~ /.{$from}(.{$length})/; print("$header"."$1"); }

but it appears as if the arrays I created are empty.

If $domains{$identifier}[0] is an array, then why can I not use the push statement to add an element to it?

Answer1:

$domains{identifier}[0] is not an array. <br />$domains{identifier}[0] is an array element, a scalar. <br />$domains{identifier}[0] is a reference to an array.

If it's

@array

when you have an array, it's

@{ ... }

when you have a reference to an array, so

push(@{ $domains{ $split_line[0] }[0] }, $split_line[19]);

References:

<ul><li><a href="http://www.perlmonks.org/?node_id=977408" rel="nofollow">Mini-Tutorial: Dereferencing Syntax</a></li> <li><a href="http://www.perlmonks.org/?node_id=69927" rel="nofollow">References quick reference</a></li> <li><a href="http://perldoc.perl.org/perlref.html" rel="nofollow">perlref</a></li> <li><a href="http://perldoc.perl.org/perlreftut.html" rel="nofollow">perlreftut</a></li> <li><a href="http://perldoc.perl.org/perldsc.html" rel="nofollow">perldsc</a></li> <li><a href="http://perldoc.perl.org/perllol.html" rel="nofollow">perllol</a></li> </ul>

Recommend

  • write and Replace particular line in file
  • Phonegap iOS: Download image to Photo Library
  • Printing in IE8 Has @href contents inline
  • Kendo UI Timepicker custom interval format
  • Grails Resources Plugin — How to get generated urls?
  • SharePoint timer job add list items, but does not trigger the associated workflow
  • UIScrollView - setting proper contentOffset for new contentSize yields undesirable empty space
  • NSCollectionView does not scroll items past initial visible rect
  • Basic Augmenting Types in Javascript
  • Assembly code support in source insight
  • acts_as_taggable_on Tags added twice
  • Delphi TCollection - Possible to prevent changing items in run-time?
  • Using Python objects in C++
  • Can a variable be stored within an image or div tag?
  • Is it safe to accept URL parameters for populating the `url_for` method?
  • how can I compare dates in array to find the earliest one?
  • Divide a $1 by 3 and adjusting 1 cent
  • How do I shift the decimal place in Python?
  • Ember.js model to be organised as a tree structure
  • How does document.ready work with angular element directives?
  • Paperclip, set path outside of rails root folder
  • Using $this when not in object context
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Perl system calls when running as another user using sudo
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • SQL merge duplicate rows and join values that are different
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • coudnt use logback because of log4j
  • LevelDB C iterator
  • Can't mass-assign protected attributes when import data from csv file
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal