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

401 questions
120
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,163
  • 11
  • 38
  • 56
47
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,022
  • 2
  • 21
  • 38
30
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
  • 79,569
  • 109
  • 264
  • 349
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
  • 772,407
  • 210
  • 1,477
  • 1,841
22
votes
7 answers

How can I maintain the order of keys I add to a Perl hash?

How can I maintain the order of actual list after counting its occurrence using a hash in the following program? For example, are a b e a c d a c d b etc. Using hash, i counted the occurrence of each element. and what i want is: a 3 b …
Cthar
  • 623
  • 2
  • 8
  • 11
18
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
  • 908
  • 3
  • 12
  • 32
17
votes
9 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
  • 683
  • 2
  • 12
  • 31
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
  • 176,854
  • 291
  • 683
  • 1,015
14
votes
5 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
  • 231
  • 1
  • 4
  • 14
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
  • 303
  • 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!
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,045
  • 24
  • 51
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
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
8
votes
2 answers

Search for hash in an array by value

I have a function which extracts Excel data into an array of hashes like so: sub set_exceldata { my $excel_file_or = '.\Excel\ORDERS.csv'; if (-e $excel_file_or) { open (EXCEL_OR, $excel_file_or) || die("\n can't open…
DidYouJustDoThat
1
2 3
26 27