On 11-03-17 02:04 PM, Rob Dixon wrote:
A beginners list isn't the place to introduce arbitrarily complex Perl
constructs. Replies have to be sensitive to the ability of the OP or
they may co

On the other hand, telling them to use a kluge to get the results they want is very bad advice. Tell them to do it right from the start.

If you want to tack the data together into a string, do it right:

#!/usr/bin/env perl

use strict;
use warnings;

use Data::Dumper;

# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent   = 1;

# Set maximum depth for Data::Dumper, zero means unlimited
local $Data::Dumper::Maxdepth = 0;

# TBD

sub encode_tsv {
  my @data = @_;
  my %encodings = (
    "\t" => "\\t",
    "\\" => "\\\\",
  );

  for my $datum ( @data ){
    $datum =~ s{ ( [\\\t] ) }{$encodings{$1}}gmsx;
  }
  return join( "\t", @data );
}

sub decode_tsv {
  my $str = shift @_;
  my %decodings = (
    "t"  => "\t",
    "\\" => "\\",
  );

  my @data = split( "\t", $str );
  for my $datum ( @data ){
    $datum =~ s{ \\ ( . ) }{$decodings{$1}}gmsx;
  }
   return @data;
}

sub show {
  my @data = @_;
  print Dumper \@data;

  my $str = encode_tsv( @data );
  print Dumper $str;

  @data = decode_tsv( $str );
  print Dumper \@data;
}

print "Test 1:\n";
show( qw( fee fie foo fum ));

print "\n\nTest 1:\n";
show(
  "string\twith\ttabs",
  "string\\with\\backslashes",
);




--
Just my 0.00000002 million dollars worth,
  Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software:  Fail early & often.

Eliminate software piracy:  use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to