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

Hello, I am trying to create a string that uses some variables..
my $today = $time{'dd_mm_yyyy'}; foreach $icp (@icpList) { $fn = "C:\\SI\\Logs\\log_".$icp."_$today.txt"; print $fn;
now when i run this, i get: C:\SI\Logs\log_
10.37.72.20
_22_11_2005.txt
now i am wondering why is this putting stuff on new lines. any help would be good.

Replies are listed 'Best First'.
Re: Creating a String
by sh1tn (Priest) on Nov 22, 2005 at 14:12 UTC
    Probably because the elements in @icpList array contains new line character. Try this before the cycle:
    map{ s/\n//g }@icpList;


Re: Creating a String
by ptum (Priest) on Nov 22, 2005 at 14:13 UTC
    Well, we can't see what you have in @icpList, but I would guess that you may wish to chomp($icp) before you include it in your string, to remove any trailing \n character that might be there. Or perhaps even run it through a substitution regex to strip off any leading spaces or return characters, something like this (untested): $icp =~ s/^\s+(.*?)\s+$/$1/;

    No good deed goes unpunished. -- (attributed to) Oscar Wilde
Re: Creating a String
by davorg (Chancellor) on Nov 22, 2005 at 14:20 UTC

    Because all of the elements in @icpList have newlines on the end of them. I'd guess that you have read them in from a file without using chomp on them. Try running chomp @icpList before this code.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Creating a String
by Anonymous Monk on Nov 22, 2005 at 15:25 UTC
    i figured out why i have been getting errors. When using the XML::Mini::Document package, when it generates an XML child it does so in this manner:
    <TAG> TEXT </TAG>
    I wasnt taking into account the extra tab that this package puts in. I used map{ s/\s//g }@icpList; and it works now. Thanks for the help
Re: Creating a String
by Anonymous Monk on Nov 22, 2005 at 14:35 UTC
    i used the map{ s/\n//g }@icpList; before i enter the loop, now i get this...
    C:\SI\Logs\log_ 10.37.72.20 _22_11_2005.txt

      I think you really need to show us the code that builds @icpList. Without it we're just guessing (or debugging by telepathy - which really gives me a headache!)

      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

        well, all of this started when i decided to use a new module to generate some XML. i put XML::Mini::Document and it began to mess around with everything lol.
        sub genIcpList($){ my @icpList; my $fn = $_[0]; my $xp = XML::XPath->new(filename => $fn); my $ip; #specify what we are looking for... my $nodeset = $xp->find('/AutomationInformation/NodeList/Node/IPAd +dress'); # find all IP's #foreach instance of '/AutomationInformation/NodeList/Node/IPAddre +ss', add it to the array foreach my $node ($nodeset->get_nodelist) { $ip = substr(XML::XPath::XMLParser::as_string($node), 11, -12) +; @icpList = (@icpList, $ip); } #return the array to main return @icpList; }
        this recieves an xml file and looks for every occurance of a tag and then places the content into an array.