Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Multiline Hash - how to get rid of formatting?

by tospo (Hermit)
on Apr 30, 2012 at 07:49 UTC ( #968018=note: print w/ replies, xml ) Need Help??


in reply to Multiline Hash - how to get rid of formatting?

What do you mean with "formatted text"? That you get a lot of spaces? That's because you have those spaces in your string values for 'body'. If you want a newline you must use \n as in your second example, Perl doesn't care about newlines you used to format the script in which you defined the data structure.

But there are some other issues here:

  • your array seems to contain hashes with a single key ('Body'). If there are no other keys then you don't want an array of hashes, you want an array of arrays but maybe that's just the case in your short exapmle here? Is it possible that 'Body' is the name of a workflow? In that case you want a hash of arrays
  • you probably don't want the formatting ()newlines, spaces) in your strings at all. A data structure should store data. formatting should be done at the point of consumption of the data structure. For exmaple: if you need to print the data structure as a list in HTML you want to wrap each element in list tags. If you want to dump it to the command line, you want to add newlines at the end of each string. Try to separate data from presentation. Feel free to ask if you need more details about this.
  • the actual strings ("1. Blah..") you are storing there represent an ordered list, therefore you should store them as an array, not one long string as you do now.
  • In short, I think this is what you should have:

    my %workflows = ( 'workflow1' => [ "first step", "second step", "third step" ], 'workflow2' => [ "first step", "second step", "third step" ], );
    Print a workflow to the command line:
    print "Workflow1 is:\n" . join("\n", @%{$workflows{'workflow1'}})."\n" +;
    I hope that helps to get you started - welcome to Perl!


Comment on Re: Multiline Hash - how to get rid of formatting?
Select or Download Code
Re^2: Multiline Hash - how to get rid of formatting?
by Anonymous Monk on Apr 30, 2012 at 08:37 UTC
      hmmm, I must admit that I learned something today: after many years of working with Perl I never knew that newlines from your source code are preserved in double-qouted strings but it seems they really are.
      OK, forget the bit about HAVING to put the \n in your strings although I would still advice to do that if you want nelines because it is a nightmare having to figure out how where the spaces end and where there is an actual newline when editing your code.
      However, the main point of my reply remains valid: don't try to stuff formatting into your data structure unless you really have to. Format the data as and when you need to print it and store it as pure data. I also think that it is true that the OP hasn't got the right data structure yet to present his data.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://968018]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2014-07-26 08:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls