Perl: File into Array
#!/usr/bin/perl
use strict;
use warnings;
my $filename = 'hobbies.txt';
open (my $handle , '<:encoding br="" filename=""> or die "Could not open file '$filename' $!";
chomp (my @hobbies = <$handle>);
close $handle;
my $totalHobbies = $#hobbies;
print "hobbies: @hobbies\n";
print "---------------: $totalHobbies\n";
print "1st. hobby: $hobbies[0]\n";
my $index=9;
print "nine hobby: $hobbies[$index]\n";
print "last hobby: $hobbies[$totalHobbies]\n";
my $randomHobby=int(rand($totalHobbies+1));
print "Random hobby: $hobbies[$randomHobby]\n";
# generate ages between 18 and 118 years
my $minYear=18;
my $range=100;
my $randomYear=int(rand($range)) + $minYear;
print "Random year: $randomYear\n";
use strict;
use warnings;
my $filename = 'hobbies.txt';
open (my $handle , '<:encoding br="" filename=""> or die "Could not open file '$filename' $!";
chomp (my @hobbies = <$handle>);
close $handle;
my $totalHobbies = $#hobbies;
print "hobbies: @hobbies\n";
print "---------------: $totalHobbies\n";
print "1st. hobby: $hobbies[0]\n";
my $index=9;
print "nine hobby: $hobbies[$index]\n";
print "last hobby: $hobbies[$totalHobbies]\n";
my $randomHobby=int(rand($totalHobbies+1));
print "Random hobby: $hobbies[$randomHobby]\n";
# generate ages between 18 and 118 years
my $minYear=18;
my $range=100;
my $randomYear=int(rand($range)) + $minYear;
print "Random year: $randomYear\n";
Comentarios