Search This Blog

Tuesday, September 27, 2011

HowTo: Remove duplicates in Linux PATH

CodeSnippets: Remove duplicates in PATH [awk] [shell] [osx] [mac] [bash] [unix] [linux] [environment] [remove] [variable] [path] [duplicate]

Some apps just add paths to your path every time you run them. In this case, my path was over a screen full of duplicates. When adding to a path, grep is an easy check to see if it's there. But when you call something else, you don't know what you're going to get, and how many times it will put the same thing in there.

This one line script is perfect, just hard to find. Couldn't find the original author, but kudo's.

PATH="$(printf "%s" "${PATH}" | /usr/bin/awk -v RS=: -v ORS=: '!($0 in a) {a[$0]; print}')"

(Change ':' to ';' for Windows.)

Friday, September 23, 2011

HowTo: Clear PHP undefined offset notice when looping through arrays

undefined offset notice when looping through arrays in php - Stack Overflow

PHP Notice:  Undefined offset: 118 in /usr/share/resultsdb/www/index.php on line 465, referer: ...

While this seems to simple to write about, there weren't any posts with the answer. Even one specifically about sparse matrices didn't answer the question.
There is also bug in PHP is_array() which started this journey in the first place.
https://bugs.php.net/bug.php?id=55772

There are several HowTo's on 'PHP Notice Undefined offset' from webmasters wondering what is filling their logs, to lectures on bad programming.No one answers the question.

I have a sparse matrix, that needs each element to display on an html page. So when the matrix is loaded with defined values there are many undefined values, 6158 for example.

This is displayed as a square matrix using an HTML table - A conflict between good PHP programming and HTML programming. The undefined elements need some html, so the 'for loops' must touch each element of the array.

is_array is the correct call to use. It tests array elements, and returns true or false if they are valid or not.

While the is_array function returns true and false - PHP still issues the warning message.

For now, the isset function can be used. It's not obvious that is applies to arrays, but it works.

$data_array=array();
$data_array[2][10] = "defined";
$row_count = 10;
$col_count = 10;

//...fill with the defined values...many values undefined.

for ($r = 0; $r < $row_count; $r++)
{
echo "";
  for ($c = 0; $c < $col_count; $c++)
  {
     if (isset($data_array[$r+1][$c+1])){
       $data = $data_array[$r+1][$c+1];
     } else {
       $data = NULL;
     }
    echo "";
  }
}

Thursday, September 15, 2011

HowTo: Split Python strings with multiple separators

Python strings split with multiple separators - Stack Overflow

What to you do if each caller uses a different delimiter to list multiple integers in a string?

While the above link is nice, it doesn't treat punctuation as a delimiter to split, so 123:123 becomes 123123.

The right answer is subtle, but works:
import string,re
thestring = "Hey, you - what are you doing 123:456 here!?"
print re.sub('['+string.punctuation+']',' ',thestring).split()


http://docs.python.org/library/re.html


Assume they will use punctuation as a delimiter...Not perfect, but true in my case today.

import string
thestring = " 1234 234 345345 123:234:345:"
s=list(thestring)
print ''.join([ o for o in s if not o in string.punctuation ]).split()

Tuesday, September 13, 2011

HowTo: Append to the end of line in a file.

Sed - An Introduction and Tutorial
http://www.linuxquestions.org/questions/linux-newbie-8/sed-append-text-to-end-of-line-if-line-contains-specific-text-how-can-this-be-done-684650/

The original answer needed a little more to be general, and allow a variable to be passed to sed to create append to the end of a particular line. In this case, a file when sourced defined an environment variable. This variable could then be used in a for loop to process a list of things to do.

cat <<EOF >${RESULTS}/suite_id.prop
SUITE_ID=""
TEST_ID=""
EOF
suite_id=1104;test_id=3007
# tried gawk...sed wins this time.
#{ rm ${RESULTS}/suite_id.prop && gawk '/SUITE_ID/{print $0 "$suite_id :";}' > ${RESULTS}/suite_id.prop; } < ${RESULTS}/suite_id.prop
sed -i '/SUITE_ID/s|"$| '"$suite_id"'"|' ${RESULTS}/suite_id.prop
sed -i '/TEST_ID/s|"$| '"$test_id"'"|' ${RESULTS}/suite_id.prop
source ${RESULTS}/suite_id.prop
for i in $SUITE_ID; do
    echo "SUITE_ID: $i"
done
for i in $TEST_ID; do
    echo "TEST_ID: $i"
done

Wednesday, September 7, 2011

HowTo: MySQL SELECT * FROM TABLE WHERE DISTINCT `field`

MySQL :: MySQL 5.0 Reference Manual :: 12.2.8 SELECT Syntax

Web applications need to allow end users to select groups of rows by project, product or something interesting. So the best way to do that is to ask the database for a list of unique groups. Unfortunately, MySQL syntax doesn't have one, and the journey can lead to some commands that are downright scary for a simple database kind of thing to do.

SELECT * , count( `product` )
FROM `results`
GROUP BY `product`
HAVING count( `product` ) >=1
LIMIT 0 , 30


and bug the mysql people to add:
SELECT * FROM `results` WHERE DISTINCT `product` ;)

Thursday, September 1, 2011

HowTo: Graphviz on Cygwin

Cygwin Ports
Doxygen without graphiz is like (pick your cliche)...

Discovered Cygwin Ports, with 64 bit compilers, gnome, kde and more. From meld to majong there are several hundred packages here, and it is a seamless install within cygwin setup.exe.