Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Perl starter with big problem.

by b_vulnerability (Novice)
on Nov 03, 2008 at 10:09 UTC ( [id://721054]=perlquestion: print w/replies, xml ) Need Help??

b_vulnerability has asked for the wisdom of the Perl Monks concerning the following question:

Hi! I think the title says it all. I've just started perl, searched and googled around like crazy, but I am not able to do what I need to. So, the problem is basically this one. I have a text which is in this format:
< < PU------------- doc doc S-NN----------- id id S-MN----------- = = X-------------- " " PU-------------
and I need to clean this up and make it look like this:
</</PU doc/doc/S-NN id/id/S-MN =/=/X "/"/PU

I basically need something that replaces any TAB with a / char, and get rid of all that ------------ crappy lines. I've been suggested to use perl and regex, but I have no idea on how to do that. Maybe isn't even difficult, but I can't come up with anything.
Every suggestion will be very appreciated.
Thanks

Replies are listed 'Best First'.
Re: Perl starter with big problem.
by GrandFather (Saint) on Nov 03, 2008 at 10:30 UTC

    The first things to do are find a good place to ask questions (you've made a good start there at least), find some tutorial material (Tutorials will probably help) and find the Perl documentation which is available on line at perl. For regular expression help see perlretut and perlre.

    Next you need to write a little code and see what you can manage. If (when) you run into trouble condense the problem down to a small sample that demonstrates the issue and ask us for help (we like to see evidence of a little effort anyway).

    Remember to always use strictures (use strict; use warnings;) - they catch a lot of errors of all sorts earlier rather than later.

    However, for your immediate task what you really want is the translation operator tr/// (look for tr/SEARCHLIST/REPLACEMENTLIST/cds under Regexp Quote Like Operators in perlop).


    Perl reduces RSI - it saves typing
      Thanks for your suggestion. I'll read everything and come back if I still can't find my answer.
      Thanks!
Re: Perl starter with big problem.
by missingthepoint (Friar) on Nov 03, 2008 at 10:38 UTC
    #!/usr/bin/perl -w use strict; die "Usage: $0 <infile>\n" unless @ARGV; open(my $fh, '<', $ARGV[0]) or die "open(): $!\n"; while (<$fh>) { chomp; s{\s+}{/}g; s/-+$/ /; print; }

    I really like your attitude. :)

    The body of the while is what you should grok. This solution is slightly different to what GrandFather suggested in that it'll work with any amount of whitespace between elements. Of course, it will break if the (tab-separated) elements can contain spaces - in which case GrandFather's solution is better. It depends on your data.

    Also: at some point you might want to look at Text::CSV (which, as the POD says, should really be called 'Anything Separated Values'.


    Indicators of geekdom:
    • You get a kick out of finishing sentences with domain names, because of the syntactic overlap of certain written languages and DNS notation.
    • You understood the previous sentence.
Re: Perl starter with big problem.
by parv (Parson) on Nov 03, 2008 at 11:41 UTC

    Darn it, no, the title does not "says it all". It is just another useless collection of words for a title.

    Something to do with "transformation" or "parsing" in the title would have given better indication about your problem.

      Sorry, you're right. My first post here, and I have allready messed up. I'm sorry. Next time, I'll be more accurate
Re: Perl starter with big problem.
by brsaravan (Scribe) on Nov 03, 2008 at 10:48 UTC
    You can try this, since your replacement characters are different for each substitution.
    #! /usr/bin/perl use strict; my $str='< < PU------------- doc doc S-NN----------- id id S-MN----------- = = X-------------- " " PU-------------'; $str =~ s/\n//g; $str =~ s/\s+/\//g; $str =~ s/(-){2,}/ /g; print "$str\n";
Re: Perl starter with big problem.
by planetscape (Chancellor) on Nov 03, 2008 at 22:22 UTC
Re: Perl starter with big problem.
by sanku (Beadle) on Nov 04, 2008 at 12:21 UTC
    hi try out this one.
    #!/usr/bin/perl open(FILE,"txt1.txt"); while(<FILE>){ ($val1,$val2,$val3)=split(/\s+/,$_); ($val3)=split(/-/,$val3); $vv=sprintf "$val1".'/'."$val2".'/'."$val3"."%3s".'/'; chop($vv); print $vv; } close(FILE);
      <critic level="harsh">
      • Your code does not produce the output desired by the OP.
      • You should use warnings; because it will generate warning messages which point to bugs in the code, particularly the odd usage of sprintf.
      • You should always check the success of open.
      • There is no indentation, which makes it difficult to understand the code.
      • use strict; to avoid other common programming mistakes.
      • It is a good practice to use the 3-argument form of open and to use lexical filehandles:
      open my $fh, '<', 'txt1.txt' or die "Can not open txt1.txt: $!";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://721054]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-25 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found