Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re^4: Extracting Unique elements

by smandape1 (Acolyte)
on Jun 17, 2011 at 17:06 UTC ( #910217=note: print w/replies, xml ) Need Help??

in reply to Re^3: Extracting Unique elements
in thread Extracting Unique elements

Hey Thanks a lot. I got it. I greatly appreciate your help. I have another small issue. when I get the array of something I extract, I try to print them using the code

if (ref($bar) eq "ARRAY") { foreach my $q (@$bar){ print FH $q ,","; } } else { print FH $bar; }

But, this doesn't seem to be of help. I want to do something that if its an array separate them by ',' else just print the individual without ','. But when I use the above code it also prints the single element with a ',' and in an array it gives ',' after the last element. I know how to solve the same issue in XSLT its something like

<xsl:test if position() != last()> <xsl:value-of select="."/>; </xsl:test> <xsl:test if position() = last()> <xsl:value-of select="."/> </xsl:test>

Thus everything is separated by ';' except for the last element and if its only a single element then it gets printed as it is. Can I do something same in perl, can you please help. Thank you, Sammed

Replies are listed 'Best First'.
Re^5: Extracting Unique elements
by wind (Priest) on Jun 17, 2011 at 20:00 UTC

    Just use join instead of a loop:

    print FH ref($bar) eq "ARRAY" ? join(',', @$bar) : $bar;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://910217]
[marto]: "Eugenie Scott, executive director of the National Center for Science Education, dubbed this approach the Gish gallop, describing it as "where the creationist is allowed to run on for 45 minutes or an hour, spewing forth torrents of error that the
[marto]: evolutionist hasn't a prayer of refuting in the format of a debate." She also criticized Gish for failing to answer objections raised by his opponents"
[erix]: one would hope evolutionists haven't any prayers anyway
[marto]: obviously someone could be religious, but not creationist
[erix]: "Nothing in Intelligent Design makes sense except in the light of Creationism" <-- I made that one up myself (free after Dobzhansky )
[erix]: yes. Deplorable marto, deplorable.
[marto]: the situation seemed similar to this one, majority of the contributrions are nonsense, doesn't address any questions ...
[marto]: meh, I've been called worse :P

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (12)
As of 2017-07-28 15:45 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (431 votes). Check out past polls.