Dades i operadors

539 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
539
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Dades i operadors

  1. 1. * Before we start...$ perldoc#!/usr/bin/perluse strict;* Datos y Operadores * Datos Escalares * StringsDatos de texto o binarios, sin significado para el programa, delimitados típicamente por comillas sencillas o dobles:my $world = Mundo;my $escaped = Dan OBannon;my $backslash = ; * Comillas doblesmy $newline = "n";my $tab = "t";my $input = "hellonworld!";my $data = "08029t25";my $data = "08029,"Eixample Esquerra"";my $data = qq{08029,"Eixample Esquerra"}; * Interpolaciónmy $hello = "Hola, $world!n";my $hello = qq{Hola, "$world"!n}; * Funciones típicas de cadena length EXPR substr EXPR,OFFSET,LENGTH,REPLACEMENT substr EXPR,OFFSET,LENGTH substr EXPR,OFFSET index STR,SUBSTR,POSITION index STR,SUBSTR Functions for SCALARs or strings "chomp", "chop", "chr", "crypt", "hex", "index", "lc", "lcfirst", "length", "oct", "ord", "pack", "q//", "qq//", "reverse", "rindex", "sprintf", "substr", "tr///", "uc", "ucfirst", "y///" => Más cuando hablemos de expresiones regulares * Números$ perldoc perlnumber $n = 1234; # decimal integer $n = 0b1110011; # binary integer $n = 01234; # *octal* integer $n = 0x1234; # hexadecimal integer $n = 12.34e−56; # exponential notation $n = "−12.34e56"; # number specified as a string $n = "1234"; # number specified as a string * Hashes, Listas y Arreglos 1 de 5
  2. 2. @ Arreglo my @author; $author[0] = Asimov; $author[1] = Bear; $author[2] = King; print "First author is " . $author[0] . "n"; print "Last author is " . $author[$#author] . "n"; print "Last author is " . $author[-1] . "n"; print "There are " . @author . " authorsn"; my @num = (0..10); my @a = (@b,@c); * Funciones típicas para arreglos sort SUBNAME LIST sort BLOCK LIST sort LIST grep BLOCK LIST grep EXPR,LIST join EXPR,LIST split /PATTERN/,EXPR,LIMIT split /PATTERN/,EXPR split /PATTERN/# ++$learn$ perldoc List::Util% Hash my %name; $name{Asimov} = Isaac; $name{Bear} = Greg; $name{King} = Stephen; * Funciones típicas para hashes keys HASH values HASH print "Authors are ".keys(%name)."n"; * Identificadores, variables y su notación NotaciónLas variables son precedidas de un sigil que indica el tipo de valor de la variable:$ Escalar@ Arreglo% Hashe.g.my($nombre, @nombre, %nombre);Para acceder el elemento de un arreglo o hash, se utiliza el sigil escalar:$nombre{$id} = Ann;$nombre[$pos] = Ben;Es posible acceder múltiples valores al mismo tiempo utilizando el sigil de arreglo:@nombre{@keys} = @values; 2 de 5
  3. 3. @suspendidos = @nombre[@selected];@authors =("Asimov","Bear","King");@authors = qw(Asimov Bear King); Variables especiales$ perldoc perlvar$ perldoc English @ARGV @INC %ENV %SIG $@ @_ $_ * I/O * Consola STDIN STDOUT STDERR e.g. print STDERR "This is a debug messagen";#!/usr/bin/perluse strict;use Chatbot::Eliza;my $eliza = Chatbot::Eliza->new();while(<STDIN>) { chomp; print "> ".$eliza->transform($_),"n";} * Ficheros$ perldoc -f open$ perldoc perlopentut # # Reading from a file # # Please dont do this! open(FILE,"<$file"); # Do *this* instead open my $fh, <, filename or die "Cannot read $filename: $!n"; while (<$fh>) { chomp; say "Read a line $_"; } # # Writing to a file # use autodie; open my $out_fh, >, output_file.txt; print $out_fh "Heres a line of textn"; say $out_fh "... and heres another"; close $out_fh; # $fh->autoflush( 1 ); 3 de 5
  4. 4. # # Theres much more! # :mmap, :utf8, :crlf, ... # open($fh, ">:utf8", "data.utf"); print $fh $out; close($fh);# ++$learn$ perldoc perlio$ perldoc IO::Handle$ perldoc IO::File * Operadores y su precedencia ( y su asociatividad ( y su arity ( y su fixity ) ) )$ perldoc perlop left terms and list operators (leftward) left −> nonassoc ++ −− right ** right ! ~ and unary + and − left =~ !~ left * / % x left + − . left << >> nonassoc named unary operators nonassoc < > <= >= lt gt le ge nonassoc == != <=> eq ne cmp ~~ left & left | ^ left && left || // nonassoc .. ... right ?: right = += −= *= etc. left , => nonassoc list operators (rightward) right not left and left or xor => Atencion! print ( ($foo & 255) + 1, "n"); print ++$foo; * Operadores * Numericos * String * Logicos * Bitwise * Especiales * Estructuras de Control if (EXPR) BLOCK if (EXPR) BLOCK else BLOCK if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK LABEL while (EXPR) BLOCK LABEL while (EXPR) BLOCK continue BLOCK LABEL until (EXPR) BLOCK LABEL until (EXPR) BLOCK continue BLOCK LABEL for (EXPR; EXPR; EXPR) BLOCK LABEL foreach VAR (LIST) BLOCK LABEL foreach VAR (LIST) BLOCK continue BLOCK LABEL BLOCK continue BLOCK e.g. for(my $i=0; $i<@author; ++$i) { print $i.": ".$author[$i]."n"; } 4 de 5
  5. 5. for(0..$#author) { print $_.": ".$author[$_]."n"; } foreach my $i (0..$#author) { print $i.": ".$author[$i]."n"; } for(0..1000000) { print $_,"n"; } while(<$fh>) { # Skip comments next if /^#/; ... } * Modificadores if EXPR unless EXPR while EXPR until EXPR foreach LIST e.g. print "Value is $valn" if $debug; print $i++ while $i <= 10; Contexto * Void find_chores(); * Lista my @all_results = find_chores(); my ($single_element) = find_chores(); process_list_of_results( find_chores() ); my ($self,@args) = @_; * Escalar print "Hay ".@author." autoresn"; print "Hay ".scalar(@author)." autoresn";# ++$learn$ perldoc -f wantarray * Numerico my $a = "a"; my $b = "b"; print "a is equal to b " if ($a==$b); # Really? print "a is equal to b " if ($a eq $b); * Cadena my $a = 2; print "The number is ".$a."n"; * Booleano my $a = 0; print "a is truen" if $a; $a = 1; print "a is truen" if $a; $a = "a"; print "a is truen" if $a; my $numeric_x = 0 + $x; # forces numeric context my $stringy_x = . $x; # forces string context my $boolean_x = !!$x; # forces boolean context 5 de 5

×