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

Trimming unpack'ed strings

by Anonymous Monk
on Nov 05, 2008 at 21:37 UTC ( [id://721824]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks I'm using unpack to extract fixed width fields from a text file that I need to convert to csv. Because the text in the fields is shorter than the field width I want to trim the whitespace, and there's an added complication of the csv file having fields that aren't in the fixed width file. At the moment I'm doing the most straight forward thing like so (minus the string trimming):
my ($catno, $desc, $pack, $min, $gst, $cost, $unitcost, $retail, $barc +ode) = unpack("A7 x2 A30 A4 A4 x13 A7 x15 A8 A8 A8 x13 A13", $_); print CSTORE_OUT join(", ", "", "", $catdesc, "", "", "", "", $catno, +$barcode, $desc, $unitcost, "", "", "", $retail, $pack, $gst, "", "") +."\n";
Is there a one liner I can use to unpack, trim and join? TIA

Replies are listed 'Best First'.
Re: Trimming unpack'ed strings
by almut (Canon) on Nov 05, 2008 at 22:14 UTC

    Maybe something like that?

    $_ = "foo bar baz "; my $catdesc = " extra "; print join(",", ("", map /^\s*(.*\S)/, unpack("a10 a10 a10", $_), $cat +desc)[0,0,-1,0,0,3,0,0,1,2,0] ); # output: ",,extra,,,baz,,,foo,bar,"

    (I simplified the format, because you didn't provide sample input, and I was too lazy to create a matching string)

    That said, writing unnecessarily compact code usually isn't the best idea...

    Update: added $catdesc to show that extra variables could in principle also be handled.

Re: Trimming unpack'ed strings
by BrowserUk (Patriarch) on Nov 05, 2008 at 22:41 UTC

    If the data are left justified within their fields, then the 'An' template you are using will trim trailing spaces. Unfortunately there is no equivalent for trimming right justified fields.

    You could achieve the re-ordering of the fields using the '@n' absolute positioning template. And you can generate the empty fields using 'A0'.

    The fly in the ointment as far as producing your output is concerned, it that you have a variable in your output $catdesc, that does not appear to come from your input?

    Otherwise you could achieve the one-liner something along the lines of:

    print CSTORE_OUT join ',', unpack '(A0)2 @9A30 (A0)4 @0A7 @119A13 @9A30 @90A8 ...', $_;

    ***NOTE*** That won't work as it's a) incomplete; b) I've re-used the $desc field inplace of the absent $catdesc variable.

    It's just an example of what might have been possible had all the output fields come from the input. Whether there is any advantage in doing this as a one-liner is another question.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Trimming unpack'ed strings
by gwadej (Chaplain) on Nov 05, 2008 at 22:15 UTC

    I probably would not do this as a one-liner. The trimming can easily be handled with map, though.

    my ($catno, $desc, $pack, $min, $gst, $cost, $unitcost, $retail, $barcode) = map { s/\s+$//; $_ } unpack("A7 x2 A30 A4 A4 x13 A7 x15 A8 A8 A8 x13 A13", $_);

    The call to map makes a new list from the old list after removing any trailing whitespace.

    It seems like there's a way to get unpack to return an empty field. But that escapes me at the moment.

    Update: as jwkrahn points out, my change shouldn't be needed because of the 'A'. I should have looked at the unpack template instead of the OP reference to needing to strip the whitespace.

    G. Wade

      The  map { s/\s+$//; $_ } is superfluous because the  "A" format of unpack already removes all trailing whitespace.

Log In?
Username:
Password:

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

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

    No recent polls found