[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: [PERL] Variabler?



On Fri, 4 Jan 2002, Søren Kongstad wrote:

> Jeg er netop gået igang med at lege med Perl, og synes det er
> fantastisk brugbart. Uheldigvis er jeg stødt på et problem, jeg ikke
> lige kan overskue, og jeg har ikke lige kunnet finde svaret i
> dokumentationen. (Hvad er for øvrigt den bedtse Perl bog at starte
> med?)

Learning Perl (O'Reilly) Find den på http://www.noamazon.com

> Det program jeg er ved at lave skal samle nogen inddatafiler fra
> forskellige leverandører sammen, og behandler dem ud fra fil navnet.
:
> Da der er mange typer filer, og mange outputformater, vil jeg kunne
> gøre disse variable.
:
> men formatet for $outf vil jeg også gøre variabelt. Noget i stil med:
>
> my $outfileformat= "20$1$2$3.txt";
> my $regexp="t(\\d\\d)(\\d\\d)(\\d\\d)";
>
> foreach (@dfiles)
> 	if (/$myregexp/i){
> 	    $outf="$outdir\\$myoutfileformat";
> 	    if (-e $outf){
>                     ....
>                     }
> 	    else {... }
> 	}
>
> Hvilket selvfølgelig ikke virker - men hvordan gør jeg noget
> tilsvarende. Ideen er at jeg i en konf fil gemmer regexp, og outfil
> formater, så det er nemt at tilrette når leverandørerne ændrer deres
> formater.

#!/usr/bin/perl

# Definer nogle formater
my %formater =
    ( "format1" => {
        # simpelt 6 cifre
        "regexp" => '^(\d\d)(\d\d)(\d\d)$',
        # 123456 => "20123456.txt"
        "output" => '"20$1$2$3.txt"'
        },
      "cpr" => {
          # et cpr-nummer: 123456-7890
        "regexp" => '^(\d\d\d\d\d\d)-(\d\d\d\d)$',
        # 123456-7890 => "Cpr-1234567890.txt"
        "output" => '"Cpr-$1$2.txt"'
        },
      "date" => {
          # En dato DD/MM/YY
        "regexp" => '^(\d\d)\/(\d\d)\/(\d\d)$',
          # 12/11/98 => "19"."98-11-12" = "1998-11-12"
          # 12/11/08 => "20"."08-11-12" = "2008-11-12"
        "output" => '($3 > 30 ? "19" : "20")."$3-$2-$1"'
        },
      );

# Nogle eksempler, som vi kan bruge
my @dfiles=("123456-2345","12/13/98","880099");

foreach $f (@dfiles) {
    # løb alle formaterne igennem og prøv dem et efter et
    for $format (keys %formater) {
        # Test om formatet $format matcher filnavnet $f
	# Det er en faldgrube hvis et filnavn matches af
	# flere af formaterne
        if ($f=~/$formater{$format}{"regexp"}/i){
            # eval udfører strengen som et perl-script
            # og udregner derved det nye navn
            $outf=$outdir."\\". eval $formater{$format}{"output"};
            print $outf,"\n";
            if (-e $outf){
                1...2;
            } else {
                1...2;
            }
        }
    }
}



/Ole
-- 
Søges: 2-3 vær. lejlighed i Kbh K N Ø V. Eje, andel eller leje. Pris < 4500/md
Så hvis du kender nogen, der kender nogen, så drop en mail.



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 19:54 CEST [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] *