Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Swap Input lines

by 300zxmuro (Initiate)
on Apr 19, 2012 at 03:27 UTC ( [id://965835]=perlquestion: print w/replies, xml ) Need Help??

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

Howdy Monks, I need help with a tiny part of this code............. If you have input lines formatted like "x=y", write Perl code to swap all of them (for example, "y=x"). Fill in where commented in the code below

#!/usr/bin/perl use strict; use warnings; my $filein = .... my $fileout = ...; open(INFILE, $filein) || die "Can’t open input ’$filename’: $!\n"; open(OUTFILE, "> fileout") || die "Can’t open output ’$filename’: $!\n +"; while (my $input_line = <INFILE>) { chomp($input_line); my $output_line = ''; # FILL IN CODE HERE print OUTFILE "$output_line\n"; }

Here is a sample input file: Mary Smith = 345-78-9090 Joseph Alioto = 976-83-1234 Corresponding sample output file: 345-78-9090 = Mary Smith 976-83-1234 = Joseph Alioto You also may assume that both field1 and field2 do NOT contain any "="s.

Replies are listed 'Best First'.
Re: Swap Input lines
by GrandFather (Saint) on Apr 19, 2012 at 03:34 UTC

    I suggest you look at either perlretut for a regular expression solution or look at split for a split solution. The code would look rather similar in either case.

    True laziness is hard work
Re: Swap Input lines
by AnomalousMonk (Archbishop) on Apr 19, 2012 at 04:16 UTC
    You also may assume ...

    ... that PerlMonks is not a homework answering service – and the OPed question smells a lot like homework!

Re: Swap Input lines
by jwkrahn (Abbot) on Apr 19, 2012 at 04:18 UTC
    while ( my $input_line = <INFILE> ) { chomp $input_line; my $output_line = join '', reverse split /(\s*=\s*)/, $input_line, + 2; print OUTFILE "$output_line\n"; }
Re: Swap Input lines
by trizen (Hermit) on Apr 19, 2012 at 09:31 UTC
    $output_line = "$3$2$1" if $input_line =~ /^([a-z][a-z\s]*[a-z])(\W+)(\d[\d-]*\d)$/i;
    It accepts input like this:
    Mary Smith = 345-78-9090 Joseph Alioto 976-83-1234
    and the output will be:
    345-78-9090 = Mary Smith 976-83-1234 Joseph Alioto
      This is a detail, but this regex  /^([a-z][a-z\s]*[a-z])(\W+)(\d[\d-]*\d)$/i is too specific.
      Input file contains lines with "x=y" format, so we must only search for "=" -- see jwkrahn's contribution: Re: Swap Input lines.
      Here some examples where this regex doesn't match:
      Jean-Paul Goude = 22-22 Marcel Duchamp = One-Two-Two Victoire Passage = 118_218 Philip K. Dick = 123-456
        You are right. I misunderstood the OPs problem.
      So, you're swapping around pieces of the input, even on lines that do not contain a =? And on the other hand, you don't swap around input on lines that do contain a =, but don't match a regexp you distilled from looking at two examples? Yet, you still allow many other lines that aren't either the first, or the second example.

      That's an, uhm, "interesting" way of answering the question. I wonder whether it's helpful.

Re: Swap Input lines
by aaron_baugher (Curate) on Apr 19, 2012 at 14:23 UTC

    When parsing text, it's important to know exactly what format(s) you have to deal with. Any vagueness or uncertainty will likely lead to wrong results. For instance, you say that your lines are formatted like "x=y", but then you give examples that are "x = y". See the difference? You have to know which it is -- or if it could be both -- so you can parse it properly. In this case, you might need to split on '=', ' = ', or '/\s*=\s*/'.

    Aaron B.
    My Woefully Neglected Blog, where I occasionally mention Perl.

Re: Swap Input lines
by sundialsvc4 (Abbot) on Apr 19, 2012 at 13:13 UTC

    When testing code like this (for homework or otherwise...) sometimes it helps to start by creating very simple “proof of concept” programs that build-up to the final deliverable.   In other words, writing two throw-away programs:

    1. One that simply reads from a disk file and prints it out line-by-line.
    2. Another one that doesn’t read from a disk file at all, but rather tests the code that you intend to use to separate “a particular string” into its component parts.

    In the second case, have a look at (for instance) Test::More.   I’m not talking so much about “this particular module or its many brethren,” but rather the approach; the strategy; the philosophy.   Small, focused test-cases (whether built using that module or not) which fully exercise the code that you have written.   For instance, the splitting code needs to be able to handle at least all of the following input data:   (quote-marks are not part of the data)

    • empty string
    • “foo=bar”
    • “foo=”
    • “=bar”
    • “=”
    • Combinations involving more than one “=” character, if applicable.
    • In all of the above, testing of blank-spaces and various types of characters.

    When that particular segment of code can be thoroughly and completely tested in isolation, and passes all such tests, then you know that you can put your full body weight upon that section of code and move forward.   While it may sound strange to “go to all of that trouble” for what may well be homework, believe me, it’s worth it.

    Design and build every piece of software from the bottom up, using pro-actively tested components that are tested at the earliest possible opportunity and at every subsequent stage of integration.   Even homework.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-24 02:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found