| View previous topic :: View next topic |
| Author |
Message |
COBRa Halfop
Joined: 04 Jan 2013 Posts: 49
|
Posted: Fri Jun 20, 2014 7:06 am Post subject: Lindex help |
|
|
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: | 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 |
|
| Back to top |
|
 |
CrazyCat Revered One

Joined: 13 Jan 2002 Posts: 1032 Location: France
|
Posted: Fri Jun 20, 2014 7:28 am Post subject: |
|
|
I think you have to get the informations from the end after the "variable size":
| Code: | 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]] |
_________________ https://www.eggdrop.fr - French IRC network
Offer me a coffee - Do not ask me help in PM, we are a community. |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Fri Jun 20, 2014 10:14 am Post subject: |
|
|
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, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
CrazyCat Revered One

Joined: 13 Jan 2002 Posts: 1032 Location: France
|
Posted: Sat Jun 21, 2014 5:25 am Post subject: |
|
|
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: | | set arg [split $arg] |
It'll propermly format the elements of the list, particularly interesting if you have special chars in them. _________________ https://www.eggdrop.fr - French IRC network
Offer me a coffee - Do not ask me help in PM, we are a community. |
|
| Back to top |
|
 |
COBRa Halfop
Joined: 04 Jan 2013 Posts: 49
|
Posted: Tue Jun 24, 2014 10:37 am Post subject: |
|
|
| @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 |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Tue Jun 24, 2014 1:56 pm Post subject: |
|
|
Hello,
A crude example matching your posted strings is as follows;
| Code: | 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, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
COBRa Halfop
Joined: 04 Jan 2013 Posts: 49
|
Posted: Thu Jun 26, 2014 10:27 am Post subject: |
|
|
many thx for the swift reply do i still use the lindex code as well ie
| Code: | 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: | | set rlsname[string trim [lindex $pattern $arg 1]] |
as i have no clue lol |
|
| Back to top |
|
 |
CrazyCat Revered One

Joined: 13 Jan 2002 Posts: 1032 Location: France
|
|
| Back to top |
|
 |
COBRa Halfop
Joined: 04 Jan 2013 Posts: 49
|
Posted: Fri Jun 27, 2014 12:00 am Post subject: |
|
|
So I could use something like
| Code: | $rlsname=""
$genre="" |
As I'm not sure |
|
| Back to top |
|
 |
CrazyCat Revered One

Joined: 13 Jan 2002 Posts: 1032 Location: France
|
Posted: Fri Jun 27, 2014 3:39 am Post subject: |
|
|
...
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. _________________ https://www.eggdrop.fr - French IRC network
Offer me a coffee - Do not ask me help in PM, we are a community. |
|
| Back to top |
|
 |
COBRa Halfop
Joined: 04 Jan 2013 Posts: 49
|
Posted: Fri Jun 27, 2014 3:48 am Post subject: |
|
|
| 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 |
|
| Back to top |
|
 |
CrazyCat Revered One

Joined: 13 Jan 2002 Posts: 1032 Location: France
|
Posted: Fri Jun 27, 2014 8:46 am Post subject: |
|
|
Just a small question before answering you: even if a field is blank, is it mentionned, like:
| Code: | | 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: | | set pattern {iD3 > ([^>]+)? > ([^>]+)? from ([0-9]+)? at ([0-9]+)? Hz} |
And to set an empty field to "-", just do:
| Code: | | if {[string trim $genre]==""]} { set genre "-" } |
_________________ https://www.eggdrop.fr - French IRC network
Offer me a coffee - Do not ask me help in PM, we are a community. |
|
| Back to top |
|
 |
COBRa Halfop
Joined: 04 Jan 2013 Posts: 49
|
Posted: Fri Jun 27, 2014 10:21 am Post subject: |
|
|
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: | 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"
}
}
|
|
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Fri Jun 27, 2014 11:47 am Post subject: |
|
|
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, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
COBRa Halfop
Joined: 04 Jan 2013 Posts: 49
|
Posted: Fri Jun 27, 2014 12:41 pm Post subject: |
|
|
| 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 ? |
|
| Back to top |
|
 |
|