hash array loop in sequence?

S

Slickuser

my %data = (
"name" => "BOB",
"age" => 35,
"sex" => "M",
"phone" => "555-5555",
"city" => "LA",
"state" => "CA",
"country" => "US"
);

foreach my $entry (keys %data)
{
print $entry ." ". $data{$entry} ."\n";
##can't use sort keys or values
}


If I got my hash array like this.
How can loop through in the order of name, age, sex, phone, city,
state, country?
 
A

Andrzej Adam Filip

Slickuser said:
my %data = (
"name" => "BOB",
"age" => 35,
"sex" => "M",
"phone" => "555-5555",
"city" => "LA",
"state" => "CA",
"country" => "US"
);

foreach my $entry (keys %data)
{
print $entry ." ". $data{$entry} ."\n";
##can't use sort keys or values
}


If I got my hash array like this.
How can loop through in the order of name, age, sex, phone, city,
state, country?

Have you considered using (simple) SQL database? e.g. SQLite
see DBD::SQLite on CPAN.org

If you want only one order of entries then consider using btree tied
hashes supported by DB_File module.
 
O

Owen

my %data = (
        "name"                => "BOB",
        "age"         => 35,
        "sex"         => "M",
        "phone"               => "555-5555",
        "city"                => "LA",
        "state"               => "CA",
        "country"     => "US"
);

foreach my $entry (keys %data)
{
        print $entry ." ". $data{$entry} ."\n";
        ##can't use sort keys or values

}

If I got my hash array like this.
How can loop through in the order of name, age, sex, phone, city,
state, country?

You might want to try and array of your keys in the order you want

my @array =("name","age", etc);

then for each entry, loop through the array values printing
$data{$array[n]}



Owen
 
S

smallpond

my %data = (
"name" => "BOB",
"age" => 35,
"sex" => "M",
"phone" => "555-5555",
"city" => "LA",
"state" => "CA",
"country" => "US"
);

foreach my $entry (keys %data)
{
print $entry ." ". $data{$entry} ."\n";
##can't use sort keys or values

}

If I got my hash array like this.
How can loop through in the order of name, age, sex, phone, city,
state, country?


foreach my $entry qw(name age sex phone city state country )
{
print $entry ." ". $data{$entry} ."\n";
}
 
J

Jürgen Exner

Slickuser said:
my %data = (
"name" => "BOB",
"age" => 35,
"sex" => "M",
"phone" => "555-5555",
"city" => "LA",
"state" => "CA",
"country" => "US"
);
If I got my hash array like this.
How can loop through in the order of name, age, sex, phone, city,
state, country?

Sorted by any of those values or sorted by all of those values, i.e.
people with same name will be sorted further by age, and those with same
name and age will be sorted further by sex?

Either way, see the man page as well as 'perldoc -q sort'.

If the former then e.g. (sketch only, untested):

@sorted = sort {$a{name} cmp $b{name}} , keys(%data);
foreach (@sorted){
print $_;
}

If the latter then you got two options:
- Perl's sort() is stable. Therefore you can sort by the lowest criteria
first. And then sort again by the second-lowest and the third-lowest
etc. until the last pass is with your top criteria. This is expensive
because you are sorting the same data many times over.
- or you can do it in one pass by simply creating a proper custom
compare function as described in the man page and the FAQ.

jue
 
X

xhoster

smallpond said:
foreach my $entry qw(name age sex phone city state country )
{
print $entry ." ". $data{$entry} ."\n";
}

I was surprised that this works. I thought the list of a foreach *had* to
be in parentheses, and that the qw() wouldn't count as parentheses.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
X

xhoster

Joe Smith said:
linux% perl -le 'foreach my $entry @ARGV {print $entry}' a b c d e
Array found where operator expected at -e line 1

As the error message says, it is looking for an operator, not just "(".

Is "(" really an operator?

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
E

Eric Pozharski

On 2008-11-05 said:
If I got my hash array like this. How can loop through in the order
of name, age, sex, phone, city, state, country?

print "$_ => $data{$_}\n"
foreach (qw( name age sex phone city state country ));

=begin off-topic

regarding time that passed for your post to be answered -- consider
getting real news-feed.

=end off-topic
 
E

Eric Pozharski

linux% perl -le 'foreach my $entry @ARGV {print $entry}' a b c d e
Array found where operator expected at -e line 1

As the error message says, it is looking for an operator, not just
"(".

It is looking for an array (I<-w> rocks):

Array found where operator expected at -e line 1, at end of line
(Missing operator before ?)
Scalar value @ARGV { print $x } better written as $ARGV { print $x } at
-e line 1.
syntax error at -e line 1, near "$x @ARGV "
Execution of -e aborted due to compilation errors.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top