如何将数组中的值与perl中的文本文件相乘

时间:2016-03-23 19:44:42

标签: arrays perl

嘿伙计们,所以我一直试图教自己perl。所以我在网上发现了这个项目,我已经开始做了,但现在我被卡住了。问题是要求我打印每个记录的轨道周期,而不是几天。现在这是输入文件文本:

function setLetter(letter) {
  var img = document.getElementById("image");
  if (letter && letter.length == 1) {
    img.src =       "http://thecodingninja.com/muslim/fingerspellingimg/"+letter+"abc.jpg";
  }
  else {
    img.src = "";
  }
}

function nextLetter(word) {
  if (word.length > 0) {
    var firstLetter = word.charAt(0);
    setLetter(firstLetter);
    setTimeout(nextLetter, 1000, word.substring(1));
  }
  else {
    setLetter();

  }
    }

function spellWord() {
  var word = document.getElementById("text").value;
  nextLetter(word.toUpperCase());
  }

这就是我为我的代码提出的,但它不起作用:/请帮助我。

Adrastea XV Jupiter 129000 0.30 0.00 0.00 Jewitt 1979
Amalthea V Jupiter 181000 0.50 0.40 0.00 Barnard 1892
Ananke XII Jupiter 21200000 -631 147.00 0.17 Nicholson 1951
Ariel I Uranus 191000 2.52 0.00 0.00 Lassell 1851
Atlas XV Saturn 138000 0.60 0.00 0.00 Terrile 1980
Belinda XIV Uranus 75000 0.62 0.03 0.00 Voyager2 1986
Bianca VIII Uranus 59000 0.43 0.16 0.00 Voyager2 1986
Caliban XVI Uranus 7169000 -580 140. 0.08 Gladman 1997
Callirrhoe XVII Jupiter 24100000 ? ? ? Sheppard 2000
Callisto IV Jupiter 1883000 16.69 0.28 0.01 Galileo 1610
Calypso XIV Saturn 295000 1.89 0.00 0.00 Pascu 1980
Carme XI Jupiter 22600000 -692 163.00 0.21 Nicholson 1938
Chaldene XXI Jupiter 23387000  -733.7 165.2 0.238 Sheppard 2000
Charon I Pluto 20000 6.39 98.80 0.00 Christy 1978
Cordelia VI Uranus 50000 0.34 0.14 0.00 Voyager2 1986
Cressida IX Uranus 62000 0.46 0.04 0.00 Voyager2 1986
Deimos II Mars 23000 1.26 1.80 0.00 Hall 1877
Desdemona X Uranus 63000 0.47 0.16 0.00 Voyager2 1986
Despina V Neptune 53000 0.33 0.00 0.00 Voyager2 1989
Dione IV Saturn 377000 2.74 0.02 0.00 Cassini 1684
Earth III Sun 149600000 365.26 0.00 0.02 - -
Elara VII Jupiter 11737000 259.65 28.00 0.21 Perrine 1905
Enceladus II Saturn 238000 1.37 0.02 0.00 Herschel 1789
Epimetheus XI Saturn 151000 0.69 0.34 0.01 Walker 1980
Erinome XXV Jupiter 23279000 728 164.9 0.266 Sheppard 2000
Europa II Jupiter 671000 3.55 0.47 0.01 Galileo 1610
Galatea VI Neptune 62000 0.43 0.00 0.00 Voyager2 1989
Ganymede III Jupiter 1070000 7.15 0.19 0.00 Galileo 1610
Harpalyke XXII Jupiter 21132000 623.3 148.6 0.226 Sheppard 2000
Helene XII Saturn 377000 2.74 0.20 0.01 Laques 1980
Himalia VI Jupiter 11480000 250.57 28.00 0.16 Perrine 1904
Hyperion VII Saturn 1481000 21.28 0.43 0.10 Bond 1848
Iapetus VIII Saturn 3561000 79.33 14.72 0.03 Cassini 1671
Io I Jupiter 422000 1.77 0.04 0.00 Galileo 1610
Iocaste XXIV Jupiter 20216000 ? ? ? Sheppard 2000
Isonoe XXVI Jupiter 23078000 ? ? ? Sheppard 2000
Janus X Saturn 151000 0.69 0.14 0.01 Dollfus 1966
Juliet XI Uranus 64000 0.49 0.06 0.00 Voyager2 1986
Jupiter V Sun 778330000 4332.71 1.31 0.05 - -
Kalyke XXIII Jupiter 23745000 ? ? ? Sheppard 2000
Larissa VII Neptune 74000 0.55 0.00 0.00 Reitsema 1989
Leda XIII Jupiter 11094000 238.72 27.00 0.15 Kowal 1974
Lysithea X Jupiter 11720000 259.22 29.00 0.11 Nicholson 1938
Mars IV Sun 227940000 686.98 1.85 0.09 - -
Megaclite XIX Jupiter 23911000 ? ? ? Sheppard 2000
Mercury I Sun 57910000 87.97 7.00 0.21 - -
Metis XVI Jupiter 128000 0.29 0.00 0.00 Synnott 1979
Mimas I Saturn 186000 0.94 1.53 0.02 Herschel 1789
Miranda V Uranus 130000 1.41 4.22 0.00 Kuiper 1948
Moon I Earth 384000 27.32 5.14 0.05 - -
Naiad III Neptune 48000 0.29 0.00 0.00 Voyager2 1989
Neptune VIII Sun 4504300000 60190.00 1.77 0.01 Adams 1846
Nereid II Neptune 5513000 360.13 29.00 0.75 Kuiper 1949
Oberon IV Uranus 583000 13.46 0.00 0.00 Herschel 1787
Ophelia VII Uranus 54000 0.38 0.09 0.00 Voyager2 1986
Pan XVIII Saturn 134000 0.58 0.00 0.00 Showalter 1990
Pandora XVII Saturn 142000 0.63 0.00 0.00 Collins 1980
Pasiphae VIII Jupiter 23500000 -735 147.00 0.38 Melotte 1908
Phobos I Mars 9000 0.32 1.00 0.02 Hall 1877
Phoebe IX Saturn 12952000 -550.48 175.30 0.16 Pickering 1898
Pluto IX Sun 5913520000 90550 17.15 0.25 Tombaugh 1930
Portia XII Uranus 66000 0.51 0.09 0.00 Voyager2 1986
Praxidike XXVII Jupiter 20964000 ? ? ? Sheppard 2000
Prometheus XVI Saturn 139000 0.61 0.00 0.00 Collins 1980
Prospero XVIII Uranus 16568000 -2019 152. 0.44 Holman 1999
Proteus VIII Neptune 118000 1.12 0.00 0.00 Voyager2 1989
Puck XV Uranus 86000 0.76 0.31 0.00 Voyager2 1985
Rhea V Saturn 527000 4.52 0.35 0.00 Cassini 1672
Rosalind XIII Uranus 70000 0.56 0.28 0.00 Voyager2 1986
Saturn VI Sun 1429400000 10759.50 2.49 0.06 - -
Setebos XIX Uranus 17681000 -2239 158. 0.57 Kavelaars 1999
Sinope IX Jupiter 23700000 -758 153.00 0.28 Nicholson 1914
Stephano XX Uranus 7948000 -674 143. 0.24 Gladman 1999
Sun - - - - - - - -
Sycorax XVII Uranus 12213000 -1289 153. 0.51 Nicholson 1997
Taygete XX Jupiter 23312000 ? ? ? Sheppard 2000
Telesto XIII Saturn 295000 1.89 0.00 0.00 Smith 1980
Tethys III Saturn 295000 1.89 1.09 0.00 Cassini 1684
Thalassa IV Neptune 50000 0.31 4.50 0.00 Voyager2 1989
Thebe XIV Jupiter 222000 0.67 0.80 0.02 Synnott 1979
Themisto XVIII Jupiter 7507000 ? ? ? Sheppard 2000
Titan VI Saturn 1222000 15.95 0.33 0.03 Huygens 1655
Titania III Uranus 436000 8.71 0.00 0.00 Herschel 1787
Trinculo XXI Uranus 8578000 -759 167.0 0.208 Gladman 2001
Triton I Neptune 355000 -5.88 157.00 0.00 Lassell 1846
Umbriel II Uranus 266000 4.14 0.00 0.00 Lassell 1851
Uranus VII Sun 2870990000 30685.00 0.77 0.05 Herschel 1781
Venus II Sun 108200000 224.70 3.39 0.01 - -

4 个答案:

答案 0 :(得分:1)

您通常不想将文件读入数组。完全摆脱my @galaxy并使用默认的内部"当前记录"变量$_代替。您也可以使用默认的拆分分隔符(空格):

while (<$fh>){
   ... = split;
   ...
      print;
...

答案 1 :(得分:1)

更改[this读入数组变量]:

while ( my @galaxy = <$fh>) {

进入[这读入标量变量]:

while ( my $galaxy = <$fh>) {

你的分裂:

my($planet,$number_moons,...) = split / /, $galaxy;

使用标量变量。

在perl中,您可以有三种类型的变量,它们都具有相同的名称,并且相关。它们的区别在于语法:

# scalar:
$foo = 17;

# array:
@foo = (23);

# hash:
$bar{"x"} = 37;
%foo = %bar;

printf("scalar: %d\n",$foo);
printf("array: %d\n",$foo[0]);
printf("hash: %d\n",$foo{"x"});

答案 2 :(得分:0)

我可能会沿着这些方向做点什么

#!/usr/bin/perl

use warnings;
use strict;

my $file = 'planets.txt';

open my $fh, '<', $file;

while (my $galaxy = <$fh>){
    chomp $galaxy;
    my($planet,$number_moons,$obj_orbit,$orbital_radius,$orbital_period,$orbital_inclination, $orbtial_eccentricity, $discoverer, $year) = split ' ', $galaxy;

    if (my ($inseconds) = $orbital_period =~ /([0-9.]+)/){
        $inseconds = $orbital_period * 86400;
        $galaxy =~ s/$orbital_period/$inseconds/;
    }
    print $galaxy . "\n";
}
close($fh);

我预见到的唯一问题是,如果轨道周期之前的任何值与轨道周期相同,那么该值将被正则表达式匹配所取代。还有另一种选择,就是将该行加载到哈希中并以这种方式更新值,我将提供一个示例

---更新---

这是一个使用哈希的例子,哈希的缺点是它不会以相同的顺序打印出来,所以为了解决这个问题,你可以用这样的数组填充哈希

#!/usr/bin/perl

use warnings;
use strict;

my $file = 'planets.txt';
my %celestial;

open my $fh, '<', $file;

while (my $galaxy = <$fh>){
    chomp $galaxy;
    my($planet,$number_moons,$obj_orbit,$orbital_radius,$orbital_period,$orbital_inclination, $orbtial_eccentricity, $discoverer, $year) = split ' ', $galaxy;

    if ($orbital_period =~ /([0-9.]+)/){ $orbital_period = $orbital_period * 86400; }

    my @info;
    push @info, ($number_moons,$obj_orbit,$orbital_radius,$orbital_period,$orbital_inclination, $orbtial_eccentricity, $discoverer, $year);
    $celestial{$planet} = \@info;
}

close($fh);

for my $keys (sort keys %celestial){
    print $keys;
    foreach my $k(@{$celestial{$keys}}) {
        print " $k";
    }
    print "\n"; 
}

希望这可以帮助你

答案 3 :(得分:0)

# Read from STDIN rather than explicitly opening a file
# Read data into $_
while (<>) {
  # Split on comma to get individual fields
  my @satellite = split /,/, $_;
  # Update the fifth field
  $satellite[4] *= 86_400;
  # Join the array back into a string
  print join ',', @satellite;
}