1
2
3
4
5
sub array_to_string {
my ($switch, @array) = @_;
my $string = join($switch, @array);
...
}
Guest BobCode: (dl )1
2
3
4
5sub array_to_string {
my ($switch, @array) = @_;
my $string = join($switch, @array);
...
}
Das funktioniert zwar (nach dem Motto: bitte nicht Anfassen), erfordert aber den Aufruf der Subroutine obligatorisch mit zwei Parametern.
1
2
3
4
5
6
7
8
9
10
11
12
use strict;
use warnings;
sub array_to_string {
my ($switch, @array) = @_;
my $string = join($switch, @array);
print "string: '$string'\n";
}
array_to_string('foo');
__END__
Ausgabe:
string: ''
1 2 3 4 5 6 7 8 9 10 11
sub array_to_string { my %p = @_; #$p{'switch'} //= '-'; $p{'switch'} = '-' unless exists $p{'switch'}; # edit join $p{'switch'}, @{ $p{'array'} }; } array_to_string( array => [1..3], #switch => '#', );
1 2 3 4 5 6 7 8
sub array_to_string { my ($array, $switch) = @_; $switch = '-' if $#_ < 1; join $switch, @$array; } array_to_string([1..3], '#'); #array_to_string([1..3]);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# Delimiter and Array my $ref = { DEL => ';', ARR => [1,2,3,4,5], }; my $str = array_to_string($ref) or die "no delimiter is given"; print $str; sub array_to_string{ my $ref = shift; if(exists $ref->{DEL}){ return join($ref->{DEL}, @{$ref->{ARR}}); } else{ return } } # 1;2;3;4;5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#!/usr/bin/perl -w use strict; use warnings; use HTML::TreeBuilder::XPath; use LWP::Simple; my $html = get("http://www.perl.org"); my $tree = HTML::TreeBuilder::XPath->new; $tree->parse($html); my @contents = $tree->findvalues(q{//p[@class="intro"]}); # usw. $tree->delete; print join ("", @contents);
$article = array_to_string({array => @contents, switch => "\n"});
1
2
3
Odd number of elements in anonymous hash at [...] line 125, <STDIN> line 1.
Reference found where even-sized list expected at [...] <STDIN> line 1.
Can't use an undefined value as an ARRAY reference at [...] <STDIN> line 1.
1
2
Odd number of elements in anonymous hash at [...] <STDIN> line 1.
Odd number of elements in anonymous hash at [...] <STDIN> line 1.
$article = array_to_string(array => \@contents, switch => "\n");
$article = array_to_string(\@contents, "\n");
1 2 3 4 5
my @arr = (1,2,3,4,5); my $ref = [@arr]; my $ref1 = \@arr; print "$ref->[0]\n"; # 1 print "$ref1->[0]\n"; # 1
2011-05-21T17:05:08 rostiEine Array-Referenz kannst Du auf zwei Wegen erzeugen:
Code (perl): (dl )1 2 3 4 5my @arr = (1,2,3,4,5); my $ref = [@arr]; my $ref1 = \@arr; print "$ref->[0]\n"; # 1 print "$ref1->[0]\n"; # 1
2011-05-21T12:25:39 Bob@moritz: Hast du da nicht einen Denkfehler drin? Nach meinem Verständnis fungiert der Parameter 'foo' in deinem Beispiel als Separator, das Array ist leer, so dass der Separator genau einmal ausgegeben wird.