[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: SV: [PROGRAMMERING] Adgang til ekstern csv fil



"Henning Hansen" <sslug@sslug> writes:

> Jeg har brugt denne kode til at søge i en lokal fil og det virker perfekt.
>
> Nu skal jeg læse filen (txttv.csv) fra en anden server på www, og har så
> prøvet med
>
> $file3name = 'http://xxx.xxx.xxx/txttv.csv'; men det virker ikke, er der
> nogen der kan hjælpe mig.

Nej og du kan heller ikke i en shell skrive

  $ cat http://xxx.xxx.xxx/txttv.csv

og få vist filen. Der skal noget kode til der snakker HTTP. Deet kan
modulet LWP::Simple gøre for dig.

> Her er et uddrag af den oprindelige kode.


> $file3name = 'txttv.csv';
>
>
> &open_file3("FILE1","",$file3name);
>
>     $line = &read_file3("FILE1");
>     while (($line = &read_file3("FILE1"))) {

Dette ser ikke specielt perl-agtigt ud. Hvad gør funktionerne
open_file3 og read_file3 ud over noget svarende til:

  open(FILE,"r",$file3name);
  $line = readline(FILE); # samme som '$line = <FILE>;
  while ($line = readline(FILE))) {
      ... 
  }


Nå, men til dit egentlige spørgsmål. I det ovenstående læser du filen
en linje ad gangen og behandler den linje. Når du skal læse fra en
webserver er det letteste at læse hele filen ind i en variabel
først. Det gør du med modulet LWP::Simple:

Indsæt følgende i toppen af din perl-kode:

  use LWP::Simple;

Og så følgende istedet for ovenstående:

  $content = get($file3name);
  @lines   = split(/\n/,$file3);

  $line = shift @lines;
  while ($line = shift @lines) {
      ...
  }

>          @table3data = split(/\s*\;\s*/,$line ,$fields - 10);

Uændret ....

> {
>           for ($x=0;$x < @table3data;$x++) {
>
>
> }

Og hvad er det lige du prøver at gøre her?

> ##################  DETTE ER KUN ET UDDRAG ########################
>
> if ($event1 =~ /^normal$/i){if (($table3data[0] > 2700) and ($table3data[0]
> < 2739)){if (($table3data[3] =~ /^1$/i) and ($table3data[7] >= 0) and
> ($table3data[7] ne "X") and ($table3data[7] ne "")) {$h01 =
> $table3data[7];$u01 = $table3data[8]; if ($table3data[4] ne "U"){$status01 =
> "I GANG";} if ($table3data[4] =~ /^1$/i){$status01 = "SLUT"; $tegn01 = "1";
> $odds01 = $table3data[5]; $rodds01 = $table3data[9];}if ($table3data[4] eq
> "X"){$status01 = "SLUT"; $tegn01 = "X"; $odds01 = $table3data[5]; $rodds01 =
> $table3data[9];}if ($table3data[4] =~ /^2$/i){$status01 = "SLUT"; $tegn01 =
> "2"; $odds01 = $table3data[5]; $rodds01 = $table3data[9];}}}}
> if ($event2 =~ /^normal$/i){if (($table3data[0] > 2700) and ($table3data[0]
> < 2739)){if (($table3data[3] =~ /^2$/i) and ($table3data[7] >= 0) and
> ($table3data[7] ne "X") and ($table3data[7] ne "")) {$h02 =
> $table3data[7];$u02 = $table3data[8]; if ($table3data[4] ne "U"){$status02 =
> "I GANG";} if ($table3data[4] =~ /^1$/i){$status02 = "SLUT"; $tegn02 = "1";
> $odds02 = $table3data[5]; $rodds02 = $table3data[9];}if ($table3data[4] eq
> "X"){$status02 = "SLUT"; $tegn02 = "X"; $odds02 = $table3data[5]; $rodds02 =
> $table3data[9];}if ($table3data[4] =~ /^2$/i){$status02 = "SLUT"; $tegn02 =
> "2"; $odds02 = $table3data[5]; $rodds02 = $table3data[9];}}}}
> if ($event3 =~ /^normal$/i){if (($table3data[0] > 2700) and ($table3data[0]
> < 2739)){if (($table3data[3] =~ /^3$/i) and ($table3data[7] >= 0) and
> ($table3data[7] ne "X") and ($table3data[7] ne "")) {$h03 =
> $table3data[7];$u03 = $table3data[8]; if ($table3data[4] ne "U"){$status03 =
> "I GANG";} if ($table3data[4] =~ /^1$/i){$status03 = "SLUT"; $tegn03 = "1";
> $odds03 = $table3data[5]; $rodds03 = $table3data[9];}if ($table3data[4] eq
> "X"){$status03 = "SLUT"; $tegn03 = "X"; $odds03 = $table3data[5]; $rodds03 =
> $table3data[9];}if ($table3data[4] =~ /^2$/i){$status03 = "SLUT"; $tegn03 =
> "2"; $odds03 = $table3data[5]; $rodds03 = $table3data[9];}}}}
> if ($event4 =~ /^normal$/i){if (($table3data[0] > 2700) and ($table3data[0]
> < 2739)){if (($table3data[3] =~ /^4$/i) and ($table3data[7] >= 0) and
> ($table3data[7] ne "X") and ($table3data[7] ne "")) {$h04 =
> $table3data[7];$u04 = $table3data[8]; if ($table3data[4] ne "U"){$status04 =
> "I GANG";} if ($table3data[4] =~ /^1$/i){$status04 = "SLUT"; $tegn04 = "1";
> $odds04 = $table3data[5]; $rodds04 = $table3data[9];}if ($table3data[4] eq
> "X"){$status04 = "SLUT"; $tegn04 = "X"; $odds04 = $table3data[5]; $rodds04 =
> $table3data[9];}if ($table3data[4] =~ /^2$/i){$status04 = "SLUT"; $tegn04 =
> "2"; $odds04 = $table3data[5]; $rodds04 = $table3data[9];}}}}
> if ($event5 =~ /^normal$/i){if (($table3data[0] > 2700) and ($table3data[0]
> < 2739)){if (($table3data[3] =~ /^5$/i) and ($table3data[7] >= 0) and
> ($table3data[7] ne "X") and ($table3data[7] ne "")) {$h05 =
> $table3data[7];$u05 = $table3data[8]; if ($table3data[4] ne "U"){$status05 =
> "I GANG";} if ($table3data[4] =~ /^1$/i){$status05 = "SLUT"; $tegn05 = "1";
> $odds05 = $table3data[5]; $rodds05 = $table3data[9];}if ($table3data[4] eq
> "X"){$status05 = "SLUT"; $tegn05 = "X"; $odds05 = $table3data[5]; $rodds05 =
> $table3data[9];}if ($table3data[4] =~ /^2$/i){$status05 = "SLUT"; $tegn05 =
> "2"; $odds05 = $table3data[5]; $rodds05 = $table3data[9];}}}}
> if ($event6 =~ /^normal$/i){if (($table3data[0] > 2700) and ($table3data[0]
> < 2739)){if (($table3data[3] =~ /^6$/i) and ($table3data[7] >= 0) and
> ($table3data[7] ne "X") and ($table3data[7] ne "")) {$h06 =
> $table3data[7];$u06 = $table3data[8]; if ($table3data[4] ne "U"){$status06 =
> "I GANG";} if ($table3data[4] =~ /^1$/i){$status06 = "SLUT"; $tegn06 = "1";
> $odds06 = $table3data[5]; $rodds06 = $table3data[9];}if ($table3data[4] eq
> "X"){$status06 = "SLUT"; $tegn06 = "X"; $odds06 = $table3data[5]; $rodds06 =
> $table3data[9];}if ($table3data[4] =~ /^2$/i){$status06 = "SLUT"; $tegn06 =
> "2"; $odds06 = $table3data[5]; $rodds06 = $table3data[9];}}}}
> if ($event7 =~ /^normal$/i){if (($table3data[0] > 2700) and ($table3data[0]
> < 2739)){if (($table3data[3] =~ /^7$/i) and ($table3data[7] >= 0) and
> ($table3data[7] ne "X") and ($table3data[7] ne "")) {$h07 =
> $table3data[7];$u07 = $table3data[8]; if ($table3data[4] ne "U"){$status07 =
> "I GANG";} if ($table3data[4] =~ /^1$/i){$status07 = "SLUT"; $tegn07 = "1";
> $odds07 = $table3data[5]; $rodds07 = $table3data[9];}if ($table3data[4] eq
> "X"){$status07 = "SLUT"; $tegn07 = "X"; $odds07 = $table3data[5]; $rodds07 =
> $table3data[9];}if ($table3data[4] =~ /^2$/i){$status07 = "SLUT"; $tegn07 =
> "2"; $odds07 = $table3data[5]; $rodds07 = $table3data[9];}}}}
> if ($event8 =~ /^normal$/i){if (($table3data[0] > 2700) and ($table3data[0]
> < 2739)){if (($table3data[3] =~ /^8$/i) and ($table3data[7] >= 0) and
> ($table3data[7] ne "X") and ($table3data[7] ne "")) {$h08 =
> $table3data[7];$u08 = $table3data[8]; if ($table3data[4] ne "U"){$status08 =
> "I GANG";} if ($table3data[4] =~ /^1$/i){$status08 = "SLUT"; $tegn08 = "1";
> $odds08 = $table3data[5]; $rodds08 = $table3data[9];}if ($table3data[4] eq
> "X"){$status08 = "SLUT"; $tegn08 = "X"; $odds08 = $table3data[5]; $rodds08 =
> $table3data[9];}if ($table3data[4] =~ /^2$/i){$status08 = "SLUT"; $tegn08 =
> "2"; $odds08 = $table3data[5]; $rodds08 = $table3data[9];}}}}

wow. Kan du vedligeholde det?

>
>
> }}
>
> close(FILE1);

-- 
 Peter Makholm     |  Det sae banken osse: Hvis du overhovet vil have noen
 sslug@sslug |  glæde af din fremtid ska du ikke vente til i morgen.
 http://hacking.dk |                                  -- Divus, 2004-10-26


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2006-01-01, 02:01 CET [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *