Questions tagged [perl-data-structures]

Discussion of Perl's three built-in data types: scalars, arrays of scalars, and associative arrays of scalars, known as "hashes". At your command-line: perldoc perldata

perldata - Perl data types

perldsc - Perl Data Structures Cookbook

388 questions
134
votes
2 answers

How do I enter a multi-line comment in Perl?

Possible Duplicate: What are the common workarounds for multi-line comments in Perl? How do I add a multi-line comment to Perl source code?
vrbilgi
  • 5,703
  • 12
  • 44
  • 60
50
votes
6 answers

Perl array vs list

I have two data structures in Perl: An array: my @array2 = ( "1", "2", "3"); for $elem (@array2) { print $elem."\n"; } Giving me the following output: 1 2 3 And a list: my @array = [ "1", "2", "3"]; …
Marcin Cylke
  • 2,100
  • 2
  • 21
  • 40
35
votes
2 answers

How does double arrow (=>) operator work in Perl?

I know about the hash use of the => operator, like this $ cat array.pl %ages = ('Martin' => 28, 'Sharon' => 35, 'Rikke' => 29,); print "Rikke is $ages{Rikke} years old\n"; $ perl array.pl Rikke is 29 years old $ and I thought it…
Lazer
  • 90,700
  • 113
  • 281
  • 364
24
votes
5 answers

How can I store multiple values in a Perl hash table?

Up until recently, I've been storing multiple values into different hashes with the same keys as follows: %boss = ( "Allan" => "George", "Bob" => "George", "George" => "lisa" ); %status = ( "Allan" => "Contractor", "Bob" …
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
19
votes
8 answers

How to iterate through Hash (of Hashes) in Perl?

I have Hash where values of keys are other Hashes. Example: {'key' => {'key2' => {'key3' => 'value'}}} How can I iterate through this structure?
Jay Gridley
  • 713
  • 2
  • 12
  • 33
19
votes
4 answers

What are anonymous hashes in perl?

$hash = { 'Man' => 'Bill', 'Woman' => 'Mary, 'Dog' => 'Ben' }; What exactly do Perl's “anonymous hashes” do?
user1363308
  • 938
  • 4
  • 14
  • 33
15
votes
6 answers

How do I retrieve an integer's ordinal suffix in Perl (like st, nd, rd, th)

I have number and need to add the suffix: 'st', 'nd', 'rd', 'th'. So for example: if the number is 42 the suffix is 'nd' , 521 is 'st' and 113 is 'th' and so on. I need to do this in perl. Any pointers.
bozo user
  • 241
  • 1
  • 6
  • 15
14
votes
6 answers

How to get hashes out of arrays in Perl?

I want to write a little "DBQuery" function in perl so I can have one-liners which send an SQL statement and receive back and an array of hashes, i.e. a recordset. However, I'm running into an issue with Perl syntax (and probably some odd…
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
13
votes
1 answer

How do I create a 2D array in Perl?

I am trying to create a 2d array in Perl my code: my @wordsList=(); my @words=(); for ($id=0; $id<=@language.length; $id++) { my $eng = $db->selectall_arrayref("select word from words …
JoT
  • 301
  • 1
  • 3
  • 10
11
votes
2 answers

In Perl, why does concatenating a hash with a string give a fraction-looking result?

I have the following hash: my %villains = { "Boba" => "Fett", "Darth" => "Vader", "Moff" => "Tarkin", } I then print it like so: print "".%villains; I get the following output: 1/8 What semantics in Perl make this happen? Thank you!
Dmitry Blotsky
  • 323
  • 1
  • 7
11
votes
3 answers

sort an array according to elements of a second array

Suppose I have two arrays that look like this: ('1', '6', '8', '4', '5') ('a', 'c', 'd', 'f', 'w') I want to sort the first array, and the order of elements in the second array should change in the same way as the first array, so the order of the…
Abdel
  • 5,826
  • 12
  • 56
  • 77
10
votes
7 answers

Is there a way to replace an if-elsif-else in Perl with something better?

I want to build a bunch of Perl subrotines that all have the same template if elsif elsif else that takes a decision based on a factor variable. Here's an example of subroutine template: sub get_age{ my $factor=shift; if ($factor == 1 ){…
smith
  • 3,232
  • 26
  • 55
10
votes
1 answer

How do I access a value of a nested Perl hash?

I am new to Perl and I have a problem that's very simple but I cannot find the answer when consulting my Perl book. When printing the result of Dumper($request); I get the following result: $VAR1 = bless( { '_protocol' => 'HTTP/1.1', …
st.
  • 156
  • 1
  • 6
10
votes
1 answer

Perl Array References and avoiding "Type of arg 1 to keys must be hash" error

I have a scalar $subscribers that could be undef, reference to a HASH, or reference to an ARRAY. I have assigned the sample values $VAR1, $VAR2 and $VAR3 for testing. I'm only interested in $subscribers when it is a reference to an ARRAY, where by…
h q
  • 1,168
  • 2
  • 10
  • 23
9
votes
1 answer

Why no interpolation within `map` BLOCK?

This throws an error in Perl v5.20: use strict; use warnings; my @a = (2,3,9); my %b = map { "number $_" => 2*$_ } @a; Error: syntax error at a.pl line 4, near "} @a" Execution of a.pl aborted due to compilation errors. This doesn't: use…
user1427008
1
2 3
25 26