This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Lindex help

Help for those learning Tcl or writing their own scripts.
C
COBRa
Halfop
Posts: 49
Joined: Fri Jan 04, 2013 8:23 am

Lindex help

Post by COBRa »

hi im trying to store mp3 info to a dbase but im having a small issue

im pulling the info for an ID3 line link this

iD3 > Express_Viviana_Ft._Natt_-_Feel_The_Soul-(HC0045)-WEB-2014-SRG > Techno from 2014 at 44100 Hz

and using lindex to split the info out

Code: Select all

set rlsname [string trim [lindex $arg 1]]
         set genre [string trim [lindex $arg 3]]
         set year [string trim [lindex $arg 5]]
         set sampling [string trim [lindex $arg 7]]
but if the genre is a double named like this

iD3 > Syphilectomy-Circumcised_Abominable_Deformity-MCD-2014-DiTCH > Death Metal from 2014 at 44100 Hz

it messes with the other lindex entries after the genre

how can i get round the double named issue plz
User avatar
CrazyCat
Revered One
Posts: 1306
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

I think you have to get the informations from the end after the "variable size":

Code: Select all

set rlsname [string trim [lindex $arg 1]]
set genre [string trim [join [lrange 3 $arg end-5]]]
set year [string trim [lindex $arg end-3]]
set sampling [string trim [lindex $arg end-1]]
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Hello,
The first problem here, is that you are using lindex on a string. The lindex command (and other list-commands such as lrange, foreach, etc) is designed to operate on properly formatted tcl lists, nothing else.

If you have a common field separator, then you could use the split command to split the string into a proper list. Unfortunately, the strings you've posted so far suggests that there is no such common separator. Thus, your best option would be to use pattern matching, such as regular expressions with the regexp command.
NML_375
User avatar
CrazyCat
Revered One
Posts: 1306
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

When using lindex on a string, tcl splits on the space.
It's a bad usage, but it works.

I agree with nml375, the first thing to do is:

Code: Select all

set arg [split $arg]
It'll propermly format the elements of the list, particularly interesting if you have special chars in them.
C
COBRa
Halfop
Posts: 49
Joined: Fri Jan 04, 2013 8:23 am

Post by COBRa »

@nml375 Thanks for your reply bud but being new to tcl could you give me an example to look at plz as im not sure on what you mean i only know the lindex way
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Hello,
A crude example matching your posted strings is as follows;

Code: Select all

set pattern {iD3 > ([^>]+) > ([^>]+) from ([0-9]+) at ([0-9]+) Hz}

regexp $pattern $arg match rlsname genre year sampling
Learning regular expressions can be a bit daunting, so you might want to read up on some of the rules;
http://www.tcl.tk/man/tcl8.5/TclCmd/re_syntax.htm

In the above example, [^>] means match anything but >
The + following means match at least one instance of this atom
The [0-9] pattern will match digits 0 through 9, and once again with the + meaning one or more digits.
The strings "from", "at", "Hz" are literal strings to be matched, just like " > ".
Finally, the () are used to mark this piece for "reporting", meaning it will be added to the variables named at the end of the command line.
NML_375
C
COBRa
Halfop
Posts: 49
Joined: Fri Jan 04, 2013 8:23 am

Post by COBRa »

many thx for the swift reply do i still use the lindex code as well ie

Code: Select all

set rlsname [string trim [lindex $arg 1]]
         set genre [string trim [lindex $arg 3]]
         set year [string trim [lindex $arg 5]]
         set sampling [string trim [lindex $arg 7]]
or is it something like this

Code: Select all

set rlsname[string trim [lindex $pattern $arg 1]]
as i have no clue lol
User avatar
CrazyCat
Revered One
Posts: 1306
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

If you use the code posted by nml375, all variables are setted.
You just have to use $rlsname, $genre, ..., without any set.

Have a look on http://wiki.tcl.tk/986
C
COBRa
Halfop
Posts: 49
Joined: Fri Jan 04, 2013 8:23 am

Post by COBRa »

So I could use something like

Code: Select all

$rlsname=""
             $genre=""
As I'm not sure
User avatar
CrazyCat
Revered One
Posts: 1306
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

...

the regexp line assigns the values to rlsname, genre, year and sampling.
It's an alternative to the affectations using the lindex. It replaces your 4-lines code and do exactly the same.
C
COBRa
Halfop
Posts: 49
Joined: Fri Jan 04, 2013 8:23 am

Post by COBRa »

Ok many thx I understand just one last question if one of the fields is blank ie genre for example how could I show it as a - for example
User avatar
CrazyCat
Revered One
Posts: 1306
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

Just a small question before answering you: even if a field is blank, is it mentionned, like:

Code: Select all

iD3 > Express_Viviana_Ft._Natt_-_Feel_The_Soul-(HC0045)-WEB-2014-SRG > from 2014 at 44100 Hz
I hope it is, and you have to change the pattern into:

Code: Select all

set pattern {iD3 > ([^>]+)? > ([^>]+)? from ([0-9]+)? at ([0-9]+)? Hz}
And to set an empty field to "-", just do:

Code: Select all

if {[string trim $genre]==""]} { set genre "-" }
C
COBRa
Halfop
Posts: 49
Joined: Fri Jan 04, 2013 8:23 am

Post by COBRa »

thx bud added the code but im getting an error

[16:14:13] Tcl error [cmdencryptedincominghandler]: can't read "genre": no such variable

this is my code

Code: Select all

proc rls:id3 {nick host hand chan arg} {
global chann_ sitebot_ ftp_

    if { $chan == $chann_(spam) && $nick == $sitebot_(nick) } {
        
         set pattern {iD3 > ([^>]+)? > ([^>]+)? from ([0-9]+)? at ([0-9]+)? Hz}
         
         regexp $pattern $arg match rlsname genre year sampling
         

         if {[string trim $genre] == ""} { set genre "-" }
	 if {[string trim $year] == ""} { set year "-" }
	 if {[string trim $sampling] == ""} { set sampling "-" }
	     
         
         if { $rlsname == "" } {	
		  putquick "PRIVMSG $chann_(echo) :\0034 Error!! Release Name Empty\003"
		  return		
	    }
		
		
		 putquick  "PRIVMSG $chann_(echo) :!addid3c $rlsname $genre $year $sampling"
         		 
		}
    }	
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Hello,
Worth keeping in mind, when it comes to regexp and regular expressions:
The variables listed in the command line, will only be set if there is a match for the pattern within the text string. The regexp command will return 1 if a match was found, otherwise 0.
NML_375
C
COBRa
Halfop
Posts: 49
Joined: Fri Jan 04, 2013 8:23 am

Post by COBRa »

I've checked the pattern and it seems ok i dont understand where the error is coming from is it the code ive posted plz ?
Post Reply