×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

print the first and the last element of an hash

print the first and the last element of an hash

print the first and the last element of an hash

(OP)
Hello all! I've a csv file like :


City sunshine Temperature
Ajaccio 2790 14.7
Lyon 2072 11.4
Marseille 2763 14.2
Brest 1729 10.8
Lille 1574 9.7
Paris 1833 11.2
Strasbourg 1685 9.7

I'd like to print just the city+temperature with the highest and the lowest temperature. How?

CODE --> perl

#!/usr/bin/perl
use warnings;
use diagnostics;
use Data::Dumper;
$Data::Dumper::Terse = 1;
$Data::Dumper::Indent = 0;
 

#definyn arrays for temperatures, files and city
my (@temp, @file, @ville);
my ($file, $line);
my %temperature;

$file = "temperatures.csv";
#I don't consider the first line of the file and I put just the city and  its temperture in two arrays

open (F, "<", $file);
while(defined($line = <F>)) {
if ( $. > 1 ) {
	@file = split(/ /, $line);
	push(@ville, $file[0]);
	push(@temp, $file[2]);
	}
}

#then I create the hash and print what  there's inside

@temperature{@ville} = @temp;
print '%temperature : ', Dumper(\%temperature), "\n";

#now I "order" the hash by value 
foreach my $value (sort {$temperature{$a} <=> $temperature{$b} || ($a cmp $b) } keys %temperature) 
{
     print " the city of $value and its temperature : $temperature{$value}\n";
}


close(F);

#is there a way to print ONLY the first and the last element in order to have the highest and the lowest temperature? 

RE: print the first and the last element of an hash

If you need the hash for other purposes

CODE

my@tord;
@tord=sort{$temperature{$a}<=>$temperature{$b}}keys%temperature;
print 'The city of $tord[0] and its minimum temperature : ',$temperature{$tord[0]},"\n";
print 'The city of $tord[-1] and its maximum temperature : ',$temperature{$tord[-1]},"\n"; 
otherwise

CODE

my($maxt,$mint,$temp,$maxv,$minv,$ville)=(-1e300,1e300);
open(F,$file);
$line=<F>;
while(<F>){
  ($ville,undef,$temp)=split;
  if($temp>$maxt){
    $maxt=$temp;
    $maxv=$ville;
  }
  if($temp<$mint){
    $mint=$temp;
    $minv=$ville;
  }
}
print 'The city of $minv and its minimum temperature : ',$mint,"\n";
print 'The city of $maxv and its maximum temperature : ',$maxt,"\n"; 

Franco
http://www.xcalcs.com : Online engineering calculations
http://www.megamag.it : Magnetic brakes for fun rides
http://www.levitans.com : Air bearing pads

RE: print the first and the last element of an hash

(OP)
Thank you (Grazie!),

I found this

CODE --> perl

$regex .= $t9{$num}; 

in an exercice. What is ".=" and how is it called? This is the first time I've seen it.

RE: print the first and the last element of an hash

Just like $a+=$b means sum $b to $a and store the result in $a, $a.=$b means append string $b to string $a and store the result in $a.

http://www.xcalcs.com : Online engineering calculations
http://www.megamag.it : Magnetic brakes for fun rides
http://www.levitans.com : Air bearing pads

RE: print the first and the last element of an hash

(OP)
Thank you! now is clear!

$regex .=$t9{$t};

it's the same as...

$regex = $regex.$t9{$t}; !

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close